Back to Blogs
CSS

CSS Selectors 101: Targeting Elements with Precision

P
Punyansh Singla
January 26, 20266 min read
CSSCSS SelectorsWeb DevelopmentFrontendHTMLBeginner Guide

CSS Selectors 101: Targeting Elements with Precision

Why CSS selectors are needed

CSS selectors are the foundation of styling. CSS selectors are a way of selecting HTML elements that we want to style.

1. Element selector

Element selector selects all HTML elements of the same type.

“paint all the houses”

css
p {
    color: black;
}
  • every <p></p> becomes black

2. Class selector

It is a way of selecting all elements with a specific class and styling them.

“paint all the houses whose doors are blue”

css
.bg-aquamarine {
    background-color: aquamarine;
}
html
<div class="bg-aquamarine">Box 1</div>
<section class="bg-aquamarine">Box 2</section>
  • both elements will get an aquamarine background

3. ID selector

It is a way of selecting an element with a specific ID. An ID can be assigned to only one element.

“paint this house”

css
#paragraph {
    color: red;
}
html
<p id="paragraph">Important text</p>
  • only this paragraph color will become red

4. Group selectors

It is a way of selecting different HTML elements and applying the same style.

“paint all houses whose doors are blue and whose doors are red”

css
h1, h2, h3 {
    font-family: sans-serif;
}

5. Descendant selectors

It lets you apply style to an element that is inside another element.

“paint the door inside that house”

css
h1 p {
    color: chartreuse;
}
html
<h1>
    <p>This text will be chartreuse</p>
</h1>
<p>This text will NOT change</p>
  • only the paragraph inside h1 will be colored chartreuse

6. Universal selector (*)

This selects all the elements in HTML.

css
* {
    color: aqua;
}
html
<div>This is a div</div>
<p>This is a paragraph</p>
<h1>This is a header</h1>
  • all text will be aqua

7. General sibling selector (~)

It is used to select all the siblings of an element.

html
<div>
    <h1>Main Title</h1>
    <p>Paragraph 1 (This will be styled)</p>
    <span>Some other element</span>
    <p>Paragraph 2 (This will be styled)</p>
    <p>Paragraph 3 (This will be styled)</p>
</div>

<section>
    <h1>Again a h1</h1>
    <p>Paragraph 1 (This will be styled because it is a sibling of h1)</p>
</section>
css
h1 ~ p {
    color: blue;
    font-weight: bold;
}

8. Adjacent sibling selector (+)

This selects the element that is just after a specific element.

css
h2 + p {
    color: red;
}
html
<div>
    <h2></h2>
    <p>This p color will be red because it is just after the h2</p>
    <p>This p color will not be red</p>
</div>

9. Pseudo selector

It allows you to select an element when it is in a specific condition, like styling an input when the user hovers over it.

css
input:hover {
    color: blue;
}

There are two types of pseudo selectors -

  1. Pseudo-class selector

This is used to select a specific state of an existing element. They are denoted by : (single colon).

Top 5 pseudo-class selectors:

a. :hover

css
input {
    color: yellow;
}
input:hover {
    color: blue;
}

b. :focus

css
input:focus {
    color: aquamarine;
}

c. :link

css
a:link {
    color: pink;
}

d. :visited

css
a:visited {
    color: yellow;
}

e. :checked

css
input[type="checkbox"]:checked {
    accent-color: red;
}

2.Pseudo-element selector

These are used to design a specific part of an element. They are denoted by :: (double colon).

a. ::first-line

css
p::first-line {
    color: red;
}

b. ::first-letter

css
p::first-letter {
    color: red;
}

c. ::placeholder

css
input::placeholder {
    color: red;
}

Basic selector priority (very high level)

From lowest to highest -

  • Element selector
  • Class selector
  • ID selector
css
p {
    color: blue;
}
.text {
    color: green;
}
#main {
    color: red;
}
html
<p id="main" class="text">Hello</p>
  • the paragraph color will be red

What if selectors are the same and written multiple times?

The CSS property written last will be applied.

css
.bg {
    background-color: black;
}
.bg {
    background-color: white;
}
html
<div class="bg"></div>
  • background color of the div will be white

Enjoyed this article?

Share it with your network

Punyansh's AssistantOnline
AI

Hey there! 👋 I'm Punyansh's AI assistant. Feel free to ask me anything about his skills, projects, or experience!

20 LEFT