CSS Selectors

Element, class, id, universal and group selectors.

✍️ 3 exercises + code challenge in this lesson

Selectors decide which elements a rule applies to:

  • Element: p selects all paragraphs.
  • Class: .intro selects elements with class="intro".
  • Id: #logo selects the element with id="logo".
  • Universal: * selects every element.
  • Grouping: h1, h2, p applies one rule to several selectors.
  • Element + class: p.intro selects only paragraphs with that class.

Key Takeaways: CSS Selectors

Mastering CSS Selectors helps you write clean and reliable CSS code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

CSS Selectors Code Example

CSS
<style>
  *        { font-family: Arial, sans-serif; }
  h1, h2   { color: #071A2F; }
  .intro   { font-size: 20px; color: #2563EB; }
  p.note   { background: #FEF9C3; padding: 6px; }
  #special { border: 2px dashed #E34F26; padding: 6px; }
</style>

<h1>Heading 1</h1>
<h2>Heading 2</h2>
<p class="intro">An intro paragraph.</p>
<p class="note">A paragraph with class note.</p>
<p id="special">The one with the id.</p>
Free sign-in required · Google login available

CSS Selectors Practice Exercises

Check what you learned in “CSS Selectors”.

/ 3 solved
Fill in the blank

Select the element with id "logo".

 {
  width: 120px;
}
✓ Correct! Not quite. Try again.
Multiple choice

Which selector selects all elements with class="note"?

✓ Correct! Not quite. Try again.
Code challenge

Make the paragraph with the class "warning" red, without changing the other paragraph.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services