I know how to style when the direction is inline
<div dir="rtl">foo</div>
div[dir="rtl"]
{
....;
}
But how to style
<div style="direction:rtl">foo</div> ?
Both behaves as expected (right "alignment" of text) but I need finer tweaking for some elements inside (float, text-align...) and I can't set up my rule correctly in the second case.
I can't edit the html. I MUST use style="direction:rtl".
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).
Quick answer. If the overall document direction is right-to-left, add dir="rtl" to the html tag. Below the html tag, only use the dir attribute on structural elements on the rare occasions when the base direction needs to change in order for the text to display correctly. Never use CSS to apply the base direction.
For example, the en-US locale (for US English) specifies left-to-right. Most Western languages, as well as many others around the world, are written LTR. The opposite of LTR, RTL (Right To Left) is used in other common languages, including Arabic ( ar ) and Hebrew ( he ).
The :dir() pseudo-class in CSS allows elements to be selected based on the direction of the language, as determined in the HTML markup. There are really only two directions language can flow in a document, which are left-to-right and right-to-left.
Use dir="auto" on forms and inserted text in order to automatically detect the direction of content supplied at run-time
<div dir="auto">Hello, world!</div>
<br/>
<div dir="auto">لا إله إلا الله محمد رسول الله</div>
<br/>
<input type="text" dir="auto" value="Hello, world!" >
<br/><br/>
<input type="text" dir="auto" value="لا إله إلا الله محمد رسول الله" >
JSFIDDLE Demo https://jsfiddle.net/80k0drsf/
As you can't modify the HTML, a really really hacky selector would be:
div[style*="direction:rtl"] {
...
}
JSFiddle demo.
Note that I'm using style*=
as opposed to just style=
as this will also match elements which have more than just direction:rtl
declared within the element's style
property.
For extra strength in the selector, you could also use [style*="direction: rtl"]
to handle style
attributes which separate the values from the properties with a space:
[style*="direction:rtl"], [style*="direction: rtl"] { ... }
Alternatively in this case you could just match on a style
attribute which contains "rtl", as I'm pretty sure this combination of characters isn't used in any other property (ignoring external resources like background image file names):
[style*="rtl"] { ... }
Updated JSFiddle demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With