dir

dir 全局属性enumerated 属性,指示元素文本的方向性。

¥The dir global attribute is an enumerated attribute that indicates the directionality of the element's text.

Try it

它可以具有以下值:

¥It can have the following values:

  • ltr,表示从左到右,用于从左到右书写的语言(如英语);
  • rtl,表示从右到左,用于从右到左书写的语言(如阿拉伯语);
  • auto,让用户代理决定。它使用基本算法来解析元素内的字符,直到找到具有强方向性的字符,然后将该方向性应用于整个元素。

注意:该属性对于 <bdo> 元素是必需的,它具有不同的语义含义。

¥Note: This attribute is mandatory for the <bdo> element where it has a different semantic meaning.

  • <bdi> 元素不继承该属性。如果不设置,其值为 auto
  • 如果 CSS 页面处于活动状态并且该元素支持这些属性,则该属性可以被 CSS 属性 directionunicode-bidi 覆盖。
  • 由于文本的方向性在语义上与其内容相关,而不与其表示相关,因此建议 Web 开发者尽可能使用此属性而不是相关的 CSS 属性。这样,即使在不支持 CSS 或已禁用 CSS 的浏览器上,文本也能正确显示。
  • auto 值应用于方向性未知的数据,例如来自用户输入的数据,最终存储在数据库中。

注意:浏览器可能允许用户更改 <input><textarea> 的方向性,以协助创作内容。Chrome 和 Safari 在输入字段的上下文菜单中提供了方向性选项,而 Legacy Edge 使用组合键 Ctrl + Left ShiftCtrl + Right Shift。Firefox 使用 Ctrl/Cmd + Shift + X 但不更新 dir 属性值。

¥Note: Browsers might allow users to change the directionality of <input> and <textarea>s in order to assist with authoring content. Chrome and Safari provide a directionality option in the contextual menu of input fields while Legacy Edge uses the key combinations Ctrl + Left Shift and Ctrl + Right Shift. Firefox uses Ctrl/Cmd + Shift + X but does NOT update the dir attribute value.

规范

Specification
HTML Standard
# the-dir-attribute

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also