dir
global attribute
is an enumerated attribute that indicates the directionality of the element's text.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
It can have the following values:
ltr
, which means
left to right
and is to be used for languages that are written from the left to the right (like English);
rtl
, which means
right to left
and is to be used for languages that are written from the right to the left (like Arabic);
auto
, which lets the user agent decide. It uses a basic algorithm as it parses the characters inside the element until it finds a character with a strong directionality, then applies that directionality to the whole element.
Usage notes:
This attribute is mandatory for the
<bdo>
element where it has a different semantic meaning.
This attribute is
not
inherited by the
<bdi>
element. If not set, its value is
auto
.
This attribute can be overridden by the CSS properties
direction
and
unicode-bidi
, if a CSS page is active and the element supports these properties.
As the directionality of the text is semantically related to its content and not to its presentation, it is recommended that web developers use this attribute instead of the related CSS properties when possible. That way, the text will display correctly even on a browser that doesn't support CSS or has the CSS deactivated.
auto
value should be used for data with an unknown directionality, like data coming from user input, eventually stored in a database.
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 Internet Explorer and Edge use the key combinations
Ctrl
+
Left Shift
and
Ctrl
+
Right Shift
. Firefox uses
Ctrl
/
Cmd
+
Shift
+
X
but does NOT update the
dir
attribute value.
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'dir' in that specification. |
实时标准 | No change from latest snapshot, HTML 5.1 |
|
HTML 5.1
The definition of 'dir' in that specification. |
推荐 | 快照 HTML 实时标准 , no change from HTML5 |
|
HTML5
The definition of 'dir' in that specification. |
推荐 |
快照
HTML 实时标准
,从
HTML 4.01 Specification
it added the
auto
value, and is now a true global attribute.
|
|
HTML 4.01 Specification
The definition of 'dir' in that specification. |
推荐 |
Supported on all elements but
<applet>
,
<base>
,
<basefont>
,
<bdo>
,
<br>
,
<frame>
,
<frameset>
,
<iframe>
,
<param>
,和
<script>
.
|
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
dir
|
Chrome 完整支持 Yes | Edge 完整支持 79 | Firefox 完整支持 Yes | IE 不支持 No | Opera 完整支持 Yes | Safari 完整支持 Yes | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 Yes | Opera Android 完整支持 Yes | Safari iOS 完整支持 Yes | Samsung Internet Android 完整支持 Yes |
完整支持
不支持
HTMLElement.dir
that reflects this attribute.
<a>
<abbr>
<acronym>
<address>
<applet>
<area>
<article>
<aside>
<audio>
<b>
<base>
<basefont>
<bdi>
<bdo>
<bgsound>
<big>
<blink>
<blockquote>
<body>
<br>
<button>
<canvas>
<caption>
<center>
<cite>
<code>
<col>
<colgroup>
<command>
<content>
<data>
<datalist>
<dd>
<del>
<details>
<dfn>
<dialog>
<dir>
<div>
<dl>
<dt>
<element>
<em>
<embed>
<fieldset>
<figcaption>
<figure>
<font>
<footer>
<form>
<frame>
<frameset>
<h1>
<head>
<header>
<hgroup>
<hr>
<html>
<i>
<iframe>
<image>
<img>
<input>
<ins>
<isindex>
<kbd>
<keygen>
<label>
<legend>
<li>
<link>
<listing>
<main>
<map>
<mark>
<marquee>
<menu>
<menuitem>
<meta>
<meter>
<multicol>
<nav>
<nextid>
<nobr>
<noembed>
<noframes>
<noscript>
<object>
<ol>
<optgroup>
<option>
<output>
<p>
<param>
<picture>
<plaintext>
<pre>
<progress>
<q>
<rb>
<rp>
<rt>
<rtc>
<ruby>
<s>
<samp>
<script>
<section>
<select>
<shadow>
<slot>
<small>
<source>
<spacer>
<span>
<strike>
<strong>
<style>
<sub>
<summary>
<sup>
<table>
<tbody>
<td>
<template>
<textarea>
<tfoot>
<th>
<thead>
<time>
<title>
<tr>
<track>
<tt>
<u>
<ul>
<var>
<video>
<wbr>
<xmp>
<input>
类型
<input type="button">
<input type="checkbox">
<input type="color">
<input type="date">
<input type="datetime">
<input type="datetime-local">
<input type="email">
<input type="file">
<input type="hidden">
<input type="image">
<input type="month">
<input type="number">
<input type="password">
<input type="radio">
<input type="range">
<input type="reset">
<input type="search">
<input type="submit">
<input type="tel">
<input type="text">
<input type="time">
<input type="url">
<input type="week">