agyanadda@gmail.com

CSS Selectors

CSS Basics 1 min read Updated Dec 1, 2026 Topic 5 of 39

A selector chooses which HTML elements a rule styles.

Selector Example Selects
Element p all <p> elements
Class .note elements with class="note"
ID #header the element with id="header"
Universal * every element
Group h1, h2 all h1 and h2
Descendant nav a links inside nav
Child ul > li li that are direct children of ul
Attribute input[type="text"] text inputs

Class vs ID

A class can be used on many elements. An ID must be unique on the page.

Example

Example — Common selectors HTML
<style>
  p { color: #333; }
  .note { background: #fff7d6; padding: 8px; }
  #title { color: #1f386b; }
  ul > li { color: green; }
</style>

<h2 id="title">Selectors</h2>
<p>Normal paragraph.</p>
<p class="note">Paragraph with class note.</p>
<ul>
  <li>Direct child item</li>
</ul>

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which selector targets class="box"?

Exercise 2 Fill in the Blank

An ID selector starts with the ___ symbol.

Exercise 3 True / False

The same ID can be used on many elements.

Frequently Asked Questions

Yes. Separate them with spaces: class="card featured". Both .card and .featured rules apply.