Direction
How right-to-left layout works: the dir attribute, logical properties, the parts that mirror and text of mixed direction.
The dir attribute
Arabic, Hebrew, Persian and Urdu are written right to left. Set dir="rtl" on <html>, or on one element for a passage, and the layout follows. The demo changes that one attribute and nothing else. The text stays in English so you can compare the two.
<html lang="ar" dir="rtl">
lang does not set direction. Set both. Direction belongs in the markup and not in CSS, because the browser's text algorithm and assistive technology read the attribute.
Logical properties
The library's styles name the start and end of a line, never left and right. Start is the left edge in English and the right edge in Arabic, so the same rule is correct in both.
.card { padding-left: 1rem; margin-right: auto; text-align: left; } .card { padding-inline-start: 1rem; margin-inline-end: auto; text-align: start; }
Write your own CSS the same way and it mirrors with the rest. The layout elements, such as <layout-sidebar> and <layout-cluster>, are built on flexbox and grid, which follow the direction without any extra rule.
What mirrors
Some parts need more than logical properties, because they point somewhere. The library flips these with :dir(rtl).
| Part | In right-to-left |
| Breadcrumb separators, list arrow markers, disclosure markers | Point the other way |
| Switches, range inputs, progress bars | Fill from the right |
| Blockquote and callout borders, sidebars | Move to the other edge |
| Carousels, reels and sliders | Advance from right to left |
| Arrow keys in tabs, carousels, sliders, steppers and splitters | Left Arrow moves forward and Right Arrow moves back |
Some things must not mirror: a clock, a media player's play button, a chart's time axis, and any icon that shows a real object. The library leaves those alone.
Text of mixed direction
A user name, a product title or a search term can be in any script. Dropped into a sentence of the other direction, its punctuation and numbers can end up on the wrong side. Wrap text of unknown direction in <bdi>, which isolates it from its surroundings.
<p><bdi>إيان</bdi>: 3 posts</p><p><bdi>Mira</bdi>: 12 posts</p>
For a form field that may be typed in either direction, dir="auto" sets the direction from the first strong character, and dirname sends the direction to the server with the value.
Direction in script
isRTL() answers whether a language is written right to left, from its tag.
import { isRTL } from 'vanilla-breeze'; isRTL('ar'); isRTL('he-IL'); isRTL('en'); document.documentElement.dir = isRTL(locale) ? 'rtl' : 'ltr';
In CSS, select on direction with :dir(rtl). It matches inherited direction, which [dir="rtl"] does not.