HTML Reference Β· Characters and Codes
Character Sets and Encodings
Reference for HTML character encodings: UTF-8, UTF-16, ISO-8859-1, Windows-1252 and others, the labels browsers accept, and how to declare UTF-8 correctly.
A web page travels as bytes. The character encoding (often called the charset) is the rule the browser uses to turn those bytes back into characters. If the browser picks the wrong rule, text turns into garbage such as cafΓΒ© instead of cafΓ©, a problem known as mojibake.
Modern HTML has a simple rule: save your files as UTF-8 and declare UTF-8. Every other encoding on this page exists for old pages and old data.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>My page</title> </head>
Declaring the encoding
A browser looks for the encoding in this order and uses the first one it finds:
| Priority | Source | Example |
|---|---|---|
| 1 | A byte order mark (BOM) at the very start of the file | The bytes EF BB BF mean UTF-8 |
| 2 | The charset parameter of the HTTP Content-Type header |
Content-Type: text/html; charset=utf-8 |
| 3 | A <meta> declaration within the first 1024 bytes |
<meta charset="utf-8"> |
| 4 | The parent page's encoding (for some frames) or a guess based on the content and locale | Often Windows-1252 in Western locales |
- Put
<meta charset="utf-8">first in<head>, before the<title>and any text, so it is safely within the first 1024 bytes. - The older long form
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">still works, but the short form is simpler. Use only one of them. - The server header wins over the meta tag. If your server sends a different charset, fix the server configuration; the meta tag cannot override it.
- Labels are case-insensitive, so
utf-8,UTF-8andutf8are all fine.utf-8is the usual spelling. - Other places that mention an encoding:
<form accept-charset="utf-8">(forms already default to the page's encoding) and thecharsetparameter on CSS and JavaScript responses. Thecharsetattribute on<script>is obsolete.
Try it
See the bytes behind any text, and what happens when UTF-8 bytes are read with the wrong encoding.
The main encodings
| Encoding | Bytes per character | Covers | Notes |
|---|---|---|---|
| UTF-8 | 1 to 4 | All of Unicode | The web standard and the only encoding new HTML documents should use. ASCII text is byte-for-byte the same, so it is backward compatible with ASCII. Used by the vast majority of websites. |
| UTF-16 | 2 or 4 | All of Unicode | Used inside JavaScript strings, Java and Windows. It is not ASCII-compatible, so a <meta> tag cannot declare it; it needs a BOM or an HTTP header. Avoid it for web pages. |
| ISO-8859-1 (Latin-1) | 1 | 256 characters: ASCII plus Western European letters | The old default of HTTP and HTML 4. Browsers treat this label as Windows-1252. |
| Windows-1252 | 1 | 256 characters: Latin-1 plus curly quotes, dashes, β¬ and more |
The real encoding behind most "Latin-1" pages. It fills bytes 80β9F with printable characters (see below). |
| ISO-8859-15 (Latin-9) | 1 | 256 characters | Latin-1 updated with β¬, Ε, Ε , Ε½ and a few others. |
| US-ASCII | 1 | 128 characters | English letters, digits and punctuation only. Browsers treat this label as Windows-1252. See the ASCII table. |
| Shift_JIS, EUC-JP, ISO-2022-JP | 1 or 2 | Japanese | Common on older Japanese sites. |
| GBK, gb18030 | 1, 2 or 4 | Simplified Chinese | gb18030 can encode all of Unicode. |
| Big5 | 1 or 2 | Traditional Chinese | Legacy pages from Taiwan and Hong Kong. |
| EUC-KR | 1 or 2 | Korean | Legacy Korean pages. |
| Windows-1251, KOI8-R, KOI8-U | 1 | Cyrillic | Legacy Russian, Ukrainian and other Cyrillic pages. |
How UTF-8 stores characters
| Code point range | Bytes | Examples |
|---|---|---|
| U+0000 β U+007F | 1 | ASCII: A = 41 |
| U+0080 β U+07FF | 2 | Latin accents, Greek, Cyrillic, Hebrew, Arabic: Γ© = C3 A9 |
| U+0800 β U+FFFF | 3 | Most other scripts and symbols: β¬ = E2 82 AC, δΈ = E4 B8 AD |
| U+10000 β U+10FFFF | 4 | Emoji and rare scripts: π = F0 9F 98 80 |
Encoding labels browsers accept
Browsers follow the WHATWG Encoding Standard, which maps dozens of historical labels onto a fixed set of encodings. The value you put in charset must be one of these labels. The table lists every encoding with a selection of its labels.
| Encoding | Example labels |
|---|---|
| UTF-8 | utf-8, utf8, unicode-1-1-utf-8 |
| UTF-16LE | utf-16le, utf-16, unicode, ucs-2 |
| UTF-16BE | utf-16be |
| windows-1252 (Western) | windows-1252, cp1252, iso-8859-1, latin1, l1, ascii, us-ascii |
| windows-1250 (Central European) | windows-1250, cp1250 |
| windows-1251 (Cyrillic) | windows-1251, cp1251 |
| windows-1253 (Greek) | windows-1253, cp1253 |
| windows-1254 (Turkish) | windows-1254, iso-8859-9, latin5 |
| windows-1255 (Hebrew) | windows-1255, cp1255 |
| windows-1256 (Arabic) | windows-1256, cp1256 |
| windows-1257 (Baltic) | windows-1257, cp1257 |
| windows-1258 (Vietnamese) | windows-1258, cp1258 |
| windows-874 (Thai) | windows-874, tis-620, iso-8859-11 |
| ISO-8859-2, -3, -4, -5, -6, -7, -8, -8-I, -10, -13, -14, -15, -16 | iso-8859-2, latin2, iso-8859-5, cyrillic, iso-8859-6, arabic, iso-8859-7, greek, iso-8859-8, hebrew, iso-8859-15, latin9, and so on |
| KOI8-R, KOI8-U | koi8-r, koi8, koi8-u |
| IBM866 | ibm866, cp866 |
| macintosh | macintosh, mac, x-mac-roman |
| x-mac-cyrillic | x-mac-cyrillic, x-mac-ukrainian |
| GBK | gbk, gb2312, chinese, x-gbk |
| gb18030 | gb18030 |
| Big5 | big5, big5-hkscs, cn-big5 |
| EUC-JP | euc-jp, x-euc-jp |
| ISO-2022-JP | iso-2022-jp |
| Shift_JIS | shift_jis, sjis, ms932, windows-31j |
| EUC-KR | euc-kr, korean, windows-949, ks_c_5601-1987 |
| replacement | iso-2022-kr, iso-2022-cn, hz-gb-2312 (unsafe legacy encodings; the content decodes to a single U+FFFD) |
| x-user-defined | x-user-defined |
A <meta> tag that names UTF-16 is treated as UTF-8, because a parser that can read the tag is already reading ASCII-compatible bytes. Labels that are not in the standard, such as utf-32, are ignored.
Windows-1252 vs ISO-8859-1
The two encodings agree on every byte except 80β9F. ISO-8859-1 uses that range for invisible C1 control characters, while Windows-1252 puts useful typography there. That is why browsers decode iso-8859-1 pages as Windows-1252: real pages labeled Latin-1 almost always contain these characters.
| Byte | Character | Code point | Name |
|---|---|---|---|
| 80 | β¬ | U+20AC | Euro sign |
| 81 | (control) | U+0081 | Unassigned; browsers decode it as a C1 control character |
| 82 | β | U+201A | Single low-9 quotation mark |
| 83 | Ζ | U+0192 | Latin small letter f with hook |
| 84 | β | U+201E | Double low-9 quotation mark |
| 85 | β¦ | U+2026 | Horizontal ellipsis |
| 86 | β | U+2020 | Dagger |
| 87 | β‘ | U+2021 | Double dagger |
| 88 | Λ | U+02C6 | Modifier letter circumflex accent |
| 89 | β° | U+2030 | Per mille sign |
| 8A | Ε | U+0160 | Latin capital letter S with caron |
| 8B | βΉ | U+2039 | Single left-pointing angle quotation mark |
| 8C | Ε | U+0152 | Latin capital ligature OE |
| 8D | (control) | U+008D | Unassigned; browsers decode it as a C1 control character |
| 8E | Ε½ | U+017D | Latin capital letter Z with caron |
| 8F | (control) | U+008F | Unassigned; browsers decode it as a C1 control character |
| 90 | (control) | U+0090 | Unassigned; browsers decode it as a C1 control character |
| 91 | β | U+2018 | Left single quotation mark |
| 92 | β | U+2019 | Right single quotation mark |
| 93 | β | U+201C | Left double quotation mark |
| 94 | β | U+201D | Right double quotation mark |
| 95 | β’ | U+2022 | Bullet |
| 96 | β | U+2013 | En dash |
| 97 | β | U+2014 | Em dash |
| 98 | Λ | U+02DC | Small tilde |
| 99 | β’ | U+2122 | Trade mark sign |
| 9A | Ε‘ | U+0161 | Latin small letter s with caron |
| 9B | βΊ | U+203A | Single right-pointing angle quotation mark |
| 9C | Ε | U+0153 | Latin small ligature oe |
| 9D | (control) | U+009D | Unassigned; browsers decode it as a C1 control character |
| 9E | ΕΎ | U+017E | Latin small letter z with caron |
| 9F | ΕΈ | U+0178 | Latin capital letter Y with diaeresis |
Fixing encoding problems
| What you see | Likely cause | Fix |
|---|---|---|
ΓΒ©, Γ’β¬β’ or Γ’βΒ¬ instead of Γ©, β or β¬ |
UTF-8 bytes read as Windows-1252 | Declare UTF-8 in the HTTP header and with <meta charset="utf-8">. |
οΏ½ (the replacement character) |
Windows-1252 or Latin-1 bytes read as UTF-8 | Re-save the file as UTF-8 in your editor. |
 at the top of a page |
A UTF-8 BOM read as Windows-1252, often from an included file | Declare UTF-8, or save include files without a BOM. |
| Text looks fine in the page but breaks after saving to a database | The database or its connection uses another encoding | Use UTF-8 end to end, for example utf8mb4 in MySQL. |
Related
- Character entities β write any character with a reference
- ASCII table β the 7-bit base of almost every encoding here
- URL encoding β UTF-8 bytes in URLs
<meta>β thecharsetdeclaration- Lesson: Character Encoding and UTF-8
- Lesson: Meta Tags