A CSS selector is used to select the html elements so
that style can be applied on them. There can be multiple ways to select an element. External and
internal stylesheets cannot work without selectors.
There are various kind of selectors. Given below are the most common kinds of selectors which are
frequently used.
Before proceeding, take a small quiz to test your knowledge on CSS selectors. Then compare the result with after you finish learning and see how much you've improved.
Don't worry if you're a beginner and have scored less in this one. This is just a practice, get learning and improve your knowledge on this topic.
Code for reference:
<body>
<h3 id="h3-shows">SHOWS</h3>
<ul id="shows">
<li>Brooklyn Nine Nine</li>
<li>Suits</li>
<li>The Vampire Diaries</li>
<li>Money Heist</li>
<li class="red">Sherlock</li>
</ul>
<div id="sitcoms">
<p>Brooklyn Nine Nine</p>
<div class="B99">
<p class="red">Gina Linetti is the human version of the 100 emoji.</p>
<p>I want it that way - Now number 5</p>
</div>
<p>Friends</p>
<div class="friends">
<p>Joey doesn't share food</p>
<p>I don't even have a 'pla'</p>
</div>
</div>
<p>This is a new Para</p>
<div id="links">
<a href="https://google.com" target="_blank">Click here</a>
</div>
</body>
This is used to select the elements which have the same
tag name. For eg, if you all want all h3 tags to be styled in one way, you can use this type of
selector. This is used by just specifying the tagname alone.
Understanding using an
example:
h3 {
background-color: lightpink;
}
To select an element by ID, the '#' symbol followed by the ID name is
used in the stylesheet. This is used when a particular element with a unique ID has to be
styled.
Understanding using an example:
#h3-shows {
background-color: lightgreen;
}
To select an element by class, the '.' symbol followed by the class
name is used in the stylesheet. This is used when all the elements with the specified class name
have to be styled.
Note : it's not necessary for the classes to have the same element tags
(Eg: h3, div, h4, etc.). The classes can be used in various elements.
Understanding using an
example:
.red {
color: red;
}
Now if you want a particular class of a particular tag to be selected, you can do that by giving
tagname, followed by '.' , followed by class name.
Understanding using an example:
li.red {
color: red;
}
To give a particular style in all the tag names, the universal
selector which is denoted by '*' is used.
Understanding using an example:
* {
background-color: lightgrey;
}
If you want to apply the same styling to various elements, you can group them using commas ','.
Understanding using an example:
.B99, .friends {
color: violet;
}
This selects all elements that are children of a specified element. These elements need not
necessarily be direct children of the parent elements.
Understanding using an example:
#sitcoms p {
color: olivedrab;
}
This selects all elements that are DIRECT children of a specified element. The selectors can be in
the form of tag names, class names or IDs.
Understanding using an example:
#sitcoms > p {
color: olivedrab;
}
This selector selects the element which is directly after the element specified.
Understanding using an example:
.B99 + p {
background-color: yellow;
}
div + p {
background-color: yellow;
}
These are used in various elements. Changes the style when the user hovers the mouse on the
particular element.
Understanding using an example:
a:hover {
color: orange;
}

These are used by the anchor tag to differentiate between the different states.
Understanding
using an example:
a:visited {
color: red;
}

They are are used to select the first child and last child element respectively.
Understanding
using an example:
p:first-child {
color: blue;
}

They are used to select the nth number of child element from the start and nth number of child
element from the end respectively. The number of the position should be specified in the
brackets.
Understanding using an example:
li:nth-child(3) {
background-color: teal;
}
Note: To select every alternate element, you can specify 2n (even numbered elements) or 2n+1 (odd
numbered elements) accordingly.
Understanding using
an example:
li:nth-child(2n) {
background-color: teal;
}
Changes the first line of every element specified. Used mostly in paragraphs
Changes the first letter of every element specified.
Understanding using an example:
li::first-letter {
color: white;
background-color: black;
}
Let's see how well you've understood CSS selectors. Take a quiz to test your knowledge 💡
If your score isn't up to the mark, make sure to go through the concepts again and keep practising!