HTML tags | bdi

The <bdi> element (Bi-Directional Isolation) tells the browser’s bidirectional algorithm to treat the text it contains in isolation from its surrounding text. It’s particularly useful when a website dynamically inserts some text and doesn’t know …


This content originally appeared on DEV Community and was authored by Carlos Espada

The <bdi> element (Bi-Directional Isolation) tells the browser's bidirectional algorithm to treat the text it contains in isolation from its surrounding text. It's particularly useful when a website dynamically inserts some text and doesn't know the directionality of the text being inserted.

Bidirectional text is text that may contain both sequences of characters that are arranged left-to-right (LTR) and sequences of characters that are arranged right-to-left (RTL), such as an Arabic quotation embedded in an English string. Browsers implement the Unicode Bidirectional Algorithm to handle this. In this algorithm, characters are given an implicit directionality: for example, Latin characters are treated as LTR while Arabic characters are treated as RTL. Some other characters (such as spaces and some punctuation) are treated as neutral and are assigned directionality based on that of their surrounding characters.

Usually, the bidirectional algorithm will do the right thing without the author having to provide any special markup but, occasionally, the algorithm needs help. That's where <bdi> comes in.

The <bdi> element is used to wrap a span of text and instructs the bidirectional algorithm to treat this text in isolation from its surroundings. This works in two ways:

  • The directionality of text embedded in <bdi> does not influence the directionality of the surrounding text.
  • The directionality of text embedded in <bdi> is not influenced by the directionality of the surrounding text. For example, consider some text like:
EMBEDDED-TEXT - 1st place

If EMBEDDED-TEXT is LTR, this works fine. But if EMBEDDED-TEXT is RTL, then - 1 will be treated as RTL text (because it consists of neutral and weak characters). The result will be garbled:

1 - EMBEDDED-TEXTst place

If you know the directionality of EMBEDDED-TEXT in advance, you can fix this problem by wrapping EMBEDDED-TEXT in a <span> with the dir attribute set to the known directionality. But if you don't know the directionality - for example, because EMBEDDED-TEXT is being read from a database or entered by the user - you should use <bdi> to prevent the directionality of EMBEDDED-TEXT from affecting its surroundings.

Though the same visual effect can be achieved using the CSS rule unicode-bidi: isolate on a <span> or another text-formatting element, HTML authors should not use this approach because it is not semantic and browsers are allowed to ignore CSS styling.

Embedding the characters in <span dir="auto"> has the same effect as using <bdi>, but its semantics are less clear.

Like all other HTML elements, <bdi> element supports the global attributes, except that the dir attribute behaves differently than normal: it defaults to auto, meaning its value is never inherited from the parent element. This means that unless you specify a value of either rtl or ltr for dir, the user agent will determine the correct directionality to use based on the contents of the <bdi>.

  • Type: inline
  • Self-closing: No
  • Semantic value: No

Definition | Example | Support


This content originally appeared on DEV Community and was authored by Carlos Espada


Print Share Comment Cite Upload Translate Updates
APA

Carlos Espada | Sciencx (2021-11-18T07:26:53+00:00) HTML tags | bdi. Retrieved from https://www.scien.cx/2021/11/18/html-tags-bdi/

MLA
" » HTML tags | bdi." Carlos Espada | Sciencx - Thursday November 18, 2021, https://www.scien.cx/2021/11/18/html-tags-bdi/
HARVARD
Carlos Espada | Sciencx Thursday November 18, 2021 » HTML tags | bdi., viewed ,<https://www.scien.cx/2021/11/18/html-tags-bdi/>
VANCOUVER
Carlos Espada | Sciencx - » HTML tags | bdi. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/18/html-tags-bdi/
CHICAGO
" » HTML tags | bdi." Carlos Espada | Sciencx - Accessed . https://www.scien.cx/2021/11/18/html-tags-bdi/
IEEE
" » HTML tags | bdi." Carlos Espada | Sciencx [Online]. Available: https://www.scien.cx/2021/11/18/html-tags-bdi/. [Accessed: ]
rf:citation
» HTML tags | bdi | Carlos Espada | Sciencx | https://www.scien.cx/2021/11/18/html-tags-bdi/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.