attr

この文書は翻訳中です。他国語のままの部分などがあるのはその為です。
是非お気軽に MDN に登録して翻訳に参加し、私たちの手助けをして下さい!

概要

CSS の attr() 式は選択要素の属性値を取得し、スタイルシートでそれを使うためのものです。擬似要素で使うこともでき、この場合、擬似要素を作る元になった要素の属性値が返されます。

attr() 式はすべての CSS プロパティで使うことができます。

構文

形式文法: attr( attribute-name <type-or-unit>? [, <fallback> ]? )

attribute-name
CSS で参照する、HTML 要素の属性名です。content 以外の属性のサポートは です。
<type-or-unit>
属性値の型またはその単位を示すキーワード。HTML では暗黙的に単位を持つ属性があります。属性に対して無効な値を <type-or-unit> で与えると、attr() 式も無効になります。省略するとデフォルトの string になります。有効値の一覧です:
キーワード 関係する型 コメント デフォルト値
string <文字列> 属性値は CSS <文字列> としてパースされます 空文字列
color <color> The attribute value is parsed as a hash (3- or 6-value hash) or a keyword. It must be a valid CSS <文字列> value.
Leading and trailing spaces are stripped.
currentColor
url <uri> The attribute value is parsed as a string that is used inside a CSS url()function.
Relative URL are resolved relatively to the original document, not relatively to the style sheet.
Leading and trailing spaces are stripped.
The url about:invalid that points to a non-existent document with a generic error condition.
integer <整数値> The attribute is parsed as a CSS <整数値>. If it is not valid, that is not an integer or out of the range accepted by the CSS property, the default value is used.
Leading and trailing spaces are stripped.
0, or, if 0 is not a valid value for the property, the property's minimum value.
number <数量> The attribute is parsed as a CSS <数量>. If it is not valid, that is not a number or out of the range accepted by the CSS property, the default value is used.
Leading and trailing spaces are stripped.
0, or, if 0 is not a valid value for the property, the property's minimum value.
length <length> The attribute is parsed as a CSS <length> dimension, that is including the unit (e.g. 12.5em). If it is not valid, that is not a length or out of the range accepted by the CSS property, the default value is used.
If the given unit is a relative length, attr()computes it to an absolute length.
Leading and trailing spaces are stripped.
0, or, if 0 is not a valid value for the property, the property's minimum value.
em, ex, px, rem, vw, vh, vmin, vmax, mm, cm, in, pt, or pc <length> The attribute is parsed as a CSS <数量>, that is without the unit (e.g. 12.5), and interpreted as a <length> with the specified unit. If it is not valid, that is not a number or out of the range accepted by the CSS property, the default value is used.
If the given unit is a relative length, attr()computes it to an absolute length.
Leading and trailing spaces are stripped.
0, or, if 0 is not a valid value for the property, the property's minimum value.
angle <角度> The attribute is parsed as a CSS <角度> dimension, that is including the unit (e.g. 30.5deg). If it is not valid, that is not an angle or out of the range accepted by the CSS property, the default value is used.
Leading and trailing spaces are stripped.
0deg, or, if 0deg is not a valid value for the property, the property's minimum value.
deg, grad, rad <角度> The attribute is parsed as a CSS <数量>, that is without the unit (e.g. 12.5), and interpreted as an <角度> with the specified unit. If it is not valid, that is not a number or out of the range accepted by the CSS property, the default value is used.
Leading and trailing spaces are stripped.
0deg, or, if 0deg is not a valid value for the property, the property's minimum value.
time <時間> The attribute is parsed as a CSS <時間> dimension, that is including the unit (e.g. 30.5ms). If it is not valid, that is not a time or out of the range accepted by the CSS property, the default value is used.
Leading and trailing spaces are stripped.
0s, or, if 0s is not a valid value for the property, the property's minimum value.
s, ms <時間> The attribute is parsed as a CSS <数量>, that is without the unit (e.g. 12.5), and interpreted as an<時間> with the specified unit. If it is not valid, that is not a number or out of the range accepted by the CSS property, the default value is used.
Leading and trailing spaces are stripped.
0s, or, if 0s is not a valid value for the property, the property's minimum value.
frequency <周波数> The attribute is parsed as a CSS <周波数> dimension, that is including the unit (e.g. 30.5kHz). If it is not valid, that is not a frequency or out of the range accepted by the CSS property, the default value is used. 0Hz, or, if 0Hz is not a valid value for the property, the property's minimum value.
Hz, kHz <周波数> The attribute is parsed as a CSS <数量>, that is without the unit (e.g. 12.5), and interpreted as a <周波数> with the specified unit. If it is not valid, that is not a number or out of the range accepted by the CSS property, the default value is used.
Leading and trailing spaces are stripped.
0Hz, or, if 0Hz is not a valid value for the property, the property's minimum value.
% <割合> The attribute is parsed as a CSS <数量>, that is without the unit (e.g. 12.5), and interpreted as a <割合>. If it is not valid, that is not a number or out of the range accepted by the CSS property, the default value is used.
If the given value is used as a length, attr()computes it to an absolute length.
Leading and trailing spaces are stripped.
0%, or, if 0% is not a valid value for the property, the property's minimum value.
<fallback>
関連する属性が見つからないか、無効値を含むときに使われる値です。この値は使用されないとしても attr() の使われている場所で有効値でなければならず、他の attr() 式を含んではいけません。attr() がプロパティの唯一の構成値でないときは、その <fallback> 値は <type-or-unit> で定義された型である必要があります。セットされていないと、CSS は各 <type-or-unit> で定義されたデフォルト値を使います。

p:before {
  content: attr(data-foo) " ";
}
<p data-foo="hello">world</p>

表示結果

hello world

仕様

仕様書 策定状況 コメント
CSS Values and Units Module Level 3 勧告候補 2 つのオプション引数を追加。全プロパティで使える。<文字列> 以外の値を返す可能性がある。これらの変更は で、ブラウザのサポートが少なすぎると CR 段階で外される可能性がある
CSS Level 2 (Revision 1) 勧告 content プロパティのみに制限。常に <文字列>} を返す

ブラウザ実装状況

機能 Chrome Firefox (Gecko) Internet Explorer Opera Safari
基本サポート 2.0 1.0 (1.7 or earlier) 8 9.0 3.1
content 以外のプロパティと非文字列値の使用 未サポート 未サポート (see バグ 435426) 未サポート ? ?
機能 Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
基本サポート 2.1 1.0 (1.0) 8 10.0 3.1
content 以外のプロパティと非文字列値の使用 ? 未サポート (see バグ 435426) 未サポート ? ?

Document Tags and Contributors

Contributors to this page: sosleepy, ethertank
最終更新者: ethertank,
サイドバーを隠す