Advanced styling with CSS attribute selectors

Frontend Engineer who loves building tools.
Search for a command to run...

Frontend Engineer who loves building tools.
No comments yet. Be the first to comment.
Performance is a touchy subject in software engineering. It’s one thing to write code and ship it; it’s another thing entirely to make it faster and more enjoyable for your users — and the next developers after you. In this article, we will look at h...
Animations seem to be a necessity these days in frontend development, improving the user experience in apps by a significant margin. Thankfully vue.js shipped two built-in components that abstract away the complexities of animating our apps; transiti...
Arrays have a lot of helpful higher-order functions that make working with them easier. Our focus in this article will be the sort() method, this method has both easy and complex use cases which we'll be looking at in this article. First look at the ...
Gridsome is a static site generator used for building powerful frontend applications, it was built on the Vue.js framework. One of the beauties of Gridsome apart from its ability to generate server-side rendered applications is its compatibility with...
Build hooks are unique URLs used to trigger a build on the Netlify. They can be used to refresh the content on your website after you’ve published or unpublished content in your Storyblok space. Getting the hook URL from Netlify To create one, open y...

There are many ways to target HTML elements using CSS selectors, we mostly use the three common ones; tag, class and id selectors, but there are a lot more advanced ways to target elements.
We'll be looking at how to target elements by their attributes and values.
To target an element by its attribute, we wrap the attribute name in a pair of square brackets: [], so if we wanted to target all elements having the href attribute;
[href] {
color: red;
}
Elements affected by that styling would look like these:
<a href="https://google.com">Google.com</a> <a href="https://theninja.blog">The Ninja Blog</a>
It's also possible to style such elements based on the values of their attributes, the selectors for targeting attribute values are listed below.
[attribute=value]This selector targets elements whose attribute value match the ones specified. An example snippet:
a[target='_blank'] {
color: green;
}
Elements targeted look like this:
<a href="https://google.com" target="_blank">Google</a>
So CSS is targeting elements whose target attributes are equal to _blank.
[attribute^=value]This selector targets elements whose attribute value starts with the specified word. Example:
p[class^='red'] {
color: red;
}
Elements styled will look like this:
<p class="red blue orange">This will be a paragraph with red text</p>
[attribute*=value]This selector targets elements whose attribute values contain the specified substring regardless of the position. Example:
p[class*='blue'] {
color: blue;
}
Elements targeted will look like these:
<p class="red yellowblue orange">This will be a paragraph with blue text</p>
<p class="blueyellow red orange">This will also be a paragraph with blue text</p>
[attribute$=value]This selector targets elements whose attribute values end with the word specified in the selector. Example:
p[class$='orange'] {
color: orange;
}
Elements targeted will look like these:
<p class="red blue orange">This will be a paragraph with orange text</p>
[attribute~=value]This selector targets elements whose attribute contains a specific value separated by space, meaning that it matches the entire word and not a substring.
Example:
p[class~='red'] {
color: red;
}
Elements targeted look like:
<p class="yellow red blue">This will be a paragraph with red text</p>
These are the most used attribute selectors out there, there are a few more, you can read about the rest on CSS Tricks here.