language-icon Old Web
English
Sign In

Character encodings in HTML

HTML (Hypertext Markup Language) has been in use since 1991, but HTML 4.0 (December 1997) was the first standardized version where international characters were given reasonably complete treatment. When an HTML document includes special characters outside the range of seven-bit ASCII, two goals are worth considering: the information's integrity, and universal browser display. HTML (Hypertext Markup Language) has been in use since 1991, but HTML 4.0 (December 1997) was the first standardized version where international characters were given reasonably complete treatment. When an HTML document includes special characters outside the range of seven-bit ASCII, two goals are worth considering: the information's integrity, and universal browser display. There are several ways to specify which character encoding is used in the document. First, the web server can include the character encoding or 'charset' in the Hypertext Transfer Protocol (HTTP) Content-Type header, which would typically look like this: This method gives the HTTP server a convenient way to alter document's encoding according to content negotiation; certain HTTP server software can do it, for example Apache with the module mod_charset_lite. For HTML it is possible to include this information inside the head element near the top of the document: HTML5 also allows the following syntax to mean exactly the same: XHTML documents have a third option: to express the character encoding via XML declaration, as follows: Note that as the character encoding cannot be known until this declaration is parsed, there can be a problem knowing which encoding is used for the declaration itself. The main principle is that the declaration shall be encoded in pure ASCII, and therefore (if the declaration is inside the file) the encoding needs to be an ASCII extension. In order to allow encodings not backwards compatible with ASCII, browsers must be able to parse declarations in such encodings. Examples of such encodings are UTF-16BE and UTF-16LE. As of HTML5 the recommended charset is UTF-8. An 'encoding sniffing algorithm' is defined in the specification to determine the character encoding of the document based on multiple sources of input, including: For ASCII-compatible character encodings the consequence of choosing incorrectly is that characters outside the printable ASCII range (32 to 126) usually appear incorrectly. This presents few problems for English-speaking users, but other languages regularly—in some cases, always—require characters outside that range. In CJK environments where there are several different multi-byte encodings in use, auto-detection is also often employed. Finally, browsers usually permit the user to override incorrect charset label manually as well.

[ "HTML element", "HTML" ]
Parent Topic
Child Topic
    No Parent Topic