DEV Community

Mohd Asif Ansari
Mohd Asif Ansari

Posted on

CSS Selectors 101

You've written some HTML. Now you want to make it look good with CSS. But how do you tell CSS which elements to style?

That's where selectors come in.

Why Do We Need CSS Selectors?

Imagine you're in a classroom and you want to give instructions:

Without selectors:
"Everyone, stand up!"

With selectors:
"Students wearing red shirts, stand up!"
"Student named John, stand up!"
"Students in row 3, stand up!"

CSS selectors work the same way. They let you target specific HTML elements and apply styles to them.

Without selectors: You can't style anything
With selectors: You control exactly what gets styled

Basic HTML for Our Examples

Let's use this HTML throughout the blog:

<body>
  <h1>Welcome</h1>
  <p>This is a paragraph.</p>
  <p class="highlight">This is highlighted.</p>
  <p id="special">This is special.</p>
  <div class="box">
    <p>Paragraph inside box</p>
  </div>
</body>
Enter fullscreen mode Exit fullscreen mode

Now let's see how to select different parts.

1. Element Selector

The element selector targets all elements of a specific type.

Syntax:

tagname {
  /* styles */
}
Enter fullscreen mode Exit fullscreen mode

Example:

p {
  color: blue;
}
Enter fullscreen mode Exit fullscreen mode

What this does:
Targets ALL <p> elements and makes them blue.

Result:

  • "This is a paragraph." → Blue
  • "This is highlighted." → Blue
  • "This is special." → Blue
  • "Paragraph inside box" → Blue

Use when: You want to style all elements of one type.

More Examples

Style all headings:

h1 {
  font-size: 32px;
  color: darkblue;
}
Enter fullscreen mode Exit fullscreen mode

Style all divs:

div {
  background-color: lightgray;
  padding: 10px;
}
Enter fullscreen mode Exit fullscreen mode

2. Class Selector

The class selector targets elements with a specific class.

Syntax:

.classname {
  /* styles */
}
Enter fullscreen mode Exit fullscreen mode

Notice the dot (.) before the class name.

Example:

.highlight {
  background-color: yellow;
}
Enter fullscreen mode Exit fullscreen mode

What this does:
Targets only <p class="highlight"> and gives it a yellow background.

Result:

  • "This is highlighted." → Yellow background
  • Other paragraphs → No yellow background

Use when: You want to style specific elements that share a purpose or style.

Multiple Classes

An element can have multiple classes:

<p class="highlight bold">Text</p>
Enter fullscreen mode Exit fullscreen mode

Target it:

.highlight {
  background-color: yellow;
}

.bold {
  font-weight: bold;
}
Enter fullscreen mode Exit fullscreen mode

Both styles apply!

Classes vs Elements

Element selector (all paragraphs):

p {
  color: blue;
}
Enter fullscreen mode Exit fullscreen mode

Every <p> becomes blue.

Class selector (specific paragraphs):

.highlight {
  color: red;
}
Enter fullscreen mode Exit fullscreen mode

Only <p class="highlight"> becomes red.

3. ID Selector

The ID selector targets a single unique element.

Syntax:

#idname {
  /* styles */
}
Enter fullscreen mode Exit fullscreen mode

Notice the hash (#) before the ID name.

Example:

#special {
  font-size: 20px;
  color: red;
}
Enter fullscreen mode Exit fullscreen mode

What this does:
Targets only <p id="special"> and makes it red with larger text.

Result:

  • "This is special." → Red, 20px font
  • Other paragraphs → No change

Important: IDs should be unique. Only ONE element should have a specific ID.

Class vs ID

Class:

  • Use for multiple elements
  • Starts with .
  • Example: class="button"

ID:

  • Use for one unique element
  • Starts with #
  • Example: id="header"

HTML example:

<!-- Multiple elements can share a class -->
<button class="btn">Click</button>
<button class="btn">Submit</button>

<!-- Only one element should have an ID -->
<div id="main-content">...</div>
Enter fullscreen mode Exit fullscreen mode

CSS:

.btn {
  background-color: blue;
}

#main-content {
  width: 80%;
}
Enter fullscreen mode Exit fullscreen mode

4. Group Selectors

Group selectors let you apply the same styles to multiple selectors.

Syntax:

selector1, selector2, selector3 {
  /* styles */
}
Enter fullscreen mode Exit fullscreen mode

Use commas to separate selectors.

Example:

h1, h2, h3 {
  color: navy;
  font-family: Arial;
}
Enter fullscreen mode Exit fullscreen mode

What this does:
All <h1>, <h2>, and <h3> elements get navy color and Arial font.

Another example:

p, .highlight, #special {
  line-height: 1.5;
}
Enter fullscreen mode Exit fullscreen mode

All paragraphs, elements with class "highlight", AND the element with id "special" get line-height of 1.5.

Use when: Multiple different selectors need the same styling.

5. Descendant Selectors

Descendant selectors target elements inside other elements.

Syntax:

parent child {
  /* styles */
}
Enter fullscreen mode Exit fullscreen mode

Use a space between selectors.

Example:

div p {
  color: green;
}
Enter fullscreen mode Exit fullscreen mode

What this does:
Targets <p> elements that are inside <div> elements.

Given this HTML:

<p>Outside div</p>
<div>
  <p>Inside div</p>
</div>
Enter fullscreen mode Exit fullscreen mode

Result:

  • "Outside div" → Not green (not inside a div)
  • "Inside div" → Green (inside a div)

More Examples

Target links inside navigation:

nav a {
  color: white;
  text-decoration: none;
}
Enter fullscreen mode Exit fullscreen mode

Target paragraphs inside a specific class:

.box p {
  font-size: 14px;
}
Enter fullscreen mode Exit fullscreen mode

Deeper nesting:

div ul li {
  list-style: square;
}
Enter fullscreen mode Exit fullscreen mode

Targets <li> elements inside <ul> inside <div>.

Combining Selectors

You can combine different selector types for precision.

Class inside element:

div .highlight {
  background-color: yellow;
}
Enter fullscreen mode Exit fullscreen mode

Targets elements with class "highlight" inside <div>.

Element with class:

p.highlight {
  color: red;
}
Enter fullscreen mode Exit fullscreen mode

Targets <p> elements that have class "highlight" (no space between p and .highlight).

ID inside element:

section #special {
  font-weight: bold;
}
Enter fullscreen mode Exit fullscreen mode

Selector Priority (Specificity)

What happens if multiple selectors target the same element?

Simple rule: More specific selector wins.

Priority order (low to high):

  1. Element selectors (p)
  2. Class selectors (.highlight)
  3. ID selectors (#special)

Example:

<p class="text" id="intro">Hello</p>
Enter fullscreen mode Exit fullscreen mode
p {
  color: blue;
}

.text {
  color: green;
}

#intro {
  color: red;
}
Enter fullscreen mode Exit fullscreen mode

Result: Text is red because ID has highest priority.

Simple to remember:

  • ID beats class
  • Class beats element
  • More specific beats less specific

Practical Examples

Example 1: Simple Blog Post

/* All paragraphs */
p {
  line-height: 1.6;
  color: #333;
}

/* Main heading */
h1 {
  font-size: 36px;
  color: #000;
}

/* Highlighted quotes */
.quote {
  background-color: #f0f0f0;
  border-left: 4px solid blue;
  padding: 10px;
}

/* Author section */
#author {
  font-style: italic;
  color: gray;
}
Enter fullscreen mode Exit fullscreen mode

Example 2: Navigation Menu

/* Style all nav links */
nav a {
  color: white;
  text-decoration: none;
  padding: 10px;
}

/* Active link */
.active {
  background-color: blue;
}
Enter fullscreen mode Exit fullscreen mode

Example 3: Card Layout

/* All cards */
.card {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 20px;
}

/* Card titles */
.card h2 {
  color: #333;
  margin-bottom: 10px;
}

/* Card paragraphs */
.card p {
  color: #666;
  line-height: 1.5;
}
Enter fullscreen mode Exit fullscreen mode

Before and After Examples

HTML:

<h1>My Website</h1>
<p>Welcome to my site.</p>
<p class="important">This is important!</p>
Enter fullscreen mode Exit fullscreen mode

Before CSS (no styling):

My Website
Welcome to my site.
This is important!
Enter fullscreen mode Exit fullscreen mode

After CSS:

h1 {
  color: blue;
  font-size: 32px;
}

p {
  color: gray;
}

.important {
  color: red;
  font-weight: bold;
}
Enter fullscreen mode Exit fullscreen mode

Result:

  • "My Website" → Blue, 32px
  • "Welcome to my site." → Gray
  • "This is important!" → Red, bold

Quick Reference

Selector Syntax Example What it targets
Element tagname p All <p> elements
Class .classname .highlight Elements with class="highlight"
ID #idname #header Element with id="header"
Group sel1, sel2 h1, h2 All h1 AND all h2
Descendant parent child div p <p> inside <div>

Common Beginner Mistakes

1. Forgetting the dot for classes

/* Wrong */
highlight {
  color: red;
}

/* Right */
.highlight {
  color: red;
}
Enter fullscreen mode Exit fullscreen mode

2. Forgetting the hash for IDs

/* Wrong */
header {
  width: 100%;
}

/* Right */
#header {
  width: 100%;
}
Enter fullscreen mode Exit fullscreen mode

3. Using IDs for multiple elements

<!-- Wrong: Same ID used twice -->
<div id="box">Box 1</div>
<div id="box">Box 2</div>

<!-- Right: Use classes -->
<div class="box">Box 1</div>
<div class="box">Box 2</div>
Enter fullscreen mode Exit fullscreen mode

Summary

CSS Selectors = Ways to target HTML elements for styling

Main types:

  • Elementp (all paragraphs)
  • Class.highlight (elements with that class)
  • ID#special (one unique element)
  • Grouph1, h2, h3 (multiple selectors)
  • Descendantdiv p (p inside div)

Priority:
ID > Class > Element

Remember:

  • Use . for classes
  • Use # for IDs
  • Use space for descendant selectors
  • Use comma for group selectors

Selectors are the foundation of CSS. Master these, and you can style anything!

Top comments (0)