Skip to content

HTML Tutorial HTML and CSS

CSS Selectors for HTML: Targeting Elements

Lesson 40 of 81 2 min read By Deepak Paswan Updated Oct 4, 2026

A selector says which HTML elements a rule applies to. These are the ones you will use most.

SelectorMatchesExample
ElementAll elements of that typep
ClassElements with that class.note
IdThe element with that id#intro
DescendantElements inside anotherarticle p
GroupSeveral selectors at onceh1, h2
index.html
HTML
<style>
  p { color: #49454f; }
  .note { background: #fff3ec; padding: 6px; }
  #intro { font-size: 1.25em; }
  article em { color: #b84624; }
  h1, h2 { font-family: Georgia, serif; }
</style>

<h1>Notes</h1>
<p id="intro">Welcome.</p>
<p class="note">A note.</p>
<article><p>Some <em>emphasis</em> inside an article.</p></article>

When rules disagree: the cascade

If two rules set the same property on the same element, CSS picks one.

  1. A more specific selector wins. An id beats a class, and a class beats an element.
  2. If specificity is equal, the later rule wins.
  3. Inline style beats stylesheet rules.
index.html
HTML
<style>
  p { color: grey; }
  .warning { color: firebrick; }
</style>
<p class="warning">This paragraph is firebrick, not grey.</p>
lightbulb
Prefer classes
Classes are specific enough to be useful and flexible enough to reuse. Ids are very specific and hard to override later.