<bdi> Bidirectional isolate

Isolates text of unknown direction, such as user names.

<bdi> isolates a piece of text so its writing direction cannot disturb the text around it. That helps with content you do not control, like user names that might be in Arabic or Hebrew.

Without it, right-to-left text beside numbers or punctuation can come out in a confusing order. Pages that never mix left-to-right and right-to-left text do not need it.

The bdi element definition (opens in a new tab) targets user-generated text, such as usernames, whose direction is unknown. Compare it with the bdo element.

Syntax

<bdi>text of unknown direction</bdi>

When to use it

Wrap <bdi> around text whose direction you cannot predict, usually because users or other systems supply it: names on a leaderboard, comment authors, product titles from a marketplace feed, search results, or chat messages. Each piece then gets its direction detected on its own and cannot reorder the numbers or punctuation next to it.

A span with dir="auto" gives the same isolation in current browsers, because any element with a dir attribute is isolated. bdi is handy because it isolates without needing an attribute. For text whose order you want to force, there is bdo; for a whole paragraph in a right-to-left language, set dir and lang on the paragraph.

Wrap only the unknown part. If you include the label or the number in bdi, those can be pulled into the wrong direction too. And bdi does not set the language: if you know it, add a lang attribute as well.

Live example

Edit the code and watch the preview.

<ul>
  <li>User <bdi>jo_smith</bdi>: 12 posts</li>
  <li>User <bdi>&#x625;&#x64A;&#x627;&#x646;</bdi>: 7 posts</li>
</ul>

More examples

Leaderboard with and without bdi

The same Hebrew name appears in two paragraphs. Without bdi, the score can jump to the wrong side of the name; with bdi, the name is isolated and the score stays where you wrote it.

<h2>Without bdi</h2>
<p>&#x5D3;&#x5E0;&#x5D4;: 3 points</p>
<h2>With bdi</h2>
<p><bdi>&#x5D3;&#x5E0;&#x5D4;</bdi>: 3 points</p>

Search results from mixed languages

Search results can come from any language. Each title goes in bdi, so an Arabic title does not disturb the result number after it. A span with dir="auto" does the same job.

<ol>
  <li><bdi>Easy pancakes</bdi> (12 reviews)</li>
  <li><bdi>&#x641;&#x637;&#x627;&#x626;&#x631;</bdi> (8 reviews)</li>
  <li><span dir="auto">&#x641;&#x637;&#x627;&#x626;&#x631;</span> (5 reviews)</li>
</ol>

Tip: On multilingual pages, wrap user-generated names in bdi.

Browser support

bdi is supported in current versions of Chrome, Firefox and Safari, and in Edge since version 79. Internet Explorer and the old EdgeHTML-based Edge did not isolate it. See the bdi element support table (opens in a new tab) for details.

Accessibility

When the text direction is right, screen readers read mixed-language content in the right order. If you know the language, add a lang attribute to the bdi as well. The rules underneath all this are in the Unicode Bidirectional Algorithm (opens in a new tab).

Common mistakes

Wrong

<li>User <span>{name}</span>: 3 posts</li>

Right

<li>User <bdi>{name}</bdi>: 3 posts</li>

A right-to-left name that is not isolated can reorder the number and punctuation after it.

Frequently asked questions

When should I use the bdi tag?

Around text whose direction you cannot know in advance, which mostly means user-generated content: usernames, product names, search terms. Without <bdi>, a right-to-left name such as an Arabic username can rearrange neighbouring numbers and punctuation, and a score may end up next to the wrong name. Why mixed scripts can appear reordered is explained in Wikipedia's article on bidirectional text (opens in a new tab).

What is the difference between bdi and bdo?

<bdi> isolates text and lets the browser detect its direction, leaving the text around it alone. <bdo> overrides the bidirectional algorithm and forces the direction set by its required dir attribute. Reach for bdi with unknown text, and for bdo only when you deliberately need to force character order.

Related text elements

<p>One paragraph of text.<br>Breaks the line inside text, as in a postal address or a poem.<hr>A change of topic between paragraphs, drawn as a horizontal line.<pre>Shows text exactly as typed, spaces and line breaks included.<blockquote>A longer quotation from another source, set apart as its own block.<div>A generic block box with no meaning, for grouping and styling.<span>A generic inline wrapper with no meaning, for styling part of a line of text.<strong>Text that is important, serious or urgent. Shown in bold by default.<em>Stress emphasis that changes what a sentence means. Italic by default.<b>Makes text stand out without adding importance, as with keywords. Bold by default.<i>Text in another voice: foreign words, terms, thoughts, ship names. Italic.<u>Text with an unspoken annotation, such as a flagged spelling error.<s>Content that is no longer accurate or relevant, drawn crossed out.<mark>Highlighted text, like a marker pen, showing relevance in the current context.<small>Side comments and fine print, like copyright lines or legal notes.<sub>Subscript text, set below the line, as in H2O.<sup>Superscript text, set above the line, as in exponents or 1st.<code>A fragment of computer code, set in a monospace font.<kbd>Keys or text the user should press or type.<samp>Sample output from a program or a computer system.<var>A variable in math or in code. Italic by default.<q>A short quotation inside a sentence. The browser adds the quote marks.<cite>The title of a creative work: a book, film, song, game or article.<abbr>An abbreviation or acronym, with an optional expansion in title.<dfn>Marks the term that the surrounding sentence or paragraph defines.<time>Marks a date, time or duration and gives it a machine-readable value.<data>Ties visible content to a machine-readable value.<del>Text that was removed from the document. Struck through by default.<ins>Text that was added to the document. Underlined by default.<wbr>A spot where the browser may break a long word if it has to.<bdo>Forces a text direction and overrides the normal bidi algorithm.<ruby>Small annotations above or beside text, mostly for East Asian pronunciation.<rt>The small reading or note in a ruby annotation.<rp>Fallback brackets around ruby text, shown only where ruby is unsupported.