--- title: ':dir()' slug: 'Web/CSS/:dir' tags: - BiDi - CSS - Experimental - Pseudo-class - Reference - セレクター - 疑似クラス translation_of: 'Web/CSS/:dir' --- <div>{{CSSRef}}</div> <p><strong><code>:dir()</code> は <a href="/ja/docs/Web/CSS">CSS</a> の</strong> <a href="/ja/docs/CSS/Web/Pseudo-classes">擬似クラス</a>で、中に含まれる文字列の方向に基づいて要素を選択します。</p> <pre class="brush: css no-line-numbers notranslate">/* 右から左への文字列がある要素すべてを選択 */ :dir(rtl) { background-color: red; }</pre> <p><code>:dir()</code> 擬似クラスは文字方向の意味的な値、つまり、文書自身に設定されているもののみを使用します。スタイルによる文字方向、つまり、 {{cssxref("direction")}} のような CSS プロパティで設定された文字方向には対応しません。</p> <div class="note"> <p><strong>メモ:</strong> <code>:dir()</code> 擬似クラスは <code>[dir=…]</code> <a href="/ja/docs/Web/CSS/Attribute_selectors">属性セレクター</a> とは等価でないことに注意してください。後者は HTML の {{htmlattrxref("dir")}} 属性を選択しますが、これがない要素は、文字方向が親から継承されていても無視します。(同様に、 <code>[dir=rtl]</code> と <code>[dir=ltr]</code> は <code>auto</code> の値を選択しません。)それに対して、 <code>:dir()</code> は継承の場合も含め、{{glossary("user agent", "ユーザーエージェント")}} が計算した値で選択します。</p> </div> <div class="note"> <p><strong>メモ:</strong> HTML では、文字方向は {{htmlattrxref("dir")}} 属性で指定されます。他の文書タイプでは異なる方法があるかもしれません。</p> </div> <h2 id="Syntax" name="Syntax">構文</h2> <p><code>:dir()</code> 擬似クラスは1つの引数を取り、対象としたい文字方向を指定します。</p> <h3 id="Parameters" name="Parameters">引数</h3> <dl> <dt><code>ltr</code></dt> <dd>文字方向が左から右の要素を対象とします。</dd> <dt><code>rtl</code></dt> <dd>文字方向が右から左の要素を対象とします。</dd> </dl> <h3 id="Formal_syntax" name="Formal_syntax">形式文法</h3> {{csssyntax}} <h2 id="Examples" name="Examples">例</h2> <h3 id="HTML">HTML</h3> <pre class="brush:html; notranslate"><div dir="rtl"> <span>test1</span> <div dir="ltr">test2 <div dir="auto">עִבְרִית</div> </div> </div> </pre> <h3 id="CSS">CSS</h3> <pre class="brush: css notranslate">:dir(ltr) { background-color: yellow; } :dir(rtl) { background-color: powderblue; }</pre> <h3 id="Result" name="Result">結果</h3> <p>{{ EmbedLiveSample('Examples') }}</p> <h2 id="Specifications" name="Specifications">仕様書</h2> <table class="standard-table"> <thead> <tr> <th scope="col">仕様書</th> <th scope="col">状態</th> <th scope="col">備考</th> </tr> </thead> <tbody> <tr> <td>{{SpecName('HTML WHATWG', 'scripting.html#selector-ltr', ':dir(ltr)')}}</td> <td>{{Spec2('HTML WHATWG')}}</td> <td>変更なし。</td> </tr> <tr> <td>{{SpecName('CSS4 Selectors', '#the-dir-pseudo', ':dir()')}}</td> <td>{{Spec2('CSS4 Selectors')}}</td> <td>初回定義。</td> </tr> </tbody> </table> <h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> <div> <p>{{Compat("css.selectors.dir")}}</p> </div> <h2 id="See_also" name="See_also">関連情報</h2> <ul> <li>言語に関連する擬似クラス: {{cssxref(":lang")}}, {{cssxref(":dir")}}</li> </ul>