Masareef, my expense tracker, prints a bank-style statement from HTML. The Arabic version was wrong in every row: a debit of −EGP 9,000.00 read EGP 9,000.00−, and the date 1 Jul 2026 read Jul 2026 1.
The fix is two properties on the element that holds the figure:
[dir="rtl"] .figure {
direction: ltr;
unicode-bidi: isolate;
}You do not always need both. Which one does the work depends on the element, and that is where the usual advice goes wrong.
Why it happens#
A browser orders mixed text with the Unicode bidirectional algorithm. Latin letters are strongly left to right. A minus sign is not, and digits are weak. In a right-to-left paragraph the Latin run keeps its own order, and the minus in front of it takes the paragraph's direction, so it goes to the far side. In 1 Jul 2026 the leading 1 has no Latin letter before it, so it is placed as a separate piece.
Nothing is wrong with the string. Copy it out of the page and it is correct.
What each fix does#
I put the same strings in a dir="rtl" page and measured where Chrome 158 drew each character. This is the visual order, left to right:
| Element | CSS | −EGP 9,000.00 |
1 Jul 2026 |
|---|---|---|---|
td, div |
none | EGP 9,000.00− |
Jul 2026 1 |
td, div |
unicode-bidi: isolate |
EGP 9,000.00− |
Jul 2026 1 |
td, div |
direction: ltr |
correct | correct |
span |
unicode-bidi: isolate |
EGP 9,000.00− |
Jul 2026 1 |
span |
direction: ltr |
EGP 9,000.00− |
Jul 2026 1 |
span |
both | correct | correct |
| any of them | unicode-bidi: plaintext |
correct | correct |
bdi |
none | correct | correct |
Three things come out of it:
isolatealone never fixed it. Isolation stops the figure from affecting the text around it. Inside, the figure still has the direction it inherited, which is right to left.direction: ltralone is enough on a block or a table cell, and does nothing on an inline span. An inline element needsunicode-bidias well before its direction counts.- A span that is a flex item behaves like a block. It stops being inline, so
direction: ltralone worked there too. The same rule can work in one part of a page and fail in another.
Setting both properties is right in every row, which is why I use the pair.
Do not stop at bdi#
<bdi> and unicode-bidi: plaintext look like the simpler answer. Both pick a direction from the content, by finding the first strong character. With EGP that character is Latin, so the result is left to right.
Swap the currency for an Arabic one, such as the Egyptian pound's Arabic abbreviation after the number. Now the first strong character is Arabic. In the same test the bdi and the plaintext versions both drew the minus on the right of the number again. The pair with an explicit direction: ltr kept it on the left.
So bdi is right for a user's name, where you don't know the language. It is the wrong tool for money, where you know the order you want.
A plain −9,000.00 with no currency was wrong with no fix, like the others. It was correct in bdi, with plaintext and with the pair. +12.5% was the worst of them in a bare span, where it came out as %12.5+.
Put the alignment back#
direction also decides what start and end mean. A cell with text-align: end in a right-to-left page was aligned left. Give it direction: ltr and its text moves to the right. Restate the alignment for what you changed:
[dir="rtl"] td.num { text-align: left; }
[dir="rtl"] .when { text-align: right; }Do not put it on a layout#
One of my numeric cells held a flex row, with a small progress bar and a percentage. direction: ltr on the cell flipped that row as well, and the bar ended up on the wrong side. I gave the row its direction back and kept the fix on the text inside:
[dir="rtl"] .bud { direction: rtl; }Put the fix on the smallest element that holds the figure and you will not need that rule.
Test it by looking at it#
This code had 36 tests and an rtl flag passed through every function. No test drew a page, so none of them could fail.
What found it was one fixture written to an HTML file in both languages, then a headless screenshot of each:
chrome --headless --screenshot=ar.png --window-size=900,1400 file:///path/to/ar.htmlRead the result, and check that the file was rewritten first. I spent two rounds studying a stale screenshot because the script that generated it had failed and I had not read its output.
To assert it without a person, measure the characters. A Range around one character gives you its box:
function visualOrder(el) {
const text = el.firstChild;
return [...text.data]
.map((char, i) => {
const range = document.createRange();
range.setStart(text, i);
range.setEnd(text, i + 1);
return { char, x: range.getBoundingClientRect().left };
})
.sort((a, b) => a.x - b.x)
.map((c) => c.char)
.join('');
}It needs a real browser, and it works on an element that holds a single line of plain text without emoji. Every row in the table above came from a version of this function.
What I did not test#
I measured Chrome 158 only. The rules come from the CSS Writing Modes and Unicode specifications, so other browsers should agree, but I have not run them. A PDF made by something other than a browser may ignore these properties.
Related: Arabic paragraphs in Markdown, and the same kind of bug in Flutter, where 175 showed as 571.