On this page10 sections
  1. Learning Objectives
  2. 1. Element, Class and ID Selectors
  3. 2. Grouping With Commas
  4. 3. Combining Selectors
  5. 4. Attribute Selectors
  6. 5. Specificity: Which Selector Wins
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 002: Selectors & Combinators

Category1: CSS Basics
Lecture002 of 057
Estimated video length14 min
TargetCSS3 · any modern browser
LevelBeginner

A selector answers one question: which elements should this rule style? Pick all paragraphs, only the ones with a class, one special element by id, or links inside the navigation. Selectors are how you aim your styles.


Learning Objectives

  • Select by element, class, id and the universal selector
  • Group selectors with commas
  • Combine them: descendant, child, and element-with-class
  • Select by attribute
  • Explain specificity: why some selectors win

1. Element, Class and ID Selectors

SelectorMatchesExample
pEvery <p>p { … }
.priceEvery element with class="price".price { … }
#logoThe one element with id="logo"#logo { … }
*Every element* { … }

Example

html
<style>
  p { color: #444; }
  .price { color: crimson; font-weight: bold; }
  #site-title { color: teal; }
</style>

<h1 id="site-title">Shoe Shop</h1>
<p>Running shoes</p>
<p class="price">Rs 6,500</p>
<p>Leather sandals</p>
<p class="price">Rs 3,200</p>

An element can have several classes, separated by spaces: class="price sale".


2. Grouping With Commas

Give several selectors the same styles:

Example

html
<style>
  h1, h2, h3 { font-family: Georgia, serif; color: #2b5797; }
</style>

<h1>Main title</h1>
<h2>Section</h2>
<h3>Sub-section</h3>

3. Combining Selectors

SelectorMatches
nav aEvery <a> anywhere inside a <nav> (descendant)
ul > li<li> that are direct children of a <ul>
p.note<p> elements that have the class note
h2 + pThe <p> straight after an <h2>

Example

html
<style>
  nav a { color: white; background: #4c6ef5; padding: 4px 10px; text-decoration: none; }
  p.note { background: #fff3bf; padding: 6px; }
  h2 + p { font-weight: bold; }
</style>

<nav><a href="#">Home</a> <a href="#">Shop</a></nav>
<a href="#">This link is outside the nav, so it stays plain.</a>

<h2>Delivery</h2>
<p>This first paragraph after the heading is bold.</p>
<p>This one is normal.</p>
<p class="note">A paragraph with the note class.</p>

4. Attribute Selectors

Select by an attribute and its value:

Example

html
<style>
  input[type="email"] { border: 2px solid #4c6ef5; }
  a[target="_blank"]::after { content: " ↗"; }
</style>

<input type="text" placeholder="Name">
<input type="email" placeholder="Email (blue border)">
<p><a href="https://example.com" target="_blank">Opens in a new tab</a></p>

5. Specificity: Which Selector Wins

When two rules conflict, the more specific one wins, wherever it is in the file:

output
id  (#title)        strongest
class (.title)      ↓
element (h1)        weakest

Example

html
<style>
  #headline { color: green; }
  .big { color: orange; }
  h1 { color: blue; }
</style>

<h1 id="headline" class="big">I am green: the id wins.</h1>
<h1 class="big">I am orange: the class beats the element.</h1>
<h1>I am blue.</h1>

Use classes for almost everything. They're reusable and easy to override. Keep ids for links (#section) and JavaScript.


Common Beginner Mistakes

1. Forgetting the dot or hash. price { } looks for a <price> element. Classes need .price and ids need #price.

2. Spaces that change meaning. p.note = a paragraph with the class. p .note = an element with the class inside a paragraph.

3. Styling with ids everywhere. Their high specificity makes later changes hard.

4. Class names with spaces. class="sale price" is two classes, sale and price.


Practice Questions

  1. Write a selector for every element with the class card.
  2. Write a selector for the element with the id menu.
  3. How do you give h1 and h2 the same rule in one go?
  4. What's the difference between nav a and nav > a?
  5. An element has both id="x" and class="y". #x says red and .y says blue. Which colour wins?
  6. Write a rule that gives every input of type password a red border.

Practice Answers

  1. .card
  2. #menu
  3. h1, h2 { … }
  4. nav a matches links anywhere inside the nav; nav > a only matches links that are direct children.
  5. Red. An id is more specific than a class.
  6. input[type="password"] { border: 2px solid red; }

Key Takeaways

  • p (element), .class, #id, * (everything) are the basic selectors.
  • Commas group selectors; spaces, > and + combine them.
  • [attribute="value"] selects by attribute.
  • Specificity: id > class > element. Prefer classes.