Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials CSS CSS Selectors
CSS Beginner FREE

CSS Selectors

Lesson 2 of 16 Beginner Interactive

Selectors are patterns used to target HTML elements for styling. Understanding selectors is fundamental to writing effective CSS.

Basic Selectors

  • Element Selector — targets all elements of a type: p, h1, div
  • Class Selector — targets elements with a class attribute: .classname
  • ID Selector — targets a single element with a specific ID: #idname
  • Universal Selector — targets all elements: *
  • Grouping Selector — applies the same styles to multiple selectors: h1, h2, h3

Combinator Selectors

  • Descendantdiv p (all p inside div)
  • Childdiv > p (direct children only)
  • Adjacent Siblingh1 + p (first p after h1)
  • General Siblingh1 ~ p (all p after h1)

Syntax

CSS
/* Element Selector */
p { color: #333; }

/* Class Selector */
.highlight { background-color: yellow; }

/* ID Selector */
#header { font-size: 24px; }

/* Universal Selector */
* { box-sizing: border-box; }

/* Grouping Selector */
h1, h2, h3 { color: #1e3a8a; }

/* Descendant Combinator */
.sidebar a { color: blue; }

/* Child Combinator */
.nav > li { display: inline-block; }
CSS Selector Types
HTML
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>CSS Selector Types</title>
    <style>
        p { color: #333; line-height: 1.6; }                 /* element selector */
        .highlight { background-color: #fff3cd; padding: 2px 6px; } /* class selector */
        #main-title { font-size: 28px; color: #2c3e50; }      /* ID selector */
        .card p { color: #666; font-size: 14px; }             /* descendant selector */
        a:hover { color: #e74c3c; }                           /* pseudo-class */
        h2, h3 { font-family: Georgia, serif; }               /* group selector */
    </style>
</head>
<body>
    <h1 id="main-title">CSS Selector Types</h1>
    <p>Element selector p sets color: #333 and line-height: 1.6.</p>
    <p class="highlight">Class selector .highlight sets background-color: #fff3cd.</p>
    <div class="card"><p>Descendant .card p sets color: #666 and font-size: 14px.</p></div>
    <p>Hover the <a href="#">link</a> to see a:hover -> color: #e74c3c.</p>
    <h2>Group selector styles this h2 (Georgia serif).</h2>
</body>
</html>

Practice

1
Exercise

Select all <p> elements inside a div with class "content" and make them blue.

Answer
.content p {
    color: blue;
}
2
Exercise

Group h1, h2, and h3 selectors to apply the same font family.

Answer
h1, h2, h3 {
    font-family: Arial, sans-serif;
}
3
Exercise

Use an ID selector to style a unique header with id="page-header".

Answer
#page-header {
    background: #1e3a8a;
    color: white;
    padding: 20px;
}

Quick Quiz

1

Which selector has the highest specificity?

ID selectors have higher specificity than class and element selectors.

2

What does the selector "div > p" target?

The > combinator targets only direct children, not all descendants.

3

How do you select multiple different elements with the same style?

Comma-separated selectors apply the same declarations to all listed selectors.

Interview Questions

Specificity determines which CSS rule wins when multiple rules target the same element. It is calculated as a tuple (a, b, c) where a = number of ID selectors, b = number of class/attribute/pseudo-class selectors, c = number of element/pseudo-element selectors. Higher specificity wins. Inline styles override all, except !important.

A class can be applied to multiple elements on a page, while an ID must be unique. IDs have higher specificity (0,1,0) compared to classes (0,0,1). Use IDs for unique elements and classes for reusable styles. In practice, prefer classes for styling to avoid specificity issues.