👀 Live Preview
With UTF-8 declared, multilingual text renders correctly:
Hello! café — 日本語 — 😀
This tutorial page itself uses UTF-8 via <meta charset> in the document head.

The charset attribute in HTML plays a crucial role in specifying the character encoding for the document. Character encoding tells the browser how to interpret bytes as text, ensuring that special characters, symbols, and various languages are rendered accurately.
Modern default.
Where it lives.
First 1024 bytes.
Garbled text fix.
JS read API.
Page vs form.
charsetThe primary purpose of the charset attribute is to define the character encoding for the HTML document. This helps browsers interpret and display text correctly, avoiding issues such as mojibake (garbled characters) that may arise when the character encoding is not specified or mismatched with the actual file bytes.
<meta charset> controls how the browser decodes the HTML page. accept-charset on a <form> controls how submitted form data is encoded. They solve different problems.
Place charset on a <meta> element in the document <head>:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Your Page Title</title>
</head>
<body>...</body>
</html><meta charset="UTF-8"> in HTML5 (no http-equiv needed).<head>, within the first 1024 bytes.UTF-8 or ISO-8859-1.Content-Type header.UTF-8.The charset attribute takes a character encoding name (IANA charset label). Common values include:
UTF-8 — Unicode encoding; supports all languages and emoji. Standard for modern web pages.ISO-8859-1 — Latin-1 Western European encoding. Legacy; avoid for new projects.Windows-1252 — Common legacy Windows encoding. Prefer UTF-8 instead.<!-- Recommended -->
<meta charset="UTF-8">
<!-- Legacy HTML4 style (still valid but verbose) -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">| Attribute | Purpose | Notes |
|---|---|---|
<meta charset="UTF-8"> | Declare document encoding | Place early in <head> |
UTF-8 | Unicode (recommended) | Default for HTML5 |
accept-charset | Form submission encoding | Different attribute on <form> |
document.characterSet | Read encoding in JS | Preferred DOM property |
| HTTP header | Content-Type: text/html; charset=UTF-8 | Server-level alternative |
| HTML5 status | Valid and essential | Always include on every page |
| Element | Supported? | Notes |
|---|---|---|
<meta> | Yes | Primary use: <meta charset="UTF-8"> |
<script> (external) | Legacy only | Obsolete in HTML5; server should send correct type |
<form> | No | Use accept-charset instead |
<table> | No | Not related to table char attribute |
<html> | No | Set encoding via <meta> or HTTP header |
Example with meta charset and reading the encoding with JavaScript.
With UTF-8 declared, multilingual text renders correctly:
Hello! café — 日本語 — 😀
This tutorial page itself uses UTF-8 via <meta charset> in the document head.
To include the charset attribute in an HTML document, place it within the <meta> tag in the document’s <head> section:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Your Page Title</title>
</head>
<body>
<p>Hello! This page uses UTF-8 character encoding.</p>
</body>
</html>In this example, the charset attribute is set to UTF-8, indicating Unicode encoding for the document. The browser reads this before parsing body content so every character displays correctly.
You can read the document’s character encoding with JavaScript. Changing encoding after the page has loaded does not re-decode existing content, so always declare charset in <head> first:
<p id="encoding"></p>
<script>
// Read the document character encoding
document.getElementById("encoding").textContent =
"Document encoding: " + document.characterSet;
</script>document.characterSet returns the encoding the browser used for the document (typically UTF-8). The legacy alias document.charset also works, but changing either property after load does not re-parse the page—declare encoding in markup instead.
<html lang="en"> alongside charset for language and encoding.Add <meta charset="UTF-8"> early in the document head.
The parser uses the declared encoding to convert file bytes into characters.
Accented letters, CJK scripts, emoji, and symbols display as intended.
UTF-8 charset on every page prevents mojibake across languages.
The HTML5 <meta charset> attribute is universally supported in all modern browsers. UTF-8 is the HTML5 default encoding.
Always include <meta charset="UTF-8"> as the first element in <head>.
Bottom line: Every HTML page should declare UTF-8 with <meta charset="UTF-8"> in the head.
<meta charset="UTF-8"> on every page<head>Content-Type header to UTF-8<html lang="..."> for accessibilityaccept-charset on formschar attributecharset attribute to ensure proper text rendering. The default value for modern web pages is UTF-8, which supports a wide range of characters.The charset attribute is a fundamental element in HTML, determining how text is encoded and displayed on your web pages. Declared on <meta charset="UTF-8">, it prevents mojibake and supports global audiences.
By understanding and correctly implementing this attribute, you contribute to a seamless and accurate presentation of content for your website visitors. Make it the first line in every document head.
charsetBookmark these before publishing any HTML page.
Always use.
DefaultIn head.
SyntaxFirst in head.
PlacementRead in JS.
ScriptPage vs form.
Compare<head> as <meta charset="UTF-8">, ideally as the first element.meta charset decodes the HTML page. accept-charset on a form encodes submitted field values.document.characterSet, but changing encoding after load does not re-decode the page. Declare it in <head> instead.Practice the charset attribute with a proper <meta charset="UTF-8"> document and read the encoding with JavaScript.
5 people found this page helpful