CSS direction Property
The direction property specifies the base text direction of an element, either left-to-right or right-to-left.
This property is particularly useful for languages such as Arabic and Hebrew, which are generally written from right to left. It also influences the ordering of bidirectional text and certain layout behaviors.
For HTML documents, the dir attribute is generally preferred for defining text direction because it preserves the information independently of CSS.
direction Syntax
The direction property accepts two directional keywords.
selector {
direction: ltr | rtl;
}
Left-to-Right Direction
.text {
direction: ltr;
}
Right-to-Left Direction
.text {
direction: rtl;
}
Inherited Direction
.text {
direction: inherit;
}
CSS-Wide Keywords
.text {
direction: initial;
}
.text {
direction: unset;
}
.text {
direction: revert;
}
.text {
direction: revert-layer;
}
The initial value is ltr, but the actual direction of an element may come from its HTML dir attribute, an ancestor, or a CSS declaration.
Property Values
| Value | Description |
|---|---|
ltr |
Establishes left-to-right base direction. Commonly used for English and many other languages. |
rtl |
Establishes right-to-left base direction. Commonly used for Arabic and Hebrew. |
inherit |
Uses the computed direction of the parent element. |
initial |
Restores the initial value, ltr. |
unset |
Uses the inherited direction because the property is inherited. |
revert |
Reverts to the value established by an earlier cascade origin. |
revert-layer |
Reverts to the value established by an earlier cascade layer. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | ltr |
| Inherited | Yes |
| Applies to | All elements |
| Computed value | As specified |
| Animation type | Not animatable |
| Percentages | Not applicable |
Unlike most inherited properties, direction is not reset by the all shorthand property.
How It Works
The direction property establishes the base direction used by the Unicode bidirectional text algorithm and influences certain layout decisions.
Step 1: Select an Element
<p class="rtl-text">مرحبا بالعالم</p>
Step 2: Set the Direction
.rtl-text {
direction: rtl;
}
Step 3: Render the Text
The browser uses right-to-left base direction when arranging the paragraph's text.
What Changes?
Base direction affects bidirectional ordering, the starting side of text, and certain layout behaviors. It does not reverse the character order of every word.
Important Distinction
The direction property is not a general-purpose tool for reversing content. It establishes writing direction, while the browser applies the Unicode bidirectional algorithm to individual text runs.
Left-to-Right Direction
The ltr value establishes left-to-right base direction.
Basic Example
.english-text {
direction: ltr;
}
HTML Example
<p class="english-text">Learn HTML and CSS.</p>
Paragraph Direction
.content {
direction: ltr;
text-align: start;
}
With horizontal writing and left-to-right direction, text-align: start aligns text to the left.
Common Uses
Left-to-right direction is used for English and many European languages, as well as other writing systems that progress from left to right.
Important Consideration
Left-to-right base direction does not prevent right-to-left text from appearing within a paragraph. The Unicode bidirectional algorithm handles mixed-direction content.
Right-to-Left Direction
The rtl value establishes right-to-left base direction.
Basic Example
.arabic-text {
direction: rtl;
}
HTML Example
<p class="arabic-text">مرحبا بالعالم</p>
Right-to-Left Paragraph
.rtl-content {
direction: rtl;
text-align: start;
}
With horizontal writing and right-to-left direction, text-align: start aligns text to the right.
Nested Right-to-Left Content
<div class="rtl-section">
<h2>مرحبا</h2>
<p>هذا مثال على اتجاه النص.</p>
</div>
.rtl-section {
direction: rtl;
}
Because direction is inherited, descendants normally receive the right-to-left base direction unless another value is specified.
Important Consideration
Use right-to-left direction for content that requires it. Applying rtl indiscriminately to left-to-right text can produce unexpected punctuation and mixed-text ordering.
Bidirectional Text
Bidirectional text contains both left-to-right and right-to-left writing within the same passage.
For example, an Arabic paragraph may include English product names, numbers, or web addresses.
Mixed-Direction Paragraph
<p class="mixed-text">مرحبا بك في HTML و CSS</p>
.mixed-text {
direction: rtl;
}
How Text Is Arranged
The browser uses the Unicode bidirectional algorithm to determine how directional runs are displayed. Latin letters generally retain their left-to-right ordering within right-to-left paragraphs.
Numbers and Punctuation
Numbers and punctuation may be positioned differently depending on the surrounding characters and the paragraph's base direction.
Isolating Embedded Content
<p dir="rtl">
اسم الملف:
<bdi>report-2026.html</bdi>
</p>
The HTML <bdi> element isolates text whose direction may differ from the surrounding content.
Practical Consideration
For mixed-direction content, semantic HTML direction attributes and isolation elements are generally more reliable than trying to correct the display using CSS alone.
direction and unicode-bidi
The direction and unicode-bidi properties work together to influence bidirectional text handling.
direction
.rtl-text {
direction: rtl;
}
Establishes the base direction of the element.
unicode-bidi
.isolated-text {
unicode-bidi: isolate;
}
Controls how the element's text participates in bidirectional formatting.
Combined Example
.rtl-inline {
direction: rtl;
unicode-bidi: isolate;
}
For an inline element, unicode-bidi: isolate establishes an isolated bidirectional context in which the specified direction can be used.
Bidirectional Override
.forced-rtl {
direction: rtl;
unicode-bidi: bidi-override;
}
The bidi-override value forces directional ordering rather than allowing the normal bidirectional algorithm to determine it.
Important Consideration
Use unicode-bidi: bidi-override sparingly. It can produce text that is difficult to read, particularly when several writing systems appear together.
direction and HTML dir
HTML provides the dir attribute for declaring the direction of text.
HTML Left-to-Right
<p dir="ltr">Learn HTML and CSS.</p>
HTML Right-to-Left
<p dir="rtl">مرحبا بالعالم</p>
Automatic Direction
<p dir="auto">مرحبا بالعالم</p>
The HTML dir="auto" value allows the browser to determine direction from the content. The CSS direction property does not have an equivalent auto keyword.
CSS Direction
.rtl-text {
direction: rtl;
}
Comparison
| Feature | HTML dir | CSS direction |
|---|---|---|
| Purpose | Declares text direction in markup | Sets base direction through styling |
| Values | ltr, rtl, auto |
ltr, rtl |
| Independent of CSS | Yes | No |
| Recommended for document language direction | Yes | Generally no |
Recommended Approach
Use the HTML dir attribute to identify the direction of document content. Use CSS direction when a styling or layout requirement specifically calls for it.
direction and text-align
The direction property establishes base text direction, while text-align controls the alignment of inline content within a block.
Left-to-Right with Start Alignment
.ltr-paragraph {
direction: ltr;
text-align: start;
}
In horizontal writing, the text aligns to the left.
Right-to-Left with Start Alignment
.rtl-paragraph {
direction: rtl;
text-align: start;
}
In horizontal writing, the text aligns to the right.
Right-to-Left with Left Alignment
.rtl-left-aligned {
direction: rtl;
text-align: left;
}
The text uses right-to-left base direction but is aligned to the physical left side of its container.
Comparison
| Property | Controls |
|---|---|
direction |
Base text direction and related bidirectional behavior |
text-align |
Alignment of inline content within a block |
Recommended Approach
Use text-align: start and text-align: end when alignment should adapt automatically to the writing direction.
direction and writing-mode
The direction and writing-mode properties control different aspects of text layout.
Horizontal Writing
.horizontal-text {
writing-mode: horizontal-tb;
direction: ltr;
}
Horizontal Right-to-Left Writing
.horizontal-rtl {
writing-mode: horizontal-tb;
direction: rtl;
}
Vertical Writing
.vertical-text {
writing-mode: vertical-rl;
direction: ltr;
}
Vertical Writing with RTL Direction
.vertical-rtl {
writing-mode: vertical-rl;
direction: rtl;
}
Comparison
| Property | Purpose |
|---|---|
direction |
Controls inline base direction and bidirectional ordering. |
writing-mode |
Controls whether text lines are arranged horizontally or vertically and how lines progress. |
In vertical writing modes, direction also affects whether inline progression is from top to bottom or bottom to top.
Important Consideration
Changing direction alone does not create vertical writing. Use writing-mode for vertical text layouts.
Direction and Layout
The direction property can influence certain layout behaviors beyond text ordering.
Inline Content
The base direction affects how inline content is arranged and how bidirectional runs are displayed.
Tables
.rtl-table {
direction: rtl;
}
Right-to-left direction can affect the visual ordering of table columns.
Grid Layout
.rtl-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
direction: rtl;
}
In a horizontal writing mode, right-to-left direction changes the inline-start side used by grid placement.
Flexbox Layout
.rtl-flex {
display: flex;
flex-direction: row;
direction: rtl;
}
With flex-direction: row and horizontal writing, flex items begin at the right side when the direction is rtl.
Scrollbar Placement
Direction can influence scrollbar placement in some browser and platform combinations, but the exact behavior is not consistent everywhere.
Important Consideration
Do not use direction solely to reverse visual layout when the underlying content is left-to-right. Choose the appropriate layout properties instead.
Logical Properties
CSS logical properties adapt to writing direction and writing mode, making layouts easier to support in multiple languages.
Physical Margin
.element {
margin-left: 20px;
}
The margin remains on the physical left side regardless of direction.
Logical Margin
.element {
margin-inline-start: 20px;
}
In horizontal writing, this margin appears on the left for ltr and on the right for rtl.
Logical Padding
.element {
padding-inline-start: 15px;
}
Logical Border
.element {
border-inline-start: 3px solid #336699;
}
Logical Positioning
.positioned-element {
position: absolute;
inset-inline-start: 10px;
}
Direction-Aware Example
.notice {
padding-inline-start: 20px;
border-inline-start: 4px solid #336699;
text-align: start;
}
This styling adapts to left-to-right and right-to-left horizontal writing without requiring separate left and right declarations.
Recommended Approach
Prefer logical properties when building layouts that must support multiple writing directions.
Direction Inheritance
The direction property is inherited, meaning descendants normally receive the computed direction of their parent.
Parent Direction
.rtl-container {
direction: rtl;
}
HTML Structure
<div class="rtl-container">
<h2>مرحبا</h2>
<p>هذا نص عربي.</p>
</div>
Both the heading and paragraph inherit right-to-left direction unless another value is specified.
Override a Child
.rtl-container {
direction: rtl;
}
.rtl-container .english-content {
direction: ltr;
}
Special Case: Table Columns
The direction property can be applied to table column elements, but table cells do not inherit direction from columns because columns are not ancestors of their cells.
Important Consideration
Set the direction on a meaningful containing element when several descendants share the same writing direction.
direction Example
This example compares left-to-right and right-to-left base direction using two text containers.
Both containers use the same border and padding styles. Their direction and start alignment are different.
<div class="direction-demo">
<div class="direction-box ltr-box">
<h3>Left-to-Right (ltr)</h3>
<p>Learn HTML and CSS.</p>
<p>Text begins on the left.</p>
</div>
<div class="direction-box rtl-box" lang="ar">
<h3>Right-to-Left (rtl)</h3>
<p>مرحبا بالعالم</p>
<p>تعلم HTML و CSS</p>
</div>
</div>
.direction-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 15px;
}
.direction-demo .direction-box {
padding: 15px;
border: 1px solid #cccccc;
background-color: #f8f9fa;
text-align: start;
}
.direction-demo .direction-box h3 {
margin: 0 0 10px;
font-size: 1rem;
}
.direction-demo .direction-box p {
margin: 0 0 8px;
}
.direction-demo .ltr-box {
direction: ltr;
border-inline-start: 4px solid #336699;
}
.direction-demo .rtl-box {
direction: rtl;
border-inline-start: 4px solid #4f8a5b;
}
Left-to-Right (ltr)
Learn HTML and CSS.
Text begins on the left.
Right-to-Left (rtl)
مرحبا بالعالم
تعلم HTML و CSS
The left-to-right container starts its text on the left, while the right-to-left container starts its text on the right.
The logical border-inline-start property also adapts to direction, placing the colored border on the left or right side as appropriate.
The Browser Result includes HTML dir attributes to preserve the intended direction even if CSS is unavailable.
Accessibility
Correct text direction is important for readability, assistive technology, and multilingual content.
Use HTML Direction Attributes
Prefer dir="ltr", dir="rtl", or dir="auto" to identify the direction of HTML content.
Identify the Language
Use the HTML lang attribute to identify the language of text. Language and direction serve different purposes and may both be necessary.
Support Mixed-Direction Text
Use appropriate bidirectional isolation for embedded text such as filenames, user-generated content, and product names.
Avoid Forced Reordering
Do not use directional overrides to produce decorative text effects that make content difficult to read or interpret.
Maintain Logical Reading Order
Changing text direction should not be used to create a visual order that conflicts with the intended reading and keyboard navigation order.
Use Direction-Aware Styling
Logical CSS properties help preserve consistent spacing and alignment across left-to-right and right-to-left layouts.
Important Considerations
Keep the following points in mind when using direction:
- Purpose: Establishes the base direction of text.
- Values:
ltrandrtl. - Initial value:
ltr. - Inheritance: The property is inherited.
- HTML preference: Use the
dirattribute for defining content direction in HTML documents. - Automatic direction: HTML supports
dir="auto", but CSSdirectiondoes not supportauto. - Bidirectional text: Mixed-direction content follows the Unicode bidirectional algorithm.
- unicode-bidi: Works with
directionto control bidirectional formatting. - Text alignment:
text-align: startandendadapt to writing direction. - Writing mode:
writing-modecontrols horizontal and vertical text layout separately. - Logical properties: Inline-start and inline-end positions depend on writing direction and mode.
- Flexbox and Grid: Direction can influence layout start edges and placement.
- Table columns: Cells do not inherit direction from table column elements.
- Animation: The property is not animatable.
- all shorthand: The
allproperty does not resetdirection. - Accessibility: Correct direction improves multilingual readability and interpretation.
Use direction to support genuine writing-direction requirements. For multilingual HTML content, combine semantic direction attributes with logical CSS properties for reliable layouts.
Browser Support
The CSS direction property is widely supported across modern browsers, including Chrome, Edge, Firefox, and Safari.
Both ltr and rtl have longstanding browser support.
Basic Example
.rtl-content {
direction: rtl;
}
HTML Alternative
<div dir="rtl">
<p>مرحبا بالعالم</p>
</div>
Feature Detection
@supports (direction: rtl) {
.rtl-content {
direction: rtl;
}
}
Feature detection is generally unnecessary for basic direction support because the property is broadly implemented.
For current compatibility information, visit Can I Use? .
Specifications
The direction property is defined in CSS Writing Modes Level 3.
The specification defines the property's directional values, inheritance, and interaction with bidirectional text and writing modes.
