注意: attr() function can be used with any CSS property, but support for properties other than content is experimental, and support for the type-or-unit parameter is sparse.

attr() CSS function is used to retrieve the value of an attribute of the selected element and use it in the stylesheet. It can also be used on pseudo-elements , in which case the value of the attribute on the pseudo-element's originating element is returned.

/* Simple usage */
attr(data-count);
attr(title);
/* With type */
attr(src url);
attr(data-count number);
attr(data-width px);
/* With fallback */
attr(data-count number, 0);
attr(src url, "");
attr(data-width px, inherit);
attr(data-something, "default");
				

句法

attribute-name

Is the name of an attribute on the HTML element referenced in the CSS.

<type-or-unit>
Is a keyword representing either the type of the attribute's value, or its unit, as in HTML some attributes have implicit units. If the use of <type-or-unit> as a value for the given attribute is invalid, the attr() expression will be invalid too. If omitted, it defaults to string . The list of valid values are:
Keyword Associated type 注释 Default value
string <string> The attribute value is treated as a CSS <string> . It is NOT reparsed, and in particular the characters are used as-is instead of CSS escapes being turned into different characters. An empty 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> 值。
Leading and trailing spaces are stripped.
currentcolor
url <url> The attribute value is parsed as a string that is used inside a CSS url() 函数。
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 value 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 value 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 value 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 ,或 pc <length> The attribute value 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 value 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 value 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 value 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 value 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 value 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 value 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 value 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>
The value to be used if the associated attribute is missing or contains an invalid value. If not set, CSS will use the default value defined for each <type-or-unit> .

形式句法

attr( <attr-name> <type-or-unit>? [, <attr-fallback> ]? )

where
<type-or-unit> = string | color | url | integer | number | length | angle | time | frequency | cap | ch | em | ex | ic | lh | rlh | rem | vb | vi | vw | vh | vmin | vmax | mm | Q | cm | in | pt | pc | px | deg | grad | rad | turn | ms | s | Hz | kHz | %

范例

content property

HTML

<p data-foo="hello">world</p>
						

CSS

[data-foo]::before {
  content: attr(data-foo) " ";
}
						

结果

<color> value

这是 实验性技术
检查 浏览器兼容性表格 要小心谨慎在生产中使用这之前。

HTML

<div class="background" data-background="lime">background expected to be red if your browser does not support advanced usage of attr()</div>
						

CSS

.background {
  height: 100vh;
}
						
.background {
  background-color: red;
}
.background[data-background] {
  background-color: attr(data-background color, red);
}
						

规范

规范 状态 注释
CSS Values and Units Module Level 4
The definition of 'attr()' in that specification.
编者草案 Changed it to work like var() . Property values involving attr() are valid at parse time, and the validation of the attribute value is deferred to computed value time.
CSS Values and Units Module Level 3
The definition of 'attr()' in that specification.
候选推荐 Added two optional parameters;
can be used on all properties;
may return values other than <string> .

These changes are experimental and may be dropped during the CR phase if browser support is too small.
CSS Level 2 (Revision 1)
The definition of 'attr()' in that specification.
推荐 Limited to the content property;
always returns a <string> .

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
attr() Chrome 完整支持 2 Edge 完整支持 12 Firefox 完整支持 1 IE 完整支持 8 Opera 完整支持 9 Safari 完整支持 3.1 WebView Android 完整支持 ≤37 Chrome Android 完整支持 18 Firefox Android 完整支持 4 Opera Android 完整支持 10.1 Safari iOS 完整支持 3.1 Samsung Internet Android 完整支持 1.0
<fallback> Experimental Chrome 不支持 No Edge 不支持 No Firefox 不支持 No IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 不支持 No Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No
<type-or-unit> Experimental Chrome 不支持 No Edge 不支持 No Firefox 不支持 No IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 不支持 No Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

另请参阅

元数据

  • 最后修改:
  1. CSS
  2. CSS 参考