Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

この文書は翻訳中です。他国語のままの部分などがあるのはその為です。
是非お気軽に 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 <string> 属性値は CSS <string> としてパースされます 空文字列
color <color> The attribute value is parsed as a hash (3- or 6-value hash) or a keyword. It must be a valid CSS <string> 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 <integer> The attribute is parsed as a CSS <integer>. 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 <number> The attribute is parsed as a CSS <number>. 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 <number>, 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 <angle> The attribute is parsed as a CSS <angle> 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 <angle> The attribute is parsed as a CSS <number>, that is without the unit (e.g. 12.5), and interpreted as an <angle> 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 <time> The attribute is parsed as a CSS <time> 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 <time> The attribute is parsed as a CSS <number>, that is without the unit (e.g. 12.5), and interpreted as an<time> 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 <frequency> The attribute is parsed as a CSS <frequency> 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 <frequency> The attribute is parsed as a CSS <number>, that is without the unit (e.g. 12.5), and interpreted as a <frequency> 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.
% <percentage> The attribute is parsed as a CSS <number>, that is without the unit (e.g. 12.5), and interpreted as a <percentage>. 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
The definition of 'attr()' in that specification.
勧告候補 2 つのオプション引数を追加。全プロパティで使える。<string> 以外の値を返す可能性がある。これらの変更は で、ブラウザのサポートが少なすぎると CR 段階で外される可能性がある
CSS Level 2 (Revision 1)
The definition of 'attr()' in that specification.
勧告 content プロパティのみに制限。常に <string>} を返す

ブラウザ実装状況

機能 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) 未サポート ? ?

ドキュメントのタグと貢献者

タグ: 
 このページの貢献者: mrstork, prayash, ethertank, sosleepy
 最終更新者: mrstork,