Converting Static HTML to RTL (Arabic & Hebrew): CSS & Layout Guide
Convert static HTML to RTL by setting dir="rtl" on the Arabic or Hebrew document and replacing physical CSS properties with logical ones. This changes the page’s base text direction and the starting edge for layout, but the main production caveat is that not everything should mirror: phone numbers, product codes, media controls, and some icons must keep their original orientation.
Start with the root element, then fix components that assume “start” means “left.” Keep one stylesheet for both directions where you can, and test the result with real translated content. A page can look correctly mirrored while still rendering a product code or its trailing punctuation in the wrong order.
Set language and direction at the document root
<!-- Broken: Arabic content still inherits left-to-right direction -->
<html lang="ar">
<body>
<main style="text-align: right">مرحبًا بك</main>
</body>
</html>
<!-- Fixed: root element for the Arabic edition -->
<html lang="ar" dir="rtl">
<!-- Existing head and translated body go here. -->
</html>
<!-- Root element for the Hebrew edition -->
<html lang="he" dir="rtl">
<!-- Existing head and translated body go here. -->
</html>
lang identifies the language for tools such as screen readers; it does not set the base direction. text-align: right moves text to the right edge but does not change how mixed-direction text is resolved or where logical layout starts. Keep <!doctype html> and the existing <head> when updating the root, then confirm that a paragraph without any alignment CSS starts on the right.
Set the document’s base direction in HTML. Don’t rely on a stylesheet to make an otherwise left-to-right document read correctly.
Replace physical spacing and positioning with logical CSS
Audit left, right, and four-value shorthands before adding RTL overrides. Ask what each declaration means: if a badge belongs at the trailing edge of a card, use the inline end; if a chart annotation belongs at a fixed coordinate, a physical edge may be correct.
/* Before: these declarations assume left-to-right reading. */
.card {
padding: 12px 16px 12px 24px;
border-left: 4px solid currentColor;
text-align: left;
}
.card__badge {
position: absolute;
top: 8px;
right: 12px;
}
/* After: replace the old declarations; don't append both versions. */
.card {
position: relative;
padding-block: 12px;
padding-inline: 24px 16px;
border-inline-start: 4px solid currentColor;
text-align: start;
}
.card__badge {
position: absolute;
inset-block-start: 8px;
inset-inline-end: 12px;
}
In a horizontal writing mode, the first value of padding-inline is on the right in RTL and on the left in LTR. Appending logical properties to the old rules can leave a physical border or inset active on the opposite edge. Verify both directions and reserve enough space for the badge’s longest translation; absolute positioning won’t push the heading out of its way.
Choose logical properties by layout intent
| Intent | Use | Watch for |
|---|---|---|
| Space before an inline sibling | margin-inline-start | Use gap instead if every flex or grid item needs the same spacing. |
| Leading divider | border-inline-start | Remove the old border-left rather than keeping both. |
| Trailing floating badge | inset-inline-end | Check the containing block, usually an ancestor with position: relative. |
| Text aligned to the reading edge | text-align: start | Use text-align: end for the opposite edge, not automatically for every numeric column. |
| Rounded leading corner at the top of a horizontal layout | border-start-start-radius | The first start is the block axis; the second is the inline axis. |
| Keep an overlay on the physical left | left | Transforms such as translateX() and gradient directions such as to left are physical too. |
Let Flexbox and Grid follow source order
In a horizontal RTL layout, a flex row already starts on the right, and grid column line 1 is at the right edge. Keep the source in the order you want people to read and tab through it; don’t reverse a row just because the page changed direction.
<nav class="site-nav" aria-label="ניווט ראשי">
<a href="./index.html">דף הבית</a>
<a href="./products.html">מוצרים</a>
<a href="./contact.html">יצירת קשר</a>
</nav>
<div class="page-layout">
<main><h1>מוצרים</h1></main>
<aside><h2>מידע נוסף</h2></aside>
</div>
The first navigation link is the first tab stop among these links in both directions. The main content comes before supplementary content in the DOM, so the same markup makes sense when the columns stack or CSS is disabled.
.site-nav {
display: flex;
flex-flow: row wrap;
gap: 0.5rem 1rem;
}
.page-layout {
display: grid;
grid-template-columns: minmax(0, 1fr);
gap: 1.5rem;
}
.page-layout > * {
min-inline-size: 0;
overflow-wrap: anywhere;
}
@media (min-width: 48rem) {
.page-layout {
grid-template-columns: minmax(0, 1fr) minmax(0, 16rem);
}
}
The main column appears on the right in RTL and on the left in LTR; at narrow widths it stays above the sidebar. The zero minimum lets tracks shrink, while overflow-wrap: anywhere gives otherwise unbreakable content, such as a long URL, somewhere to wrap. Resize with real translations and tab across the links to catch visual reordering introduced by old order or row-reverse rules.
Isolate mixed-direction values
Direction for a whole page doesn’t tell the browser where a product name, handle, or identifier begins and ends. Wrap each inserted value at its actual boundary so adjacent punctuation is resolved in the surrounding sentence rather than accidentally grouped with the value.
<p lang="ar" dir="rtl">
رمز الطلب: (<bdi dir="ltr">SKU-204/A</bdi>)؛
العميل: <bdi>Alex Cohen</bdi>.
</p>
<label for="support-email">البريد الإلكتروني</label>
<input id="support-email" name="email" type="email"
dir="ltr" autocomplete="email">
<label for="customer-note">ملاحظات</label>
<textarea id="customer-note" name="note" dir="auto"></textarea>
bdi isolates inline text; without an explicit direction, it determines direction from the content’s first strong directional character. Use dir="ltr" when the value is known to require it, and keep punctuation belonging to the surrounding sentence outside the isolation boundary. Test a name in each script, a value beginning with digits, and an email address with punctuation, including how each field behaves while typing.
Never reverse the characters in an identifier or phone number to fix its appearance. Keep the stored value intact and fix the display boundary in HTML.
Mirror directional icons, not entire components
<a class="next-link" href="./step-2.html">
التالي
<span class="next-link__arrow" aria-hidden="true">→</span>
</a>
<style>
.next-link {
display: inline-flex;
align-items: center;
gap: 0.5em;
}
.next-link__arrow {
display: inline-block;
}
.next-link:dir(rtl) .next-link__arrow {
transform: scaleX(-1);
}
</style>
The arrow indicates progression, so it points left when the link’s effective HTML direction is RTL. :dir(rtl) also works for inherited direction and won’t match a link inside a nested dir="ltr" region. Check that the link label still reads normally and that the flip doesn’t hit logos, artwork, or a conventional play icon.
Validate the localized pages before publishing
- Open the LTR, Arabic, and Hebrew files at narrow and wide widths. In developer tools, inspect the root for the right
langanddir, then check for clipped labels, overlapping badges, and horizontal overflow. - Test a nested
dir="ltr"example inside each RTL page. Its text, leading border, and directional icon should follow the local direction, not the page direction. - Tab through the interface and exercise any custom arrow-key behavior. CSS mirroring doesn’t update JavaScript handlers for
ArrowLeftandArrowRight; check them against the widget’s interaction pattern. - Open the exported files, not just the development preview. Verify character encoding with
<meta charset="utf-8">and check the font actually used for Arabic and Hebrew, including any diacritics in the translated copy.
Use HTML Translate to compare localized HTML, then check direction, wrapping, and keyboard order in the browser itself.
Common RTL conversion mistakes
- Text is right-aligned but punctuation looks misplaced. Replace the alignment-only fix with root
dir="rtl"and isolate embedded values with<bdi>. - A divider appears on both sides. Old
border-leftand newborder-inline-starttarget different edges in RTL. Remove the physical declaration. - Navigation starts on the left in RTL. Remove a redundant
flex-direction: row-reverseand check that DOM order matches keyboard order. - A long code pushes the mobile page wider. Allow the affected flex or grid item to shrink with
min-inline-size: 0; addoverflow-wrap: anywherewhere wrapping is acceptable. - An arrow flips inside an English panel. An ancestor selector such as
[dir="rtl"] .arrowstill matches within nested LTR content. Match the component’s own effective direction with:dir(rtl). - Arabic letters look spaced apart or clipped. Remove decorative
letter-spacingfrom Arabic copy, test a font with Arabic coverage, and replace fixed text heights with room for natural line boxes.
RTL and dir Attribute Questions
Can Arabic and Hebrew share the same stylesheet?
Yes. Use logical properties for direction-aware layout and select language-specific typography with :lang(ar) and :lang(he) when the fonts or line spacing need to differ.
Should all numbers be forced left to right?
No. Leave ordinary localized numbers in their sentence context. Isolate machine-formatted values such as a SKU or an international phone number with <bdi dir="ltr"> when their punctuation or prefix must stay in a particular order.
Does an English quotation in an Arabic page need its own direction?
For a standalone quotation, set lang="en" dir="ltr" on the quotation container. For an isolated inline name of unknown direction, use <bdi> instead of switching the surrounding paragraph.
Should every RTL page use right-aligned text?
No. Use text-align: start for normal reading text. Centered headings can stay centered, and data columns can follow their own alignment conventions so values remain easy to compare.
Can the same CSS be pasted into HTML email?
Don’t assume browser support carries over to email clients. Keep semantic dir on the relevant email container, test nested tables and mixed-direction values in target clients, and generate locale-specific physical spacing where logical CSS isn’t supported.