aboutsummaryrefslogtreecommitdiff
path: root/files/ru/web/css/attr()/index.html
blob: 6b4f90bec1056cccdfa6636625e8eedae6a08a5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
---
title: attr
slug: Web/CSS/attr()
translation_of: Web/CSS/attr()
---
<div>{{CSSRef}}</div>

<h2 id="Описание">Описание</h2>

<p>С помощью функции <a href="/en-US/docs/Web/CSS">CSS</a> <code>attr()</code> можно извлекать значение атрибута выбранного элемента и использовать это значение в таблице стилей. Функция работает и с псевдо-элементами. В этом случае возвращается значение атрибута элемента, для которого формируется <a href="/en-US/docs/Web/CSS/Pseudo-elements">псевдо-элемент</a>.</p>

<p>Функцию <code>attr()</code> можно использовать с любым свойством CSS, но поддержка иных свойств, кроме {{cssxref("content")}}, является экспериментальной.</p>

<h2 id="Синтаксис">Синтаксис</h2>

<pre class="brush:js">/* Пример простого использования */
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');
</pre>

<h3 id="Значения">Значения</h3>

<dl>
 <dt><code>attribute-name</code></dt>
 <dd>Название атрибута элемента HTML, на который ссылаемся в CSS.</dd>
 <dt><code style="white-space: nowrap;">&lt;type-or-unit&gt;</code> {{Experimental_Inline}}</dt>
 <dd>
 <div class="translate-form__result-text typo typo_text_l i-bem typo_js_inited">Является ключевым словом, представляющим либо тип значения атрибута, либо его единицу, так как в HTML некоторые атрибуты имеют неявные единицы. Если использование <code>&lt;type-or-unit&gt;</code> в качестве значения для данного атрибута недопустимо, выражение <code>attr()</code> также будет недопустимым. Если этот параметр опущен, по умолчанию используется <code>string</code> . Список допустимых значений:</div>

 <table class="standard-table">
  <thead>
   <tr>
    <th scope="col">Ключевое слово</th>
    <th scope="col">Тип</th>
    <th scope="col">Комментарий</th>
    <th scope="col">Значение по умолчанию</th>
   </tr>
  </thead>
  <tbody>
   <tr>
    <td><code>string</code></td>
    <td>{{cssxref("&lt;string&gt;")}}</td>
    <td>The attribute value is treated as a CSS {{cssxref("&lt;string&gt;")}}.  It is NOT reparsed, and in particular the characters are used as-is instead of CSS escapes being turned into different characters.</td>
    <td>An empty string</td>
   </tr>
   <tr>
    <td><code>color</code> {{experimental_inline}}</td>
    <td>{{cssxref("&lt;color&gt;")}}</td>
    <td>The attribute value is parsed as a hash (3- or 6-value hash) or a keyword. It must be a valid CSS {{cssxref("&lt;string&gt;")}} value.<br>
     Leading and trailing spaces are stripped.</td>
    <td><code>currentColor</code></td>
   </tr>
   <tr>
    <td><code>url</code> {{experimental_inline}}</td>
    <td>{{cssxref("&lt;url&gt;")}}</td>
    <td>The attribute value is parsed as a string that is used inside a CSS <code>url()</code>function.<br>
     Relative URL are resolved relatively to the original document, not relatively to the style sheet.<br>
     Leading and trailing spaces are stripped.</td>
    <td>The url <code>about:invalid</code> that points to a non-existent document with a generic error condition.</td>
   </tr>
   <tr>
    <td><code>integer</code> {{experimental_inline}}</td>
    <td>{{cssxref("&lt;integer&gt;")}}</td>
    <td>The attribute value is parsed as a CSS {{cssxref("&lt;integer&gt;")}}. 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.<br>
     Leading and trailing spaces are stripped.</td>
    <td><code>0</code>, or, if <code>0</code> is not a valid value for the property, the property's minimum value.</td>
   </tr>
   <tr>
    <td><code>number</code> {{experimental_inline}}</td>
    <td>{{cssxref("&lt;number&gt;")}}</td>
    <td>The attribute value is parsed as a CSS {{cssxref("&lt;number&gt;")}}. 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.<br>
     Leading and trailing spaces are stripped.</td>
    <td><code>0</code>, or, if <code>0</code> is not a valid value for the property, the property's minimum value.</td>
   </tr>
   <tr>
    <td><code>length</code> {{experimental_inline}}</td>
    <td>{{cssxref("&lt;length&gt;")}}</td>
    <td>The attribute value is parsed as a CSS {{cssxref("&lt;length&gt;")}} dimension, that is including the unit (e.g. <code>12.5em</code>). 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.<br>
     If the given unit is a relative length, <code>attr()</code>computes it to an absolute length.<br>
     Leading and trailing spaces are stripped.</td>
    <td><code>0</code>, or, if <code>0</code> is not a valid value for the property, the property's minimum value.</td>
   </tr>
   <tr>
    <td><code>em</code>, <code>ex</code>, <code>px</code>, <code>rem</code>, <code>vw</code>, <code>vh</code>, <code>vmin</code>, <code>vmax</code>, <code>mm</code>, <code>cm</code>, <code>in</code>, <code>pt</code>, or <code>pc</code> {{experimental_inline}}</td>
    <td>{{cssxref("&lt;length&gt;")}}</td>
    <td>The attribute value is parsed as a CSS {{cssxref("&lt;number&gt;")}}, that is without the unit (e.g. <code>12.5</code>), and interpreted as a {{cssxref("&lt;length&gt;")}} 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.<br>
     If the given unit is a relative length, <code>attr()</code>computes it to an absolute length.<br>
     Leading and trailing spaces are stripped.</td>
    <td><code>0</code>, or, if <code>0</code> is not a valid value for the property, the property's minimum value.</td>
   </tr>
   <tr>
    <td><code>angle</code> {{experimental_inline}}</td>
    <td>{{cssxref("&lt;angle&gt;")}}</td>
    <td>The attribute value is parsed as a CSS {{cssxref("&lt;angle&gt;")}} dimension, that is including the unit (e.g. <code>30.5deg</code>). 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.<br>
     Leading and trailing spaces are stripped.</td>
    <td><code>0deg</code>, or, if <code>0deg</code> is not a valid value for the property, the property's minimum value.</td>
   </tr>
   <tr>
    <td><code>deg</code>, <code>grad</code>, <code>rad</code> {{experimental_inline}}</td>
    <td>{{cssxref("&lt;angle&gt;")}}</td>
    <td>The attribute value is parsed as a CSS {{cssxref("&lt;number&gt;")}}, that is without the unit (e.g. <code>12.5</code>), and interpreted as an {{cssxref("&lt;angle&gt;")}} 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.<br>
     Leading and trailing spaces are stripped.</td>
    <td><code>0deg</code>, or, if <code>0deg</code> is not a valid value for the property, the property's minimum value.</td>
   </tr>
   <tr>
    <td><code>time</code> {{experimental_inline}}</td>
    <td>{{cssxref("&lt;time&gt;")}}</td>
    <td>The attribute value is parsed as a CSS {{cssxref("&lt;time&gt;")}} dimension, that is including the unit (e.g. <code>30.5ms</code>). 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.<br>
     Leading and trailing spaces are stripped.</td>
    <td><code>0s</code>, or, if <code>0s</code> is not a valid value for the property, the property's minimum value.</td>
   </tr>
   <tr>
    <td><code>s</code>, <code>ms</code> {{experimental_inline}}</td>
    <td>{{cssxref("&lt;time&gt;")}}</td>
    <td>The attribute value is parsed as a CSS {{cssxref("&lt;number&gt;")}}, that is without the unit (e.g. <code>12.5</code>), and interpreted as an{{cssxref("&lt;time&gt;")}} 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.<br>
     Leading and trailing spaces are stripped.</td>
    <td><code>0s</code>, or, if <code>0s</code> is not a valid value for the property, the property's minimum value.</td>
   </tr>
   <tr>
    <td><code>frequency</code> {{experimental_inline}}</td>
    <td>{{cssxref("&lt;frequency&gt;")}}</td>
    <td>The attribute value is parsed as a CSS {{cssxref("&lt;frequency&gt;")}} dimension, that is including the unit (e.g. <code>30.5kHz</code>). 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.</td>
    <td><code>0Hz</code>, or, if <code>0Hz</code> is not a valid value for the property, the property's minimum value.</td>
   </tr>
   <tr>
    <td><code>Hz</code>, <code>kHz</code> {{experimental_inline}}</td>
    <td>{{cssxref("&lt;frequency&gt;")}}</td>
    <td>The attribute value is parsed as a CSS {{cssxref("&lt;number&gt;")}}, that is without the unit (e.g. <code>12.5</code>), and interpreted as a {{cssxref("&lt;frequency&gt;")}} 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.<br>
     Leading and trailing spaces are stripped.</td>
    <td><code>0Hz</code>, or, if <code>0Hz</code> is not a valid value for the property, the property's minimum value.</td>
   </tr>
   <tr>
    <td><code>%</code> {{experimental_inline}}</td>
    <td>{{cssxref("&lt;percentage&gt;")}}</td>
    <td>The attribute value is parsed as a CSS {{cssxref("&lt;number&gt;")}}, that is without the unit (e.g. <code>12.5</code>), and interpreted as a {{cssxref("&lt;percentage&gt;")}}. 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.<br>
     If the given value is used as a length, <code>attr()</code>computes it to an absolute length.<br>
     Leading and trailing spaces are stripped.</td>
    <td><code>0%</code>, or, if <code>0%</code> is not a valid value for the property, the property's minimum value.</td>
   </tr>
  </tbody>
 </table>
 </dd>
 <dt><code>&lt;fallback&gt;</code> {{experimental_inline}}</dt>
 <dd>The value to be used if the associated attribute is missing or contains an invalid value. The fallback value must be valid where <code>attr()</code> is used, even if it is not used, and must not contain another <code>attr()</code> expression. If <code>attr()</code> is not the sole component value of a property, its <code>&lt;fallback&gt;</code> value must be of the type defined by <code>&lt;type-or-unit&gt;</code>. If not set, CSS will use the default value defined for each <code>&lt;type-or-unit&gt;</code>.</dd>
</dl>

<h3 id="Формальный_синтаксис">Формальный синтаксис</h3>

{{csssyntax}}

<h2 id="Пример">Пример</h2>

<pre class="brush: css">p::before {
  content: attr(data-foo) " ";
}
</pre>

<pre class="brush: html">&lt;p data-foo="hello"&gt;world&lt;/p&gt;
</pre>

<h3 id="Результат">Результат</h3>

<p>{{EmbedLiveSample("Пример", '100%', '80')}}</p>

<h2 id="Спецификации">Спецификации</h2>

{{Specifications}}

<h2 id="Совместимость_с_браузерами">Совместимость с браузерами</h2>

<p>{{Compat}}</p>