Bidirectional text and Persian UI
Definition and purpose
Bidirectional text can combine Persian, Latin identifiers, punctuation and numbers in one line. The Unicode bidi algorithm uses character direction and the base direction to arrange the visible text.
Isolate a product identifier
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; } #a, #b { background: lightblue; } #p { text-align: start; } #a { unicode-bidi: isolate; } </style> </head> <body> <p id="p" lang="fa" dir="rtl"> شماره سفارش: <bdi id="a" dir="ltr">AB-123</bdi> ، آماده است. </p> </body></html>The paragraph has RTL base direction, and the embedded code remains isolated LTR. These checks inspect styles, not all glyph ordering.
Syntax and mental model
Set lang="fa" dir="rtl" on Persian content. For an embedded identifier of known LTR direction use <bdi dir="ltr">AB-123</bdi>; dir="auto" can infer direction from the first strong character for unknown text. CSS unicode-bidi: isolate separates directional runs; text-align: start aligns according to base direction. Do not force letter spacing on connected Persian glyphs.
Logical text alignment
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; } #a, #b { background: lightblue; } #p { text-align: start; } </style> </head> <body> <p id="p" lang="fa" dir="rtl"> شماره سفارش: <bdi id="a" dir="ltr">AB-123</bdi> ، آماده است. </p> </body></html>Start alignment follows the right edge for this Persian paragraph.
Common mistakes
Never reverse the source string to simulate RTL or use bidi-override for ordinary mixed text. Preserve source and reading order and isolate foreign fragments.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Logical borders and insets. Next lesson: International component project.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Bidirectional text and Persian UI — knowledge check
Five questions on bidirectional text and persian ui.
Which HTML attribute sets base direction?
Which HTML element isolates embedded text direction?
What is dir="auto" useful for?
Should source strings be reversed to display Persian?
Which alignment follows base direction?