Why it matters
- Without a declared direction, the browser lays the page out left to right: lines start on the left, the punctuation at the end of a sentence moves to its start, and numbers and English words inside Arabic sentences end up in the wrong places.
- Direction is part of the text's meaning, so it belongs in HTML, not CSS: this is the recommendation of the W3C and the HTML standard. Styles may fail to load or be ignored, for example in a browser's reader mode or when the content is copied elsewhere, while the attribute stays with the text.
- Putting it on
<body>alone is not enough: elements outside<body>do not inherit it, including<title>, which appears in the browser tab and in bookmarks.
Example
<html lang="ar">
<html lang="ar" dir="rtl">
How to fix
Put the direction and the language together on <html>:
html
<html lang="ar" dir="rtl">- You can then remove
direction: rtlfrom CSS if it is set onhtmlorbody; the attribute replaces it. - When part of the page needs the other direction, such as a code sample or an English paragraph, put
dir="ltr"on that element only. dir="auto"does not suit a whole page: the browser guesses the direction from the first letter of the text, so it may choose left to right when the page starts with a Latin brand name.
How we detect
- We count the letters of the visible text in
<body>, as in the page-language rule; the rule applies when more than half of them are Arabic-script. That includes other languages written in Arabic script, such as Persian and Urdu, which are also written right to left. - We read the
dirattribute of<html>. The rule passes when its value isrtl, in any letter case. - It fails when the attribute is missing or is
ltr,autoor anything else. Whendir="rtl"is on<body>only, the message says so.
This version does not read CSS, and the rule asks for the attribute even when CSS sets the direction.