In this guide we'll some of the most common CSS selectors to target HTML elements with styling. Knowing about the selectors will help clean up your CSS rulesets and be the most efficient in styling elements.
The same way HTML is made up of elements (consisting of tags, attributes, and content), CSS is made up of rulesets (selectors & declarations - property + value). Here is an example of a CSS ruleset:
The most basic CSS ruleset consists of the selector (p in this case), curly braces, and then declarations between the braces. You can learn more about applying CSS to HTML in my guide The 5 Ways to Add CSS to Your Website: A Complete Guide.
There are a number of selectors that you can use. Let's cover those next.
The Universal Selector is denoted by the asterisk as the selector and is generally used to neutralize browser default styles in a CSS Reset file or your own reset rulesets.
The Universal Selector is useful because it selects all elements on a page.
The Element Selector is one of the best ways to get started building out css rulesets. It is the perfect transition from HTML because you are simply naming the HTML element as the selector followed by the curly braces and declarations.
When you are looking to style all of the elements with a given HTML name be sure to use the Element selector. I find rulesets much easier to read when you use these broader selectors before going more niche.
In HTML you can attach a class attribute to associate a bunch of elements together and in CSS you can then target that class attribute to style those elements all in the same way. It is like element selector but it isn't element specific. For instance if you wanted all your first headings and paragraphs to be the color red you could do that with element selectors or with a class selector.
To use the class selector you want to first type a period followed by the name of the class and then curly braces. In the example above if we were to use the Element selector all the headings and paragraphs would be styled. If were to use the class selector only the headings and paragraphs that have the HTML class attribute would be the color red.
The id selector is very similar to the class selector except instead of targeting a group of elements you are just targeting one specific element. When you think of the id selector think of yourself (you are the only unique living breathing person on the planet). To use the id selector you need to first use the HTML id attribute on an element and then in css use the octothorpe (hashtag, pound sign, etc.) followed by the name of the id.
You'll see the id attribute used a lot in forms but you also can use the id attribute for any element just like the class attribute. The only difference is the id is for one element whereas multiple elements can be in a class.
The attribute selector is used when you want to target an element/s with a specific attribute. When writing the CSS selector it can be used in conjunction with the element name or separate. Take this example:
Here both CSS rulesets accomplish the same thing, but using element + attribute is a bit more specific and clear.
There are multiple variations of the attribute selector beyond checking the existence of an attribute like the example above to keep in mind:
Any descendent element within a container that matches this selector will be styled.
Here we are selecting all <p> elements inside a <div>. It serves as like a catch all to style all descendents of a parent container.
The child selector will select all the immediate children of a parent container. This is a selector I personally use more often then most and find it a bit more intuitive then some other selectors.
The child selector is in a lot of ways an expansion of the element selector where you are "digging deeper" to select internal elements of parents. All you need to do is use the > after the tag name, class name, etc. and then specify the child element/s that you wish to target. Direct children will be stylized. Any non-immediate children like grandchildren or great grandchildren will not be adjusted.
When you want to style the element immediately next to another you can use the adjacent sibling selector. Instead of the > you will want to use the + symbol.
Note: Elements cannot be in different containers and must have the same parent.
You can think of the general sibling selector as an expansion of the sibling selector. Instead of a + you will use a ~ and it will style all sibling elements that come after (not before) the element.
All paragraphs that follow the div element with the "warning" class will be styled. If there was a paragraph preceding the warning class it would not be styled.
With Pseudo-Classes you select elements based on their state, position, or characteristic. Let us breakdown what that means.
Essentially you'll specify the element/s followed by a colon and then some defining aspect of the element/s that you want to target. In a lot of ways it is similar to regular expressions (regex).
One of the most common ones I use the most frequently is the hover pseudo-class which is useful when you want to show a response to the client when they hover of an element like a button for instance before clicking it.
Below are all of the available pseudo-classes you can use when building out css rulesets.
A Pseudo-Element Selector visually is almost similar to a Pseudo-Class Selector except for an additional colon ":". The most common selector I see being used is ::before or ::after like when you want to add an special icon immediately before or after an element.
Result 💡 This is a paragraph with a pseudo-element.
When using before and after you'll need to specify the content you wish to add before or after the css element.
I also find it empowering to know the expanse of a topic and hope this kind is useful for knowing all about the different types of css selectors you can use. While it isn't required to memorize the css selectors I recommend practicing adding one new selector each time you build a new project so that you can expand your capacity for css. Happy styling!
No fluff. Just real projects, real value, and the path from code to cash — one useful build at a time.