- direction
- Try it
- Syntax
- Values
- Formal definition
- Formal syntax
- Examples
- Setting right-to-left direction
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- How to display text Right-to-Left Using CSS?
- Alignment in CSS
- Center Alignment
- Syntax
- Example
- Center Alignment
- Left and Right Alignment
- Syntax
- Example
- left direction (hey there)
- Right direction (hello guys)
- Different methods of displaying text in CSS
- Text alignment Method
- Example
- text alignment method
- Text Directions Method
- Syntax
- Example
- hello everyone.
- hey, guys, this is the default direction of the text.
- Syntax
- Conclusion
- W3.CSS Display
- W3.CSS Display Classes
- How to display text Right-to-Left Using HTML?
- Syntax
- Example
- Example
- Example
- CSS Layout — Horizontal & Vertical Align
- Example
- Center Align Text
- Example
- Center an Image
- Example
- Left and Right Align — Using position
- Example
- Left and Right Align — Using float
- Example
- The clearfix Hack
- Without Clearfix
- With Clearfix
- Example
- Center Vertically — Using padding
- Example
- Example
- Center Vertically — Using line-height
- Example
- Center Vertically — Using position & transform
- Example
- Center Vertically — Using Flexbox
- Example
direction
Warning: Where possible, authors are encouraged to avoid using the direction CSS property and use the HTML dir global attribute instead.
The direction CSS property sets the direction of text, table columns, and horizontal overflow. Use rtl for languages written from right to left (like Hebrew or Arabic), and ltr for those written from left to right (like English and most other languages).
Try it
Note that text direction is usually defined within a document (e.g., with HTML’s dir attribute) rather than through direct use of the direction property.
The property sets the base text direction of block-level elements and the direction of embeddings created by the unicode-bidi property. It also sets the default alignment of text, block-level elements, and the direction that cells flow within a table row.
Unlike the dir attribute in HTML, the direction property is not inherited from table columns into table cells, since CSS inheritance follows the document tree, and table cells are inside of rows but not inside of columns.
The direction and unicode-bidi properties are the two only properties which are not affected by the all shorthand property.
Syntax
/* Keyword values */ direction: ltr; direction: rtl; /* Global values */ direction: inherit; direction: initial; direction: revert; direction: revert-layer; direction: unset;
Values
Text and other elements go from left to right. This is the default value.
Text and other elements go from right to left.
For the direction property to have any effect on inline-level elements, the unicode-bidi property’s value must be embed or override .
Formal definition
Formal syntax
Examples
Setting right-to-left direction
In the example below are two strings of text, both which are displaying using direction: rtl . While the Arabic text is displayed correctly with this setting, the English text now has a full stop in an unusual location.
blockquote direction: rtl; width: 300px; >
blockquote> p>This paragraph is in English but incorrectly goes right to left.p> p>p> blockquote> blockquote> p>هذه الفقرة باللغة العربية ، لذا يجب الانتقال من اليمين إلى اليسار.p> p>p> blockquote>
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on May 22, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
How to display text Right-to-Left Using CSS?
CSS (Cascading Style Sheets) is a style sheet language that is mainly used to style and describe the elements of HTML documents. One of the main features of this programming language is the separation of elements and presentation such as layers, colors, etc.
CSS is used to style all HTML tags, including the document’s body, headings, paragraphs, and other pieces of text. CSS can also be used to style the display of table elements, grid elements, and images.
The main difference between these two programming languages is that HTML (Hypertext Markup Language) is a language used to describe the structure of a web page whereas CSS (Cascading Style Sheets) is a language used to describe the style of a web page created by using HTML.
Alignment in CSS
There are four main types of alignments: left, right, center, and justify.
- Left − Text is aligned with the left margin. It is often used for body text, as it is the easiest to read.
- Right − Text is aligned with the right margin. It is often used for headlines and titles, as it creates a more formal look.
- Center − Text is centered between the left and right margins. It is often used for images, as it creates a more balanced look.
- Justify − Text is aligned with both the left and right margins. It is often used for paragraphs, as it creates a cleaner look, like this good-looking article.
Let’s take a better look at these alignments in detail.
Center Alignment
In CSS, center alignment is mainly used to align images in the center of the page. To center align an element in CSS, you can use the margin property with the value set to auto. margin: auto; will center align the element. although we mainly use this for images, it can also align text using the following syntax −
Syntax
Example
.centerCenter Alignment
To horizontally center a block element (like this), use margin: auto;
Hey. this is center alignment.
Left and Right Alignment
In CSS, left and right alignment have similar code. The main difference is that the name of the direction is changed.
Syntax
Variable/heading < text-align: left; >Variable/heading
Example
The following example helps us understand the alignment better.
h1 < text-align: left; >h2left direction (hey there)
Right direction (hello guys)
The two headings above are aligned left and right.
Different methods of displaying text in CSS
Now, let’s see the different types of methods of how to display text using right to left using CSS. There are mainly 2 different methods of doing this namely
Let’s discuss them one by one in detail with examples.
Text alignment Method
This particular method more or less has a similar syntax or method of application as it is discussed above for aligning the content in different places with respect to margin. The user can easily display text from right to left using this method. Moreover, this method is for displaying the whole content aligning in the desired direction whether it is left, right, or center to the margin. For the given question this syntax will be
Example
For more clarity here’s an example to simplify things
text alignment method
Text is displayed in the right direction by using the text alignment method
Text Directions Method
The text-direction property in CSS defines the direction of the text. It can be used to set the text direction for an entire document, or a specific element within the document. The text-direction property can be set to one of four values −
Syntax
Text direction in CSS can be displayed easily in right to left direction by the following syntax −
Example
The following code shows the comparison between the normal text direction, which is left to right, and the other text direction which is right to left −
h1 < color: orange; text-align: center; >.rtlhello everyone.
hey, guys, this is the default direction of the text.
Now, the text is from right to left direction.
We can use the «direction» property in CSS to set the text direction. The «direction» property in CSS defines the text direction. It can be set to either «ltr» (left-to-right) or «rtl» (right-to-left). The syntax for “ltr” is very similar to the “rtl” syntax.
Syntax
However, it is already set as the default direction in most programming languages including CSS and HTML. So, this syntax is not used that frequently.
Conclusion
To conclude, direction property of CSS is a useful tool, one can easily display text right-to-left. This is an important feature to consider when designing websites for international audiences as it ensures that everyone can access and understand your content. With this guide, you now have a better understanding of how to use the direction property in CSS to display text alignments in CSS.
W3.CSS Display
The display classes allow you to display HTML elements in specific positions inside other HTML elements:
W3.CSS Display Classes
W3.CSS provides the following display classes:
Class | Defines |
---|---|
w3-display-container | Container for w3-display-classes |
w3-display-topleft | Displays content at the top left corner of the w3-display-container |
w3-display-topright | Displays content at the top right corner of the w3-display-container |
w3-display-bottomleft | Displays content at the bottom left corner of the w3-display-container |
w3-display-bottomright | Displays content at the bottom right corner of the w3-display-container |
w3-display-left | Displays content to the left (middle left) of the w3-display-container |
w3-display-right | Displays content to the right (middle right) of the w3-display-container |
w3-display-middle | Displays content in the middle (center) of the w3-display-container |
w3-display-topmiddle | Displays content at the top middle of the w3-display-container |
w3-display-bottommiddle | Displays content at the bottom middle of the w3-display-container |
w3-display-position | Displays content at a specified position in the w3-display-container |
w3-display-hover | Displays content on hover inside the w3-display-container |
w3-left | Floats an element to the left (float: left) |
w3-right | Floats an element to the right (float: right) |
w3-show | Shows an element (display: block) |
w3-hide | Hides an element (display: none) |
w3-mobile | Adds mobile-first responsiveness to any element. Displays elements as block elements on mobile devices |
How to display text Right-to-Left Using HTML?
The direction property specifies the text direction within a block element on the web page.
We use the style attribute, to set text direction in HTML. The style attribute specifies an inline style for an element within a block. The style attribute is used with the CSS property direction to set direction for the text.
Syntax
Following is the syntax to set text direction (right-to-left) using CSS property.
Example
Following is the example program to set direction to the
element in HTML.
DOCTYPE html> html> head> head> body> p style = "direction: rtl;"> whiteboards are so often at the core of a collaborative process p> body> html>
Example
Following is the example program to set direction to the element in HTML.
DOCTYPE html> html> head> head> body> h2 style = "direction: rtl;"> Hallowen season it is. h2> body> html>
Example
DOCTYPE html> html> head> head> body> ul style = "direction: rtl;"> li>Coffeeli> li>Teali> li>Milkli> ul> body> html>
CSS Layout — Horizontal & Vertical Align
Setting the width of the element will prevent it from stretching out to the edges of its container.
The element will then take up the specified width, and the remaining space will be split equally between the two margins:
This div element is centered.
Example
Note: Center aligning has no effect if the width property is not set (or set to 100%).
Center Align Text
To just center the text inside an element, use text-align: center;
Example
Tip: For more examples on how to align text, see the CSS Text chapter.
Center an Image
To center an image, set left and right margin to auto and make it into a block element:
Example
Left and Right Align — Using position
One method for aligning elements is to use position: absolute; :
In my younger and more vulnerable years my father gave me some advice that I’ve been turning over in my mind ever since.
Example
Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.
Left and Right Align — Using float
Another method for aligning elements is to use the float property:
Example
The clearfix Hack
Note: If an element is taller than the element containing it, and it is floated, it will overflow outside of its container. You can use the «clearfix hack» to fix this (see example below).
Without Clearfix
With Clearfix
Then we can add the clearfix hack to the containing element to fix this problem:
Example
Center Vertically — Using padding
There are many ways to center an element vertically in CSS. A simple solution is to use top and bottom padding :
Example
To center both vertically and horizontally, use padding and text-align: center :
I am vertically and horizontally centered.
Example
Center Vertically — Using line-height
Another trick is to use the line-height property with a value that is equal to the height property:
I am vertically and horizontally centered.
Example
.center <
line-height: 200px;
height: 200px;
border: 3px solid green;
text-align: center;
>
/* If the text has multiple lines, add the following: */
.center p line-height: 1.5;
display: inline-block;
vertical-align: middle;
>
Center Vertically — Using position & transform
If padding and line-height are not options, another solution is to use positioning and the transform property:
I am vertically and horizontally centered.
Example
.center <
height: 200px;
position: relative;
border: 3px solid green;
>
.center p margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
>
Tip: You will learn more about the transform property in our 2D Transforms Chapter.
Center Vertically — Using Flexbox
You can also use flexbox to center things. Just note that flexbox is not supported in IE10 and earlier versions:
Example
.center <
display: flex;
justify-content: center;
align-items: center;
height: 200px;
border: 3px solid green;
>
Tip: You will learn more about Flexbox in our CSS Flexbox Chapter.