HTML <bdi> Bidirectional Isolation tag


The <bdi> tag in HTML stands for "Bidirectional Isolation." It is used to isolate a section of text that may be affected by bidirectional text formatting, such as mixing languages with different text directions (e.g., left-to-right and right-to-left).

Purpose:

The <bdi> tag is designed to handle text that may be affected by the surrounding text's directionality, ensuring that it is displayed correctly regardless of the surrounding content's direction. This is particularly useful for dynamically inserted content or text in multiple languages that might have different text directions.

Syntax:

<bdi>Text to be isolated</bdi>

How It Works:

  • Bidirectional Text: Text direction can be either left-to-right (LTR) or right-to-left (RTL). For instance, English is usually LTR, while Arabic and Hebrew are RTL.
  • Isolation: When text inside the <bdi> tag is isolated, it maintains its intended directionality, even if it is surrounded by text with the opposite direction. This prevents the bidirectional algorithm from affecting it.

Example:

<p>In this sentence, we have mixed directions. For example, <bdi>1234</bdi> and Arabic text.</p>

In this example:

  • The <bdi> tag ensures that the number "1234" is displayed correctly, regardless of the direction of the surrounding Arabic text.

Use Cases:

  1. Dynamic Content: When displaying user-generated content or dynamic content where the text direction is not known in advance.
  2. Multilingual Pages: On pages with mixed language content where different languages have different text directions.
  3. Embedded Text: When embedding text from external sources that may have different directionality.

Comparison with <bdo>:

  • <bdi> (Bidirectional Isolation): Isolates text to prevent it from being affected by the surrounding text's direction.
  • <bdo> (Bidirectional Override): Forces the text inside it to be displayed in a specific direction, overriding the default bidirectional algorithm.

Example with <bdo>:

<p>Some text with <bdo dir="rtl">1234</bdo> in RTL direction.</p>

Here, the <bdo> tag forces the text "1234" to be displayed in RTL direction.

Accessibility and SEO:

  • Accessibility: The <bdi> tag helps ensure that text is displayed correctly, which is important for screen readers and users who rely on proper text rendering for understanding content.
  • SEO: Properly rendering bidirectional text does not have a direct impact on SEO, but it contributes to a better user experience, which can indirectly affect user engagement and SEO metrics.