css: dir=“rtl” VS style=“direction:rtl”

六眼飞鱼酱① 提交于 2020-08-24 06:06:28

问题


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".


回答1:


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.




回答2:


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/




回答3:


just add class "rtl" to html tag

<html dir="rtl" class="rtl">
 <head>
   <style>
      html[class*="rtl"] body {
         background-color:blue;
      }
      html[class*="rtl"] div{
         background-color:green;
      }
   </style>
 </haed>

 <body>

 <div>
 </div>

 </body>
</html>


来源:https://stackoverflow.com/questions/22511460/css-dir-rtl-vs-style-directionrtl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!