HTML - charset Attribute



HTML charset attribute is used for defining the character encoding in the HTML document.

If it is present in the <meta> element, its value must be an ASCII case-sensitive match for the string utf-8, because UTF-8 is only valid encoding for HTML5 documents.

Different charsets include ASCII, ANSI, ISO-8859-1, UTF-8, etc. ISO-8859-1 supports 256 different character codes. ASCII defined 128 different alphanumeric characters.

You can use this attribute with <meta> as well as <script> tag, but for the script element, the charset attribute is considered as the deprecated attribute. The charset attribute is used with <meta> tag so the specify the character encoding.

In HTML, character encoding is a technique used to convert bytes into characters. It is important to choose the right character encoding for an HTML document to validate or display it properly.

Syntax

<meta charset = "value">

It contains the value i.e character_set which specify the character encoding for the HTML document.

Applies On

Below listed elements allow using of the HTML charset attribute.

Element Description
<meta> HTML <meta> tag lets us specify metadata, which is additional important information about a document.
<script> HTML <script> The script tag is employed for defining a client-side script for image manipulation, form validation, and dynamic content updates.

Example of charset attribute in meta tag

In the following example, we are using the charset attribute with the meta tag. When we run the above code, it will generate an output consisting of the text displayed on the wbpage.

<!DOCTYPE html>
<html lang="en">
<head>
   <!--UTF-8-->
   <meta charset="UTF-8">
   <title>HTML 'charset' attribute</title>
</head>
<body>
   <!--HTML 'charset' attribute-->
   <h2>Example of the HTML 'charset' attribute</h2>
   <p>We are trying to understand the usage of the 
   'charset' attribute with the help of this program. </p>
</body>
</html>

Supported Browsers

Attribute Chrome Edge Firefox Safari Opera
charset Yes Yes Yes Yes Yes
html_attributes_reference.htm
Advertisements