Save time searching for the important CSS selectors.
A comprehensive guide can help web developers avoid common mistakes and adopt a consistent, efficient approach to using CSS selectors.
This guide is a one-stop for all of the most useful and popular CSS selectors for developers to reference.
CSS Selectors are used to target specific elements on the web page. In addition, selectors are the key to the whole process of styling and altering the web page and its elements.
There are several types of css selectors and you are going to learn almost all of them.
CSS class selector plays a very important role when we talk about layout or structure. So you can say it's a must-have selector in your code.
Key points:
Example:
Change the background color of all the elements with the class name "modal".
ID selectors are used to target an element on the page that is unique. It stands for "Identifier".
Key points:
Example:
Change the background color of the home page.
Universal selector (*) is a CSS selector that matches every element.
Key Points:
Example:
Reset all the browser default padding and margin to zero.
Consider it an optimization selector. You can group multiple selectors and apply common styles to them. Pretty useful.
Key Points:
Example:
There are four button variations. But we only need to apply border-radius to .btn-primary and .btn-secondary
Instead of doing this:
.btn-primary{ }
.btn-secondary{}
Use grouping CSS selector:
The element or type selector allows us to choose any HTML element with its name on the page and start styling it with CSS rules.
key Points:
Example:
Add font-family, font-size to the body element
The descendant combinator selector includes all elements that are children of the current element. In CSS, this means it affects any child node.
Key Points:
Example:
Select all the paragraph elements inside a container and add red color to them.
Child combinator CSS Selector selects all the specified direct children of the parent.
Key Points:
Example:
Add background-color to only those button elements which are direct children of .container
The adjacent sibling selector selects all of the siblings that are next to each other on the same line.
Key Points:
Example:
Add display: none; to property to all the direct (first) span elements which are next to div elements
All elements that are next siblings of the specified element are selected by the general sibling selector.
Key Points:
Example:
Add color:black; to all the specified next siblings(p) of element (div).
div ~ p {
color: black;
}
It selects the elements based on given attributes.
Key Points:
Syntax with examples:
You can utilize an attribute selector based on several different conditions and those conditions with examples are given below.
Add red color to all the anchor elements where href is equal to hash "#"
Add border-radius to all the img elements where alt tag contains a word "circle"
Quick Note: A value needs to be a whole word.
Add 500px of width to all the img elements where the alt tag contains a word "opt"
Quick Note: A value doesn't need to be a whole word
If there are 3 images with alt tags (optimize-img, optimizeimg, optimize-img). It will select all three images. Because each one contains opt.
Add the 500px width to all the img elements where alt tag begins with "optimize" value
Quick Note: A value needs to be a whole word
if there are 3 images with alt tags (optimize-img, optimizeimg, optimize-img). It will only select the first and third images. Because the second one is not a complete word.
Add the border-radius to all the img elements where alt tag begins with "radius" value
Quick Note: A value doesn't need to be a whole word
If there are 3 images with alt tags (radius-circle, radiuscircle, radius-circle). It will select all three images. Because all of them begin with radius.
Add the red color to all the paragraphs where class attribute value ends with alert.
Quick Note: The value does not have to be a whole word!
And Yeah! That's all for attribute css selectors.
As the name suggests, it selects the first child from the list of children.
Key Points:
Example:
Add margin-top at the first item of the categories list.
As the name suggests, it selects the last child from the list of children.
Key Points:
Example:
Add border-bottom at the end of the categories list (item).
It selects the first letter of the paragraph.
Key Points:
Example:
Increase the font size of the first letter of every paragraph.
Similar to the first letter, you can even select the first line of a paragraph.
Example:
Increase font weight of first line of every paragraph.
Similar to the :first-child and :last-child, it selects the element based on the given value.
Key Points:
Example:
Add background-color to the 5th children of 11 boxes.
It is essential to know about CSS selectors so that you can use them correctly in your projects.
In this tutorial, you learned about the most important CSS selectors, which you will see from time to time. You can always use it as a one-stop CSS Selectors cheat sheet.
Before you go, there is an extremely powerful conditional-based CSS selector which I didn't mention.
Don't worry, I have also got it covered in a stand-alone guide. Go ahead and look into :not css selector
Make sure to share this guide with others & subscribe to our newsletter for more quick web development tutorials.