How to set character encoding for document in HTML5 ?
In this article, we will learn how to set character encoding for document in HTML5. Character encoding is a method of defining a mapping between bytes and text. To display an HTML document correctly, we must choose a proper character encoding.
The different types of character encoding include:
- ASCII Character Set: It is the first ever character encoding standard. The major disadvantage with ASCII is that it contained only a limited range of characters (128 characters).
- ANSI Character Set: This standard was an extended version of standard ASCII character set. It supports 256 characters.
- ISO-8859-1 Character Set: It is the default character encoding in HTML 2.0. It is also an extension of ASCII standard with International characters. This used full bytes (8-bits) to show characters.
- UTF-8 Character Set: This standard covers almost all of the characters and symbols in the world. The limitations of ANSI and ISO-8859-1 were satisfied by the UTF-8 Character Set. The default character encoding for HTML5 is UTF-8.
The HTML5 specification encourages developers to use the UTF-8 character set.
A character can be 1-4 bytes long in the UTF-8 Encoding Standard. This is also the most preferred encoding for email and web pages.
- Character encoding can be specified in the meta tag in HTML.
- The meta tag is used for specifying metadata about the webpage and will not be displayed in the web pages.
- The meta tag helps search engines to understand what a web page is about.
- The meta tag should be placed with the head tag in HTML.
Syntax:
1. For HTML4
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
2. For HTML5
The default character encoding for HTML5 is UTF-8, but you can still specify this to be extra cautious.
<meta charset="UTF-8">
Example: In this example, we will see the use of a meta character set
HTML
<!DOCTYPE html> < html > < head > <!-- NOTE: <meta charset="UTF-8"> is also applicable. --> < meta charset = "utf-8" > < title >Page Title</ title > </ head > < body > < h2 >Welcome To GFG</ h2 > < p > Default code has been loaded into the Editor. </ p > </ body > </ html > |
Output: