HTML charset Attribute

Beginner
⏱️ 4 min read
📚 Updated: Jun 2026
🎯 2 Examples
Document & Encoding

Introduction

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.

What You’ll Learn

01

UTF-8

Modern default.

02

meta tag

Where it lives.

03

Early head

First 1024 bytes.

04

Mojibake

Garbled text fix.

05

characterSet

JS read API.

06

vs accept

Page vs form.

Purpose of charset

The 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.

💡
Not the same as accept-charset

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

📝 Syntax

Place charset on a <meta> element in the document <head>:

charset.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Your Page Title</title>
</head>
<body>...</body>
</html>

Syntax Rules

  • Use on <meta charset="UTF-8"> in HTML5 (no http-equiv needed).
  • Place as early as possible in <head>, within the first 1024 bytes.
  • Value is a character encoding label such as UTF-8 or ISO-8859-1.
  • The server may also send encoding via the HTTP Content-Type header.
  • For new sites, always choose UTF-8.

💎 Values

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.
charset-values.html
<!-- Recommended -->
<meta charset="UTF-8">

<!-- Legacy HTML4 style (still valid but verbose) -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

⚡ Quick Reference

AttributePurposeNotes
<meta charset="UTF-8">Declare document encodingPlace early in <head>
UTF-8Unicode (recommended)Default for HTML5
accept-charsetForm submission encodingDifferent attribute on <form>
document.characterSetRead encoding in JSPreferred DOM property
HTTP headerContent-Type: text/html; charset=UTF-8Server-level alternative
HTML5 statusValid and essentialAlways include on every page

Applicable Elements

ElementSupported?Notes
<meta>YesPrimary use: <meta charset="UTF-8">
<script> (external)Legacy onlyObsolete in HTML5; server should send correct type
<form>NoUse accept-charset instead
<table>NoNot related to table char attribute
<html>NoSet encoding via <meta> or HTTP header

Examples Gallery

Example with meta charset and reading the encoding with JavaScript.

👀 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.

Example

To include the charset attribute in an HTML document, place it within the <meta> tag in the document’s <head> section:

charset.html
<!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>
Try It Yourself

How It Works

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.

Dynamic Values with JavaScript

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:

charset.html
<p id="encoding"></p>

<script>
  // Read the document character encoding
  document.getElementById("encoding").textContent =
    "Document encoding: " + document.characterSet;
</script>
Try It Yourself

How It Works

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.

♿ Accessibility

  • Correct encoding enables readable text — Without UTF-8, screen readers may receive garbled content.
  • Supports multilingual users — UTF-8 ensures names and labels in any script display correctly.
  • Pair with lang attribute — Use <html lang="en"> alongside charset for language and encoding.
  • Avoid mojibake in forms — Correct page encoding helps users see what they type before submission.
  • Test special characters — Verify accented letters and non-Latin text in real assistive tech sessions.

🧠 How charset Works

1

Author declares charset

Add <meta charset="UTF-8"> early in the document head.

Markup
2

Browser reads bytes

The parser uses the declared encoding to convert file bytes into characters.

Decoding
3

Text renders correctly

Accented letters, CJK scripts, emoji, and symbols display as intended.

Display
=

Universal readable page

UTF-8 charset on every page prevents mojibake across languages.

Browser Support

The HTML5 <meta charset> attribute is universally supported in all modern browsers. UTF-8 is the HTML5 default encoding.

HTML5 standard · Essential

Supported everywhere

Always include <meta charset="UTF-8"> as the first element in <head>.

100% Modern browsers
Google Chrome Full support
Supported
Mozilla Firefox Full support
Supported
Apple Safari Full support
Supported
Microsoft Edge Full support
Supported
meta charset attribute Universal

Bottom line: Every HTML page should declare UTF-8 with <meta charset="UTF-8"> in the head.

💡 Best Practices

✅ Do

  • Always use <meta charset="UTF-8"> on every page
  • Place charset as the first element inside <head>
  • Save HTML files as UTF-8 on disk
  • Match server Content-Type header to UTF-8
  • Pair with <html lang="..."> for accessibility

❌ Don’t

  • Omit charset and rely on browser guessing
  • Confuse charset with accept-charset on forms
  • Use legacy ISO-8859-1 for new projects
  • Try to change encoding after the page has loaded
  • Confuse charset with table char attribute
  • Always specify the charset attribute to ensure proper text rendering. The default value for modern web pages is UTF-8, which supports a wide range of characters.
  • Be consistent with the chosen character encoding throughout your website to avoid conflicts and display issues.
  • Test your web pages across different browsers to ensure consistent rendering, especially when dealing with special characters and multilingual content.

Conclusion

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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about charset

Bookmark these before publishing any HTML page.

5
Core concepts
📝 02

meta tag

In head.

Syntax
⚠️ 03

Early

First in head.

Placement
⚙️ 04

characterSet

Read in JS.

Script
📋 05

vs accept

Page vs form.

Compare

❓ Frequently Asked Questions

It tells the browser which character encoding to use when decoding the HTML document, such as UTF-8.
Inside <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.
UTF-8 for virtually all modern websites. It supports every language and emoji.
You can read it with document.characterSet, but changing encoding after load does not re-decode the page. Declare it in <head> instead.
The browser may guess the encoding, which can cause mojibake (garbled text) for non-ASCII characters.

Declare UTF-8 on every page

Practice the charset attribute with a proper <meta charset="UTF-8"> document and read the encoding with JavaScript.

Try UTF-8 example →

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.

5 people found this page helpful