Skip to main content

Command Palette

Search for a command to run...

CSS Selectors

Published
•1 min read•View as Markdown

What we are studying in this article:

  1. Why CSS selectors are needed

  2. Element selector

  3. Class selector

  4. ID selector

  5. Group selectors

  6. Descendant selectors

  7. Basic selector priority (very high level)

1. Why CSS selectors are needed

CSS selectors are used to select HTML elements so we can apply styles to them.

Without selectors, CSS wouldn’t know which element to style.

this tells the browser make all <p> elements blue.

2. Element Selector

An element selector targets all HTML elements of a specific type

styles all <h1> elements.

3. Class Selector

A class selector targets elements that have a specific class attribute

HTML

CSS

4. ID Selector

An ID selector targets one unique element.

HTML

CSS

5. Group Selector

Group selectors apply the same styles to multiple selectors.

CSS

Reduced repeated CSS

6. Descendant Selector

A descendant selector selects elements inside another element.

style <p> elements that are inside a <div>.

7. Basic Selector priority

When multiple CSS rules target the same element, priority (specificity) decides which one applies.

Simple priority order (low → high):

  1. Element selector

  2. Class selector

  3. ID selector

  4. Inline styles