27
loading...
This website collects cookies to deliver better user experience
If you not noticed the part-1, don't worry just relax explore the part-2 then move to Part-1 and I'm not rush you which is completely based on your pace.
Every attribute selector should be in square-bracket[ ]
[attr]
attr
.<a href="http://example.com" target="_blank">I'm External Link😎</a>
<a href="#internal">I'm internal link🙄</a>
[target]{
color: orange;
}
/* attribute selector can also be specify with their element name */
a[target]{
color: orange;
}
targe
attribute exist in a
tag then apply their styles. Hope make sense. If you feel need some practice then don't hesitate, feel free to play around in codepen below.Real time situation - Some situations like need to imply the user that some links are linked to external resources. At that time, Use a[target]
[attr=value]
In simple words, Attribute Exactly Equals Certain Value.
<a href="https://example.org" target="_blank">Yes, I'm😎</a>
<a href="#internal">Oops, I'm not😣</a>
/* <a> elements with an href matching "https://example.org" */
a[href="https://example.org"] {
color: green;
}
a
tag contain href
attribute which exactly equal to the"https://example.org"
value and then only which apply styles. Hope you get a idea and i wish you to do practice on code pen.[attr~=value]
Simply like Attribute Value is in Space-Separated List
<img src="//placehold.it/150/150" alt="abstract art">
<img src="//placehold.it/150/150" alt="something to eat">
<img src="//placehold.it/150/150" alt="athlete starting a new sport">
img[alt~="art"] {
border: 3px solid lightgreen;
}
alt
attribute, which either as the only value or as a whole word in a space separated list. Hey observe that, the image with the alt
text "athlete starting a new sport" is not outlined. But, The image with alt
text "abstract art" is outlined. Hope make sense.[attr|=value]
Rephrase the above definition in simple words like Attribute value starts with this in a dash-separated list.
<ul>
<li data-years="1800-1900">The 19th Century</li>
<li data-years="1900-2000">The 20th Century</li>
<li data-years="2000-2100">The 21st Century</li>
</ul>
/* attr|=value */
li[data-years|="1900"] {
color: red;
}
[attr^=value]
^
) before the equals sign. Be consious , case-sensitivity matters. Attribute Begins with Certain Value
<img src="//placehold.it/150/184/abstract" alt="artistic pattern">
<img src="//placehold.it/150/184/food" alt="a healthy meal">
<img src="//placehold.it/150/184/sports" alt="Arthur Miller">
img[alt^="art"] {
/* alt attribute starts with the value "art" */
border: 3px solid #e18728;
}
alt
text that starts with "art". Notice that the image with the alt
text "artistic pattern" is selected, but the image with the alt
text "Arthur Miller" is not because attribute selectors are case-sensitive.[attr$=value]
Similar to type-5. In type-5(^
) which check the match in start but in type-6($
) which check the match in end
<h2>[attr$=value]</h2>
<a href="example.pdf" target="_blank">I'm pdf file😉</a>
<a href="#internal">hm, I'm a link😒</a>
[href$="pdf"]{
background: white;
color: brown;
text-decoration: none;
padding: .5em;
border-radius: 2em;
display: inline-block;
}
href
that ends with "pdf" and then apply the styles.[attr*=value]
It simply means value need not to be in start or end or space-seprated or dash seperated, which expect Attribute Contains Certain Value anywhere.
<h2>[attr*=value]</h2>
<img src="//placehold.it/150/150" alt="abstract art">
<img src="//placehold.it/150/150" alt="something to eat">
<img src="//placehold.it/150/150" alt="athlete starting a new sport">
/* [attr*=value] */
img[alt*="art"] {
border: 3px solid lightgreen;
}
alt
text and give them an outline. Notice that the images with the alt text "abstract art" and "athlete starting a new sport" both get outlined. Hope you grasp that idea.