CSS Reference ยท Typography
The direction property
Reference for the CSS direction property: ltr and rtl, how it flips text, flex and table order, why the HTML dir attribute is usually better, with a live demo.
The direction property sets the base direction of text and of the inline layout inside an element: ltr for left-to-right scripts such as English, or rtl for right-to-left scripts such as Arabic and Hebrew. It decides where lines start, which side text-align: start means, the order of inline boxes and flex items in a row, and the column order of tables.
For content in a document, you should normally set the direction with the HTML dir attribute, not with CSS. The attribute is part of the markup, so it keeps working when styles fail to load. Use the CSS property for cases where direction is purely a presentation choice.
.arabic { direction: rtl; }
Try it
In the second panel, the lines start at the right edge, the exclamation mark moves to the left end of the English phrase (the base direction decides where neutral characters go), and the numbered flex items run from right to left.
Syntax
direction: ltr | rtl;
| Initial value | ltr |
| Applies to | all elements |
| Inherited | yes |
| Animatable | no |
| Computed value | as specified |
Values
| Value | Description |
|---|---|
ltr |
Left-to-right text and inline direction. Lines start at the left; the initial value. |
rtl |
Right-to-left text and inline direction. Lines start at the right. |
Usage notes
- Prefer
dirin HTML.<html dir="rtl" lang="ar">ordiron a single element describes the content's direction. The CSS property is for presentation. Browsers mapdirtodirectionthrough their default style sheet. - The browser still reorders text. Even in an
ltrblock, Arabic or Hebrew words display right-to-left, thanks to the Unicode bidirectional algorithm.directionsets the base direction that algorithm starts from. Seeunicode-bidifor overriding it. - What flips. Inline content,
text-align: startandend, flex rows, grid column order, table column order, horizontal scrollbar start position, and the default position of overflowing content. - What does not flip. Physical properties such as
margin-leftandleftkeep their meaning. Use logical properties (margin-inline-start,inset-inline-start) so spacing follows the direction; see Writing Modes and Logical Properties. - Direction versus writing mode.
directioncontrols the inline direction within the currentwriting-mode. In vertical writing it flows top-to-bottom or bottom-to-top. - Matching in selectors. The
:dir()pseudo-class selects elements by their direction; see:dir(). - Accessibility. Always set the language and direction of the real content; assistive technologies and spell checkers depend on both.
Browser support
Supported in every browser.
Related
unicode-bidiโ how embedded text is orderedwriting-modeโ horizontal or vertical text:dir()โ select by text directiontext-alignโstartandendfollow the direction- Lesson: Writing Modes and Logical Properties