Logical CSS properties adapt to writing direction and flow. They are generally more suitable for multilingual layouts than hard-coded left and right properties.
<style>
.card {
padding-inline: 1rem;
margin-inline-start: 1rem;
border-inline-start: 4px solid blue;
}
</style>
<article class="card" dir="rtl">
<p>This layout follows the text direction.</p>
</article>Logical properties such as padding-inline and margin-inline-start adapt to the writing mode and direction.
9. Avoid Directional Assumptions
- Declare the main language on the html element.
- Mark sections that switch to a different language.
- Use dir="auto" for suitable user-generated text with unknown direction.
- Use bdi for mixed-direction inline content when isolation is helpful.
- Prefer CSS logical properties for adaptable layouts.
- Test mixed-language content, punctuation, numbers and identifiers.
Common Mistakes
- Leaving the document language undeclared.
- Using the wrong language code.
- Assuming every language reads from left to right.
- Using bdo when direction isolation with bdi would be more appropriate.
- Hard-coding left and right spacing for every language.
Practice Exercises
- Create an HTML page with English and Odia content.
- Set the language on a paragraph that switches to Hindi.
- Create a right-to-left paragraph and include a mixed-direction identifier.
- Use CSS logical properties to create a direction-aware card.
Revision Quiz
- What does lang communicate?
- Which language code is commonly used for Odia?
- What does dir="rtl" mean?
- Which element isolates inline text with an independent direction?
- What does bdo do?
- Why are CSS logical properties useful?
Answer Key and Explanations
- The language of the content — It supports pronunciation, language processing and accessibility.
- or — It is the standard language subtag for Odia.
- Right to left — It establishes a right-to-left base direction.
- bdi — It isolates inline content with an independent direction.
- Overrides text direction — It applies an explicit directional override.
- They adapt to writing direction and flow — This reduces hard-coded left/right assumptions.