The Wayback Machine - https://web.archive.org/web/20250108115450/https://www.geeksforgeeks.org/html-charset-attribute/
Open In App

HTML charset Attribute

Last Updated : 28 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The charset attribute in HTML is used to define the character encoding. The charset attribute is used with the <meta> and <script> elements.
Charset attribute in <meta> Tag: The charset attribute is present in the meta element. It specifies the character encoding for the HTML document.

Supported Tags: 

Syntax:  

<meta charset="character_set"> 

Attribute Values: It contains the value i.e character_set which specifies the character encoding for the HTML document.

Values:

  • UTF-8: It specify the character encoding for Unicode.
  • ISO-8859-1: It specify the character encoding for the Latin alphabet.


Example: This example illustrates the use of charset attribute in meta element. 

html
<!DOCTYPE html>
<html>

<head>
    <title>
        HTML charset Attribute
    </title>

    <meta name="keywords" 
          charset="UTF-8" 
          content="Meta Tags, Metadata" />
</head>

<body style="text-align:center">
    <h1>Hello GeeksforGeeks!</h1>

    <h2>
        HTML charset Attribute in Meta Element
    </h2>
</body>

</html>

Output: 


Charset attribute in <script> Tag: When charset attribute present in the script element, it specifies the character encoding used in an external script. 

Syntax:  

<script charset="charset">

Example: This Example illustrates the use of charset attribute in script element.  

html
<!DOCTYPE html>
<html>

<head>
    <title>
        Charset Attribute in Script Tag
    </title>

    <style>
        body {
            text-align: center;
        }

        h1 {
            color: green;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>

    <h2>
        HTML charset Attribute in
        <code>&lt;script&gt;</code> Element
    </h2>

    <p id="Geeks"></p>

    <script charset="UTF-8">
        document.getElementById("Geeks").innerHTML = "Hello GeeksforGeeks!"; 
    </script>
</body>

</html>

Output:  


Supported Browsers: The browser supported by HTML charset Attribute are listed below:  

HTML charset Attribute – FAQs

What is the default charset value in HTML5?

The default charset value in HTML5 is UTF-8, which is a widely-used encoding that supports a vast array of characters from different languages.

Why is UTF-8 recommended as the charset value?

UTF-8 is recommended because it is compatible with most characters and symbols from all languages, ensuring that content is displayed correctly regardless of the user’s language or location.

Can I use other values for the charset attribute besides UTF-8?

Yes, other character encodings like ISO-8859-1 or Windows-1252 can be used, but UTF-8 is preferred for its broader compatibility.

What happens if the charset attribute is missing from an HTML document?

If the charset attribute is missing, browsers may default to ISO-8859-1 or attempt to guess the encoding, which can lead to characters being displayed incorrectly.

Is the charset attribute required for every HTML document?

While not strictly required, it is highly recommended to include the charset attribute to avoid issues with text rendering across different browsers and devices.



Next Article

Similar Reads

three90RightbarBannerImg