CSS Tutorial

CSS Selectors

Targeting elements with precision and style

What are Selectors?

CSS Selectors are used to "find" (or select) the HTML elements you want to style. Without selectors, CSS would have no way of knowing which part of the page you want to change.
css
/* The Selector targets all <p> elements */
p {
  color: blue;
}

Basic Selectors

TypeSyntaxDescription
Elementh1Targets all <h1> tags
Class.btnTargets all elements with class="btn"
ID#headerTargets the single element with id="header"
Universal*Targets every single element on the page

Combinators

Combinators allow you to select elements based on their relationship to other elements.
  • div p (Descendant): All <p> inside a <div>.
  • div > p (Child): All <p> that are direct children of a <div>.
  • div + p (Adjacent Sibling): The <p> placed immediately after a <div>.

Pseudo-classes

Wait! Pseudo-classes allow you to style an element based on its state (like when a user hovers over it).
css
button:hover {
  background-color: darkblue;
}

li:nth-child(even) {
  background-color: #f2f2f2;
}

Specificity

Wait! If two selectors target the same element, the browser uses a scoring system called Specificity to decide which one "wins."
  1. ID (Score: 100)
  2. Class (Score: 10)
  3. Element (Score: 1)
[!TIP] Always try to use classes (.btn) for styling. Avoid using IDs (#btn) because their high specificity makes them very hard to override later.

Summary

TermDescription
SelectorThe pattern used to target elements
DeclarationThe property and value (e.g., color: red;)
SpecificityThe weight assigned to a selector
Pseudo-classStyle based on state (hover, active, focus)
Pseudo-elementStyle a specific part of an element (::before, ::after)
Best ForControlling exactly which elements get styled
Logic"Targeting and styling"
SafetyKeep specificity low to avoid "CSS Wars"
ModernThe :has() selector is the new "Parent Selector"
StandardUse kebab-case for class names
IdentityThe "C" in CSS (Cascading) is driven by selectors