:dir() - CSS: Cascading Style Sheets 编辑

The :dir() CSS pseudo-class matches elements based on the directionality of the text contained in them.

/* Selects any element with right-to-left text */
:dir(rtl) {
  background-color: red;
}

The :dir() pseudo-class uses only the semantic value of the directionality, i.e., the one defined in the document itself. It doesn't account for styling directionality, i.e., the directionality set by CSS properties such as direction.

Note: Be aware that the behavior of the :dir() pseudo-class is not equivalent to the [dir=…] attribute selectors. The latter match the HTML dir attribute, and ignore elements that lack it — even if they inherit a direction from their parent. (Similarly, [dir=rtl] and [dir=ltr] won't match the auto value.) In contrast, :dir() will match the value calculated by the user agent, even if inherited.

Note: In HTML, the direction is determined by the dir attribute. Other document types may have different methods.

Syntax

The :dir() pseudo-class requires one parameter, representing the text directionality you want to target.

Parameters

ltr
Target left-to-right elements.
rtl
Target right-to-left elements.

Formal syntax

:dir( ltr | rtl )

Examples

HTML

<div dir="rtl">
  <span>test1</span>
  <div dir="ltr">test2
    <div dir="auto">עִבְרִית</div>
  </div>
</div>

CSS

:dir(ltr) {
  background-color: yellow;
}

:dir(rtl) {
  background-color: powderblue;
}

Result

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of ':dir(ltr)' in that specification.
Living StandardNo changes.
Selectors Level 4
The definition of ':dir()' in that specification.
Working DraftInitial definition.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:69 次

字数:4817

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文