CSS - Unicode-bidi Property: Mastering Bidirectional Text
Hello there, future web developers! Today, we're going to embark on an exciting journey into the world of CSS and explore a property that might sound a bit intimidating at first: unicode-bidi
. But don't worry, by the end of this tutorial, you'll be handling bidirectional text like a pro!
What is Unicode-bidi?
Before we dive into the nitty-gritty, let's understand what unicode-bidi
is all about. Imagine you're writing a multilingual website that includes both English (left-to-right) and Arabic (right-to-left) text. How do you ensure that each language displays correctly? That's where unicode-bidi
comes to the rescue!
The unicode-bidi
property is your trusty sidekick when dealing with bidirectional text in CSS. It helps you control how bidirectional text is handled in a document, ensuring that your content is displayed correctly regardless of the writing direction.
Possible Values
Let's take a look at the possible values for the unicode-bidi
property:
Value | Description |
---|---|
normal | The element doesn't open an additional level of embedding |
embed | Opens an additional level of embedding |
bidi-override | Creates an override for the bidirectional algorithm |
isolate | The element is isolated from its siblings |
isolate-override | Combines the effects of both 'isolate' and 'bidi-override' |
plaintext | The element is isolated and the bidirectional formatting is reset |
Don't worry if these seem confusing now. We'll explore each of them in detail with examples!
Applies to
The unicode-bidi
property can be applied to all elements. However, it's most commonly used with inline elements or elements that have been set to display: inline
.
DOM Syntax
To use unicode-bidi
in your JavaScript, you can use the following syntax:
object.style.unicodeBidi = "value"
Now, let's dive into each value and see how they work in action!
CSS unicode-bidi - normal Value
The normal
value is the default setting. It doesn't open an additional level of embedding with respect to the bidirectional algorithm.
<p style="unicode-bidi: normal;">
This is English text. هذا نص عربي.
</p>
In this example, the text will be displayed as:
This is English text. هذا نص عربي.
The English text is left-to-right, while the Arabic text is right-to-left, following their natural directions.
CSS unicode-bidi - embed Value
The embed
value opens an additional level of embedding with respect to the bidirectional algorithm.
<p style="unicode-bidi: embed; direction: rtl;">
This is English text. هذا نص عربي.
</p>
This will display as:
.هذا نص عربي This is English text.
Here, the entire paragraph is treated as right-to-left, but the English text maintains its left-to-right direction within the overall RTL context.
CSS unicode-bidi - bidi-override Value
The bidi-override
value creates an override for the bidirectional algorithm. It forces the directionality of the text to match the direction
property.
<p style="unicode-bidi: bidi-override; direction: rtl;">
This is English text. هذا نص عربي.
</p>
This will display as:
.يبرع صن اذه .txet hsilgnE si sihT
In this case, all the text is treated strictly as right-to-left, even reversing the order of characters in the English text.
CSS unicode-bidi - isolate Value
The isolate
value isolates the element from its siblings in terms of bidirectional formatting.
<p>This is <span style="unicode-bidi: isolate; direction: rtl;">هذا نص عربي</span> some English text.</p>
This will display as:
This is هذا نص عربي some English text.
The Arabic text is isolated and displayed right-to-left, while the surrounding English text remains unaffected.
CSS unicode-bidi - isolate-override Value
The isolate-override
value combines the effects of both isolate
and bidi-override
.
<p>This is <span style="unicode-bidi: isolate-override; direction: rtl;">English text</span> in a sentence.</p>
This will display as:
This is txet hsilgnE in a sentence.
The text within the span is isolated and strictly reversed.
CSS unicode-bidi - plaintext Value
The plaintext
value isolates the element and resets the bidirectional formatting to its implicit direction.
<p style="direction: rtl;">
This is <span style="unicode-bidi: plaintext;">some English text</span> بعض النص العربي
</p>
This will display as:
بعض النص العربي some English text This is
The English text within the span is displayed in its natural left-to-right direction, regardless of the parent's right-to-left direction.
CSS unicode-bidi - Related Properties
When working with unicode-bidi
, you'll often find yourself using these related properties:
Property | Description |
---|---|
direction | Sets the text direction |
writing-mode | Defines whether lines of text are laid out horizontally or vertically |
Remember, the unicode-bidi
property works hand in hand with the direction
property to control the flow of bidirectional text.
Conclusion
Congratulations! You've just taken a deep dive into the world of unicode-bidi
. This property might seem complex at first, but it's an invaluable tool when working with multilingual websites. Remember, practice makes perfect, so don't hesitate to experiment with different values and see how they affect your text.
As you continue your journey in web development, you'll encounter many situations where understanding bidirectional text is crucial. Whether you're creating a website for a global audience or working on a localization project, the skills you've learned today will serve you well.
Keep coding, keep learning, and most importantly, have fun! The world of web development is full of exciting challenges, and mastering properties like unicode-bidi
is just the beginning of your adventure. Happy coding!
Credits: Image by storyset