HTML - charset Attribute



The HTML charset attribute is used for 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

Following is the syntax for HTML charset attribute −

<meta charset = "value">

Example

In the following example, we are using the charset attribute with the meta tag.

<!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>

When we run the above code, it will generate an output consisting of the text displayed on the wbpage.

html_attributes_reference.htm
Advertisements