Character Encoding with <meta charset>
What you'll learn: How to declare UTF-8 encoding so your web pages display text correctly in any language.
Why Character Encoding Matters
Imagine you're writing a letter, but the person reading it doesn't know which alphabet you're using. Your message might appear as gibberish! Similarly, browsers need to know which character encoding your HTML document uses to translate the bytes of your file into readable text.
Without declaring an encoding, special characters, emojis, and non-English text (like "你好" or "مرحبا") might display as question marks or strange symbols.
What is UTF-8?
UTF-8 is a universal character encoding standard that supports virtually all written languages, symbols, and emoji. It's the recommended encoding for all modern web pages because it ensures your content appears correctly for users worldwide.
The <meta charset> Tag
Inside your document's <head> element, you declare the character encoding with this tag:
<meta charset="UTF-8">
This single line tells the browser: "Interpret this document's text using UTF-8 encoding."
Where It Goes
Place this tag early in your <head>, ideally right after the opening <head> tag. Browsers scan the first 1024 bytes of your document to detect encoding, so declaring it early ensures proper rendering from the start:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- Other head elements follow -->
</head>
Key Takeaway: Always include <meta charset="UTF-8"> at the top of your <head> element to ensure proper text rendering across all languages and prevent display issues with special characters.