View video tutorial

HTML charset Attribute

HTML

The HTML charset attribute specifies the character encoding for an HTML document or an external resource.

Definition and Usage


➔ This attribute ensures that the text content of a webpage or linked file is displayed correctly across different browsers and operating systems.

➔ UTF-8 supports a wide variety of international characters and symbols, so UTF-8 is the universally recommended character set.

➔ It's a good idea to put meta charset="UTF-8" as the first element inside the <head> so that the browser can interpret the document correctly from the start.

➔ When used with the <script> element, the charset attribute specifies the character encoding used in an external script file, and when used with the <a> tag, the charset attribute specifies the character encoding used in an externally linked resource.

Code Sample
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Page Title here</title>
</head>
<body>
  <!-- Page Content goes here -->
</body>
</html>

Code Sample
<script charset="utf-8" src="pathofthe_script.js"></script>

Code Sample
<a href="https://w3context.com" charset="UTF-8">w3context.com</a>

Applies to

This attribute can be used on the following element.

Attribute Element
charset <meta>, <script>

Example
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Page Title here</title>
</head>
<body>
  <h2>HTML charset attribute example</h2>
  <p>The charset attribute specifies the character encoding for an HTML document.</p>
</body>
</html>

Click on the "copy" button and try this example in your project.