attr
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since иÑÐ»Ñ 2015 г..
* Some parts of this feature may have varying levels of support.
ÐпиÑание
С помоÑÑÑ ÑÑнкÑии CSS attr() можно извлекаÑÑ Ð·Ð½Ð°Ñение аÑÑибÑÑа вÑбÑанного ÑлеменÑа и иÑполÑзоваÑÑ ÑÑо знаÑение в ÑаблиÑе ÑÑилей. ФÑнкÑÐ¸Ñ ÑабоÑÐ°ÐµÑ Ð¸ Ñ Ð¿Ñевдо-ÑлеменÑами. Ð ÑÑом ÑлÑÑае возвÑаÑаеÑÑÑ Ð·Ð½Ð°Ñение аÑÑибÑÑа ÑлеменÑа, Ð´Ð»Ñ ÐºÐ¾ÑоÑого ÑоÑмиÑÑеÑÑÑ Ð¿Ñевдо-ÑлеменÑ.
ФÑнкÑÐ¸Ñ attr() можно иÑполÑзоваÑÑ Ñ Ð»ÑбÑм ÑвойÑÑвом CSS, но поддеÑжка инÑÑ
ÑвойÑÑв, кÑоме content, ÑвлÑеÑÑÑ ÑкÑпеÑименÑалÑной.
СинÑакÑиÑ
/* ÐÑÐ¸Ð¼ÐµÑ Ð¿ÑоÑÑого иÑполÑÐ·Ð¾Ð²Ð°Ð½Ð¸Ñ */
attr(data-count);
attr(title);
/* С Ñипом */
attr(src url);
attr(data-count number);
attr(data-width px);
/* Ñ ÑоллбÑком */
attr(data-count number, 0);
attr(src url, '');
attr(data-width px, inherit);
attr(data-something, 'default');
ÐнаÑениÑ
attribute-name-
Ðазвание аÑÑибÑÑа ÑлеменÑа HTML, на коÑоÑÑй ÑÑÑлаемÑÑ Ð² CSS.
<type-or-unit>-
ЯвлÑеÑÑÑ ÐºÐ»ÑÑевÑм Ñловом, пÑедÑÑавлÑÑÑим либо Ñип знаÑÐµÐ½Ð¸Ñ Ð°ÑÑибÑÑа, либо его единиÑÑ, Ñак как в HTML некоÑоÑÑе аÑÑибÑÑÑ Ð¸Ð¼ÐµÑÑ Ð½ÐµÑвнÑе единиÑÑ. ÐÑли иÑполÑзование
<type-or-unit>в каÑеÑÑве знаÑÐµÐ½Ð¸Ñ Ð´Ð»Ñ Ð´Ð°Ð½Ð½Ð¾Ð³Ð¾ аÑÑибÑÑа недопÑÑÑимо, вÑÑажениеattr()Ñакже бÑÐ´ÐµÑ Ð½ÐµÐ´Ð¾Ð¿ÑÑÑимÑм. ÐÑли ÑÑÐ¾Ñ Ð¿Ð°ÑамеÑÑ Ð¾Ð¿ÑÑен, по ÑмолÑÐ°Ð½Ð¸Ñ Ð¸ÑполÑзÑеÑÑÑ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ÐнаÑение по ÑмолÑаниÑ: An empty string
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-
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:invalidthat points to a non-existent document with a generic error condition. 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, if0is not a valid value for the property, the property's minimum value. 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, if0is not a valid value for the property, the property's minimum value. 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, if0is not a valid value for the property, the property's minimum value. em,ex,px,rem,vw,vh,vmin,vmax,mm,cm,in,pt, orpc-
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, if0is not a valid value for the property, the property's minimum value. 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, if0degis not a valid value for the property, the property's minimum value. deg,grad,rad-
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, if0degis not a valid value for the property, the property's minimum value. 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, if0sis not a valid value for the property, the property's minimum value. s,ms-
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, if0sis not a valid value for the property, the property's minimum value. 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, if0Hzis not a valid value for the property, the property's minimum value. Hz,kHz-
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, if0Hzis not a valid value for the property, the property's minimum value. %-
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, if0%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 anotherattr()expression. Ifattr()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( <attr-name> <attr-type>? , <declaration-value>? )
<attr-name> =
[ <ident-token>? '|' ]? <ident-token>
<attr-type> =
type( <syntax> ) |
raw-string |
number |
<attr-unit>
<syntax> =
'*' |
<syntax-component> [ <syntax-combinator> <syntax-component> ]* |
<syntax-string>
<attr-unit> =
<custom-ident>
<syntax-component> =
<syntax-single-component> <syntax-multiplier>? |
'<' transform-list '>'
<syntax-combinator> =
'|'
<syntax-string> =
<string>
<syntax-single-component> =
'<' <syntax-type-name> '>' |
<ident>
<syntax-multiplier> =
'#' |
'+'
<syntax-type-name> =
angle |
color |
custom-ident |
image |
integer |
length |
length-percentage |
number |
percentage |
resolution |
string |
time |
url |
transform-function
ÐÑимеÑ
p::before {
content: attr(data-foo) " ";
}
<p data-foo="hello">world</p>
РезÑлÑÑаÑ
СпеÑиÑикаÑии
| Specification |
|---|
| CSS Values and Units Module Level 5 > # attr-notation > |