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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
---
title: Selektory artybutów
slug: Web/CSS/Attribute_selectors
translation_of: Web/CSS/Attribute_selectors
---
<div>{{CSSRef}}</div>
<p><strong>Selektor atrybutów </strong>CSS dopasowuje elementy w oparciu o obecność lub wartość danego atrybutu.</p>
<pre class="brush: css no-line-numbers">/* <a> Element z artybutem "title" */
a[title] {
color: purple;
}
/* <a> elemente href zawierajacy "https://example.org" */
a[href="https://example.org"] {
color: green;
}
/* <a> element href zawirający "example" */
a[href*="example"] {
font-size: 2em;
}
/* <a> elementy href kończące się na ".org" */
a[href$=".org"] {
font-style: italic;
}
/* <a> elementy, których atrybut klasy zawiera słowo "logo" */
a[class~="logo"] {
padding: 2px;
}</pre>
<h2 id="Syntax">Syntax</h2>
<dl>
<dt><code>[<em>attr</em>]</code></dt>
</dl>
<p>Reprezentuje elementy z atrybutem o nazwie attr.</p>
<dl>
<dt><code>[<em>attr</em>=<em>value</em>]</code></dt>
<dd>Reprezentuje elementy z atrybutem o nazwie attr, którego wartością jest "value".</dd>
<dt><code>[<em>attr</em>~=<em>value</em>]</code></dt>
<dd>Represents elements with an attribute name of <em>attr</em> whose value is a whitespace-separated list of words, one of which is exactly <em>value</em>.</dd>
<dt><code>[<em>attr</em>|=<em>value</em>]</code></dt>
<dd>Represents elements with an attribute name of <em>attr</em> whose value can be exactly <em>value</em> or can begin with <em>value</em> immediately followed by a hyphen, <code>-</code> (U+002D). It is often used for language subcode matches.</dd>
<dt><code>[<em>attr</em>^=<em>value</em>]</code></dt>
<dd>Represents elements with an attribute name of <em>attr</em> whose value is prefixed (preceded) by <em>value</em>.</dd>
<dt><code>[<em>attr</em>$=<em>value</em>]</code></dt>
<dd>Represents elements with an attribute name of <em>attr</em> whose value is suffixed (followed) by <em>value</em>.</dd>
<dt><code>[<em>attr</em>*=<em>value</em>]</code></dt>
<dd>Represents elements with an attribute name of <em>attr</em> whose value contains at least one occurrence of <em>value</em> within the string.</dd>
<dt id="case-insensitive"><code>[<em>attr</em> <em>operator</em> <em>value</em> i]</code></dt>
<dd>Adding an <code>i</code> (or <code>I</code>) before the closing bracket causes the value to be compared case-insensitively (for characters within the ASCII range).</dd>
<dt id="case-sensitive"><code>[<em>attr</em> <em>operator</em> <em>value</em> s]</code> {{Experimental_Inline}}</dt>
<dd>Adding an <code>s</code> (or <code>S</code>) before the closing bracket causes the value to be compared case-sensitively (for characters within the ASCII range).</dd>
</dl>
<h2 id="Examples">Examples</h2>
<h3 id="Links">Links</h3>
<h4 id="CSS">CSS</h4>
<pre class="brush: css">a {
color: blue;
}
/* Internal links, beginning with "#" */
a[href^="#"] {
background-color: gold;
}
/* Links with "example" anywhere in the URL */
a[href*="example"] {
background-color: silver;
}
/* Links with "insensitive" anywhere in the URL,
regardless of capitalization */
a[href*="insensitive" i] {
color: cyan;
}
/* Links with "cAsE" anywhere in the URL,
with matching capitalization */
a[href*="cAsE" s] {
color: pink;
}
/* Links that end in ".org" */
a[href$=".org"] {
color: red;
}</pre>
<h4 id="HTML">HTML</h4>
<pre class="brush: html"><ul>
<li><a href="#internal">Internal link</a></li>
<li><a href="http://example.com">Example link</a></li>
<li><a href="#InSensitive">Insensitive internal link</a></li>
<li><a href="http://example.org">Example org link</a></li>
</ul></pre>
<h4 id="Result">Result</h4>
<p>{{EmbedLiveSample("Links")}}</p>
<h3 id="Languages">Languages</h3>
<h4 id="CSS_2">CSS</h4>
<pre class="brush: css">/* All divs with a `lang` attribute are bold. */
div[lang] {
font-weight: bold;
}
/* All divs without a `lang` attribute are italicized. */
div:not([lang]) {
<span class="st">font-style: italic;</span>
}
/* All divs in US English are blue. */
div[lang~="en-us"] {
color: blue;
}
/* All divs in Portuguese are green. */
div[lang="pt"] {
color: green;
}
/* All divs in Chinese are red, whether
simplified (zh-CN) or traditional (zh-TW). */
div[lang|="zh"] {
color: red;
}
/* All divs with a Traditional Chinese
`data-lang` are purple. */
/* Note: You could also use hyphenated attributes
without double quotes */
div[data-lang="zh-TW"] {
color: purple;
}
</pre>
<h4 id="HTML_2">HTML</h4>
<pre class="brush: html"><div lang="en-us en-gb en-au en-nz">Hello World!</div>
<div lang="pt">Olá Mundo!</div>
<div lang="zh-CN">世界您好!</div>
<div lang="zh-TW">世界您好!</div>
<div data-lang="zh-TW">世界您好!</div>
</pre>
<h4 id="Result_2">Result</h4>
<p>{{EmbedLiveSample("Languages")}}</p>
<h3 id="HTML_ordered_lists">HTML ordered lists</h3>
<p>The HTML specification requires the {{htmlattrxref("type", "input")}} attribute to be matched case-insensitively due to it primarily being used in the {{HTMLElement("input")}} element, trying to use attribute selectors to with the {{htmlattrxref("type", "ol")}} attribute of an {{HTMLElement("ol", "ordered list")}} doesn't work without the <a href="#case-sensitive">case-sensitive</a> modifier.</p>
<h4 id="CSS_3">CSS</h4>
<pre class="brush: css">/* List types require the case sensitive flag due to a quirk in how HTML treats the type attribute. */
ol[type="a"] {
list-style-type: lower-alpha;
background: red;
}
ol[type="a" s] {
list-style-type: lower-alpha;
background: lime;
}
ol[type="A" s] {
list-style-type: upper-alpha;
background: lime;
}</pre>
<h4 id="HTML_3">HTML</h4>
<pre class="brush: html;"><ol type="A">
<li>Example list</li>
</ol></pre>
<h4 id="Result_3">Result</h4>
<p>{{EmbedLiveSample("HTML_ordered_lists")}}</p>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName("CSS4 Selectors", "#attribute-selectors", "attribute selectors")}}</td>
<td>{{Spec2("CSS4 Selectors")}}</td>
<td>Adds modifier for ASCII case-sensitive and case-insensitive attribute value selection.</td>
</tr>
<tr>
<td>{{SpecName("CSS3 Selectors", "#attribute-selectors", "attribute selectors")}}</td>
<td>{{Spec2("CSS3 Selectors")}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName("CSS2.1", "selector.html#attribute-selectors", "attribute selectors")}}</td>
<td>{{Spec2("CSS2.1")}}</td>
<td>Initial definition</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("css.selectors.attribute")}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li>{{CSSxRef("attr")}}</li>
<li>Selecting a single element: {{DOMxRef("Document.querySelector()")}}, {{DOMxRef("DocumentFragment.querySelector()")}}, or {{DOMxRef("Element.querySelector()")}}</li>
<li>Selecting all matching elements: {{DOMxRef("Document.querySelectorAll()")}}, {{DOMxRef("DocumentFragment.querySelectorAll()")}}, or {{DOMxRef("Element.querySelectorAll()")}}</li>
<li>The above methods are all implemented based on the {{DOMxRef("ParentNode")}} mixin; see {{DOMxRef("ParentNode.querySelector()")}} and {{DOMxRef("ParentNode.querySelectorAll()")}}</li>
</ul>
|