Skip to content

Courses / CSS3 Mastery: Beginner to Advanced / Basic Selectors

2,080 people viewed this

Basic Selectors

CSS Basic Selectors

Learning Objectives

  • Understand element, class, ID and universal selectors.
  • Apply styles to individual or multiple HTML elements.
  • Understand selector specificity.
  • Use selectors to write maintainable CSS.

1. Element Selector

An element selector targets every HTML element of a specified type.

p { color: blue; }
h1 { font-size: 32px; }
button { background-color: green; }

2. Class Selector

A class selector begins with a dot and matches elements containing that class.

.highlight { background-color: yellow; }
.card { padding: 20px; border: 1px solid #ccc; }
<p class="highlight">Highlighted text</p>
<div class="card">Card content</div>

A class can be reused on multiple elements, and an element can have multiple classes.

3. ID Selector

An ID selector begins with a hash. An ID should uniquely identify an element within an HTML document.

#main-title { color: darkred; }
<h1 id="main-title">Welcome</h1>

4. Universal Selector

The universal selector matches all elements within its scope.

* { box-sizing: border-box; }

5. Combining Selectors

p.note { color: purple; }
.card.active { border-color: green; }

The first selector matches paragraph elements with the note class. The second matches elements with both card and active classes.

Specificity Basics

  • Element selectors contribute to the type-selector column.
  • Class selectors contribute to the class column.
  • ID selectors contribute to the ID column.
  • The universal selector contributes no specificity.

Specificity is not the only cascade factor. Importance, origin and cascade layers are considered before specificity.

Rate this course

No ratings yet — yours can be the first.

Stars show on this course. Your written note stays private.

Your stars