Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

HTML term or value: dir

The `dir` attribute sets semantic base direction for text so the bidirectional algorithm can order characters, numbers, and punctuation correctly.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<p dir="auto">…</p>

dir is enumerated: ltr selects left-to-right, rtl selects right-to-left, and auto derives direction from the first strong character. Direction is inherited unless an element-specific rule establishes a boundary.

Use auto for content whose language or direction is unknown, but leading numbers and neutral punctuation do not decide the result. When an unknown fragment appears inside a surrounding sentence, the bdi element isolates its bidirectional influence from adjacent text.

Do not use alignment as a substitute for writing direction. The semantic HTML attribute affects text processing, controls, and copying, whereas the CSS direction property is presentational and should be reserved for styling exceptions.

The example uses dir="auto" and starts with a strong Arabic character; getComputedStyle() reports rtl. The result demonstrates the directionality algorithm rather than merely detecting right-to-left text.

Examples

<!doctype html><meta charset="utf-8"><p id="x" dir="auto">مرحبا HTML</p><output id="result"></output><script>result.textContent=`attribute=${x.dir}; computed=${getComputedStyle(x).direction}`;</script>

Result

attribute=auto; computed=rtl

The executed example verifies automatic base-direction resolution for content beginning with a strong RTL character.

Sources