--- title: Selektory artybutów slug: Web/CSS/Attribute_selectors translation_of: Web/CSS/Attribute_selectors ---
Selektor atrybutów CSS dopasowuje elementy w oparciu o obecność lub wartość danego atrybutu.
/* <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; }
[attr]
Reprezentuje elementy z atrybutem o nazwie attr.
[attr=value]
[attr~=value]
[attr|=value]
-
(U+002D). It is often used for language subcode matches.[attr^=value]
[attr$=value]
[attr*=value]
[attr operator value i]
i
(or I
) before the closing bracket causes the value to be compared case-insensitively (for characters within the ASCII range).[attr operator value s]
{{Experimental_Inline}}s
(or S
) before the closing bracket causes the value to be compared case-sensitively (for characters within the ASCII range).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; }
<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>
{{EmbedLiveSample("Links")}}
/* All divs with a `lang` attribute are bold. */
div[lang] {
font-weight: bold;
}
/* All divs without a `lang` attribute are italicized. */
div:not([lang]) {
font-style: italic;
}
/* 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;
}
<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>
{{EmbedLiveSample("Languages")}}
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 case-sensitive modifier.
/* 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; }
<ol type="A"> <li>Example list</li> </ol>
{{EmbedLiveSample("HTML_ordered_lists")}}
Specification | Status | Comment |
---|---|---|
{{SpecName("CSS4 Selectors", "#attribute-selectors", "attribute selectors")}} | {{Spec2("CSS4 Selectors")}} | Adds modifier for ASCII case-sensitive and case-insensitive attribute value selection. |
{{SpecName("CSS3 Selectors", "#attribute-selectors", "attribute selectors")}} | {{Spec2("CSS3 Selectors")}} | |
{{SpecName("CSS2.1", "selector.html#attribute-selectors", "attribute selectors")}} | {{Spec2("CSS2.1")}} | Initial definition |
{{Compat("css.selectors.attribute")}}