attr

概要

attr() CSS 函數使用於樣式取得被選取之元素中特定屬性的值。它也可以用在擬元素選取項(Pseudo-element),在此情形下,其屬性值來自於擬元素選取項相依的原始元素。

attr() 函數可以被用在任何 CSS 屬性,但除了 content 以外的屬性皆屬於實驗階段。

語法

/* 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

在 CSS 中參考之 HTML 元素的屬性名稱。

<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 Comment Default value
string <string> (en-US) The attribute value is treated as a CSS <string> (en-US). 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> (en-US) The attribute value is parsed as a hash (3- or 6-value hash) or a keyword. It must be a valid CSS <string> (en-US) value. Leading and trailing spaces are stripped. currentColor
url 實驗性質 <uri> (en-US) 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> (en-US) The attribute value is parsed as a CSS <integer> (en-US). 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> (en-US) The attribute value is parsed as a CSS <number> (en-US). 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> (en-US) The attribute value is parsed as a CSS <length> (en-US) 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> (en-US) The attribute value is parsed as a CSS <number> (en-US), that is without the unit (e.g. 12.5), and interpreted as a <length> (en-US) 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> (en-US) The attribute value is parsed as a CSS <angle> (en-US) 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> (en-US) The attribute value is parsed as a CSS <number> (en-US), that is without the unit (e.g. 12.5), and interpreted as an <angle> (en-US) 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> (en-US) The attribute value is parsed as a CSS <time> (en-US) 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> (en-US) The attribute value is parsed as a CSS <number> (en-US), that is without the unit (e.g. 12.5), and interpreted as an<time> (en-US) 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> (en-US) The attribute value is parsed as a CSS <frequency> (en-US) 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> (en-US) The attribute value is parsed as a CSS <number> (en-US), that is without the unit (e.g. 12.5), and interpreted as a <frequency> (en-US) 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> (en-US) The attribute value is parsed as a CSS <number> (en-US), that is without the unit (e.g. 12.5), and interpreted as a <percentage> (en-US). 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. The fallback value must be valid where attr() is used, even if it is not used, and must not contain another attr() expression. If attr() is not the sole component value of a property, its <fallback> value must be of the type defined by <type-or-unit>. If not set, CSS will use the default value defined for each <type-or-unit>.

形式語法

<attr()> = 
attr( <q-name> <attr-type>? (en-US) , <declaration-value>? (en-US) )

<attr-type> =
string | (en-US)
url | (en-US)
ident | (en-US)
color | (en-US)
number | (en-US)
percentage | (en-US)
length | (en-US)
angle | (en-US)
time | (en-US)
frequency | (en-US)
flex | (en-US)
<dimension-unit>

範例

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

結果

規範

Specification
CSS Values and Units Module Level 5
# attr-notation

瀏覽器相容性

BCD tables only load in the browser