Geek Slack

Introduction to HTML
About Lesson



HTML Encoding (Character Sets)


HTML Encoding (Character Sets)

HTML encoding, also known as character encoding, is a way to represent characters in HTML documents using standardized character sets. Proper encoding ensures that all characters, including special characters and symbols, are displayed correctly in web browsers.

1. Setting the Character Set

The character set of an HTML document is specified using the <meta> tag within the <head> section of the document. The most commonly used character set is UTF-8.

Example:

Setting the character set to UTF-8:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>UTF-8 Example</title>
</head>
<body>
    <p>This is a paragraph with UTF-8 encoding.</p>
</body>
</html>

2. Why Use UTF-8?

UTF-8 (Unicode Transformation Format – 8-bit) is the most widely used character encoding because it supports all characters in the Unicode standard. This includes characters from most of the world’s writing systems, as well as symbols and emojis.

Example:

Using UTF-8 to display special characters and symbols:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Special Characters Example</title>
</head>
<body>
    <p>Smiley face: 😀</p>
    <p>Euro sign: €</p>
    <p>Chinese character: ä½ </p>
</body>
</html>

3. HTML Entities for Special Characters

When you need to display special characters that have reserved meanings in HTML (like <, >, and &), you can use HTML entities to represent them.

Example:

Using HTML entities for special characters:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HTML Entities Example</title>
</head>
<body>
    <p>Less than: &lt;</p>
    <p>Greater than: &gt;</p>
    <p>Ampersand: &amp;</p>
    <p>Double quote: &quot;</p>
    <p>Single quote: &apos;</p>
</body>
</html>

4. Specifying Character Sets for External Resources

When linking to external resources like CSS or JavaScript files, it’s important to specify the character set to ensure these files are interpreted correctly by the browser.

Example:

Specifying the character set for an external CSS file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>External Resource Example</title>
    <link rel="stylesheet" href="styles.css" charset="UTF-8">
</head>
<body>
    <p>This is a paragraph with an external CSS file.</p>
</body>
</html>

5. Verifying Character Encoding

To ensure your HTML document is using the correct character encoding, you can verify it by checking the document’s meta tags and the browser’s settings.

Example:

Checking the character encoding in the browser:

  1. Open your HTML document in a web browser.
  2. Right-click on the page and select “View Page Source” or “Inspect”.
  3. Look for the <meta charset="UTF-8"> tag in the <head> section.

Conclusion

Setting the correct character encoding in your HTML documents is crucial for ensuring that all characters are displayed correctly across different browsers and devices. UTF-8 is the most widely used encoding and supports a wide range of characters, making it the best choice for most web projects.

Join the conversation