Text encoding, entities, languages and right-to-left pages
Show every character correctly: UTF-8, entities, the lang attribute, right-to-left layouts, emoji and multilingual pages.
In this lesson you will
- Fix broken characters with UTF-8
- Write HTML entities
- Declare language and direction
- Prepare pages for several languages
About 14 minutes
Text is harder than it looks
Priya’s friend Ahmed in Karachi writes the menu in Urdu. He pastes it into the site and the page shows strange symbols like ال. Her price list shows a broken question mark where the rupee sign should be. A customer from Paris sees her name written without accents. All three problems come from the same root: how text is stored and described. Get it right once and it just works worldwide.
Character encoding in one paragraph
A computer stores text as numbers. An encoding is the table that says which number is which letter. Old tables only handled English, and others handled one region each, so mixing them caused garbage. UTF-8 is the modern table that covers every written language, plus symbols and emoji. Your job is to save your file as UTF-8 and tell the browser so.
<meta charset="UTF-8">
Both parts matter. Your editor must save the file as UTF-8 (most do by default), and the tag tells the browser how to read it. If a page shows odd characters, one of those two is wrong, and the cause is almost always a missing charset tag or a file saved in an older encoding.
HTML entities
Some characters have a special meaning in HTML. The less-than sign starts a tag, and the ampersand starts an entity. To show them as plain text, write an entity, which starts with an ampersand and ends with a semicolon.
| You want | Write | Name |
|---|---|---|
| < | < |
less than |
| > | > |
greater than |
| & | & |
ampersand |
| " | " |
quote |
| © | © |
copyright |
| € | € |
euro |
| non-breaking space | |
nbsp |
With UTF-8 you can usually type the symbol directly, such as the copyright sign or an accented letter. The entities that you really must use are for <, > and & when you want to display them as text, which is how code samples are shown on pages like this one.
Press Run and see how the entities display. Then add a line that shows the text 5 < 10 & 10 > 5 correctly by using entities.
<p>Price: €20 © 2026</p>
<p>Write 5 < 10 here</p>
Replace the < with < and add the rest using & and >.
<p>Price: €20 © 2026</p>
<p>Write 5 < 10 & 10 > 5 here</p>
The nbsp and typography tools
is a space that never breaks across lines. Use it between a number and its unit, such as10 kg, so they stay together.—and–give long and short dashes.…gives an ellipsis.<wbr>suggests a place where a very long word may break.<br>makes a line break. Use it for addresses and poems, not for spacing between paragraphs.
Using many non-breaking spaces to create gaps or alignment is a bad habit. Spacing belongs in CSS with margin and padding.
Declare the language
<html lang="en">
...
<p>Priya says: <span lang="fr">Bon appétit!</span></p>
The lang attribute helps screen readers choose the right pronunciation, helps browsers offer to translate, helps search engines serve the page to the right audience, and improves hyphenation and quote styles in CSS. Use en, ur, fr and so on, or the longer form such as en-GB where it matters.
Right-to-left languages
Arabic, Urdu, Hebrew and Persian are written from right to left. For a whole page, set the direction on the root.
<html lang="ur" dir="rtl">
When one phrase in the opposite direction sits inside a paragraph, use dir on a span, or the <bdi> element, which isolates text of unknown direction such as a user’s name. For your layout, use logical CSS properties such as margin-inline-start and padding-inline-end instead of left and right, and the page adapts to both directions automatically.
This Urdu sentence is displayed from left to right. Add lang=”ur” and dir=”rtl” to the paragraph so it flows correctly.
<p>یہ ایک مثال ہے۔</p>
Both attributes go in the opening p tag.
<p lang="ur" dir="rtl">یہ ایک مثال ہے۔</p>
Emoji and symbols
Emoji are just characters in UTF-8, so you can paste them directly. Two cautions: they look different on different devices, and screen readers read their names aloud, so a row of ten emoji becomes a long and silly announcement. Use them sparingly, and not as the only way to convey meaning. For icons that must look the same everywhere, use SVG images instead.
Preparing for many languages
- Keep text out of images, so it can be translated.
- Leave room for longer text. German and Finnish words can be much longer than English ones.
- Show dates, numbers and currencies in the visitor’s format. JavaScript’s
Intltools help. - Use
hreflanglinks to tell search engines which page is the version for which language. - Test with real text in each language, not just placeholder text.
<link rel="alternate" hreflang="en" href="https://www.example.com/en/">
<link rel="alternate" hreflang="ur" href="https://www.example.com/ur/">
<link rel="alternate" hreflang="x-default" href="https://www.example.com/">
Is the file saved as UTF-8? Is the charset meta tag present and first? Does the server send UTF-8 in its headers? One of those three is the culprit almost every time.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
Which encoding covers every written language and emoji?
- ASCII
- UTF-8
- Latin-1
- BMP
UTF-8 is the universal modern encoding.
How do you display a literal less-than sign in HTML text?
<<<lt>- It cannot be done
Write the entity < so the browser shows a < character.
What does the lang attribute help with?
- Page speed
- Screen reader pronunciation and translation tools
- Image size
- Colour
It tells software which language the text is in.
Which attribute makes a whole page flow right to left?
dir="rtl"align="right"flow="left"lang="rtl"
dir=”rtl” sets the text direction.
Which CSS properties help layouts work in both directions?
- margin-left and margin-right
- Logical ones like margin-inline-start
- float only
- position fixed
Logical properties follow the text direction automatically.
Summary
- Save as UTF-8 and declare charset first.
- Use entities for characters that HTML treats as code.
- lang and dir make pages work for screen readers and for RTL scripts.
- Use hreflang for multilingual sites.
Questions
Why do I see strange symbols on my page?
The browser is reading the file with the wrong encoding. Add the UTF-8 charset tag and save the file as UTF-8.
Do I have to use entities for accented letters?
No. With UTF-8 you can type them directly.
Does lang affect SEO?
It helps search engines understand your page language and audience, together with hreflang for multi-language sites.
