HTML charset Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The charset attribute on a <meta> element declares the document’s character encoding — how bytes become text. In HTML5, the only valid value is a case-insensitive match for utf-8.

Put it early in <head> (within the first 1024 bytes). Pair the page with lang as needed. For form submissions, see accept-charset. The old http-equiv Content-Type pragma is a legacy alternative.

Baseline · Widely available.

On <meta>, charset is not deprecated. Use <meta charset="UTF-8"> on every HTML page.

Obsolete elsewhere.

Do not put charset on <script>, <a>, or <link>. Those uses are obsolete — scripts inherit the document encoding; linked resources should use HTTP Content-Type.

HTML Syntax

syntax.html
<meta charset=" utf-8">

UTF-8, utf-8, and Utf-8 are all valid — the match is case-insensitive.

Attribute Values

What belongs on modern pages:

ValueDescriptionTry it
UTF-8Only valid HTML5 document encoding (ASCII case-insensitive)Try it
Early in <head>Must appear within the first 1024 bytes of the fileTry it
vs form encodingPage decode = charset; form submit = accept-charsetTry it
Legacy encodingsDo not use ISO-8859-1, Windows-1252, etc. on new HTMLTry it

Live Preview

This page’s encoding (via document.characterSet) and sample UTF-8 text. Change the display sample — the real charset is already UTF-8.

UTF-8 covers virtually all languages and symbols. Always declare <meta charset="UTF-8">.

Document encoding

Hello — café, naïve, résumé

<meta charset="UTF-8">

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Declare UTF-8

The standard one-liner every HTML page should include.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>My page</title>
</head>
<body>
  <p>Café, naïve, and 日本語 render correctly in UTF-8.</p>
</body>
</html>
Try It Yourself

How It Works

  1. meta charset tells the browser how to decode the file.
  2. UTF-8 is the only valid HTML5 document encoding.
  3. Save the file as UTF-8 in your editor — declaration alone is not enough if the bytes are wrong.
  4. lang is separate: it names the language, not the encoding.

Example 2 — Place It Early

Keep the declaration inside the first 1024 bytes — before long titles or comments.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Early charset declaration</title>
</head>
<body>
  <h1>Encoding is set before other head content piles up</h1>
</body>
</html>
Try It Yourself

How It Works

  1. First in head (or near the top) is the safe habit.
  2. 1024-byte rule exists so the browser can decode before guessing wrong.
  3. Avoid huge comments before the meta charset line.
  4. viewport and title can follow immediately after.

Example 3 — Page charset + Form accept-charset

Two different jobs: decode the HTML vs encode submitted form data.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Contact</title>
</head>
<body>
  <form action="/contact" method="post" accept-charset="UTF-8">
    <label for="name">Name:</label>
    <input type="text" id="name" name="name">
    <button type="submit">Send</button>
  </form>
</body>
</html>
Try It Yourself

How It Works

  1. meta charset decodes the HTML document as UTF-8.
  2. accept-charset documents UTF-8 for the form payload.
  3. Both should be UTF-8 on modern sites.
  4. Server must also expect UTF-8 for names like José.

Edge Cases & Pitfalls

Mojibake almost always means a mismatch between file bytes, HTTP headers, and meta charset.

UTF-8 only

Do not invent other encodings

HTML5 requires utf-8. Save editors and databases as UTF-8 as well.

1024 bytes

Too late in the file

If the declaration sits after the first 1024 bytes, the browser may already have guessed wrong.

obsolete

No charset on script / link / a

Obsolete. Omit it on scripts; use HTTP Content-Type for linked resources.

HTTP

HTTP header can override

A Content-Type: …; charset=… response header takes precedence. Keep server and meta in agreement (UTF-8).

legacy

http-equiv Content-Type

Still works historically, but prefer the shorter <meta charset="UTF-8">.

Browser Support

charset on <meta> is an HTML attribute for document encoding. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

charset

Universally supported on meta. Always use UTF-8. Obsolete on script, a, and link — do not use those forms.

99% Global support (approx.)
Google Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All modern
Full support
Internet Explorer All (use UTF-8)
Supported
meta charset Widely supported

Bottom line: Put

near the top of every HTML document. Keep server Content-Type charset in sync.

Key Takeaways

  • Role: declare how the browser decodes the HTML document.
  • Value: only UTF-8 (case-insensitive) in HTML5.
  • Placement: early in <head>, within the first 1024 bytes.
  • Not on script/link/a — those charset uses are obsolete.

One line: every page needs <meta charset="UTF-8"> near the top of <head>.

Frequently Asked Questions

On a meta element, charset declares the character encoding used to decode the HTML document. In HTML5 the only valid value is UTF-8 (case-insensitive).
In the head, entirely within the first 1024 bytes of the document — ideally as one of the first tags after the opening head.
Yes for HTML documents. The value must be an ASCII case-insensitive match for "utf-8". Save your files as UTF-8 too.
meta charset tells the browser how to read the page. accept-charset on a form states the encoding for submitted form data (also UTF-8 in current HTML).
No. charset on script is obsolete/redundant (scripts inherit the document encoding). charset on a and link is obsolete — use HTTP Content-Type instead.
Use document.characterSet (legacy alias: document.charset) to read the document’s character encoding.

Next: checked

Pre-select checkboxes, radios, and options.

checked tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful