Skip to main content

Command Palette

Search for a command to run...

CSS Selectors 101: Targeting Elements with Precision

Published
•3 min read•View as Markdown

If HTML is the skeleton of your website, CSS (Cascading Style Sheets) is the wardrobe. But how does the code know which specific bone to dress?

That’s where Selectors come in. A CSS selector is the part of a CSS rule that tells the browser which HTML elements should receive specific styles. Mastering selectors is the difference between a messy stylesheet and a clean, professional codebase.


The Anatomy of a CSS Rule

Before we dive into the types, let's look at how a selector fits into a standard CSS rule:

/* Selector { Property: Value; } */
p {
  color: blue;
  font-size: 16px;
}

In this example, p is the selector. It targets every <p> element on the page and turns the text blue.


1. The Basic Selectors

These are the foundation of all web styling. You will use these 90% of the time.

A. Element (Type) Selector

Targets elements by their HTML tag name.

  • Use case: Setting global styles like fonts for paragraphs or margins for headings.
h1 {
  text-align: center;
}

B. Class Selector (.)

Targets elements with a specific class attribute. Classes are reusable; you can apply the same class to multiple elements.

  • Use case: Styling multiple buttons or cards to look the same.
.btn-primary {
  background-color: green;
  border-radius: 5px;
}

C. ID Selector (#)

Targets a unique element with a specific id attribute. Important: An ID should only be used once per page.

  • Use case: Styling a unique element like a "Navigation Bar" or "Main Logo.”
#main-header {
  background-color: #333;
}

2. The Relationship Selectors

Sometimes you only want to style an element if it’s inside another specific element. This is called combinators.

Descendant Selector (Space)

Targets all instances of an element that are anywhere inside a parent.

/* Styles any <span> that is inside a <div> */
div span {
  font-weight: bold;
}

Child Selector (>)

Targets only the direct children of a parent. It doesn't reach further down into nested elements.

/* Styles only the <li> tags directly inside this <ul> */
ul > li {
  list-style-type: square;
}

3. State-Based Selectors (Pseudo-classes)

What if you want to change an element's look when someone interacts with it? We use Pseudo-classes (indicated by a colon :).

  • :hover: Styles an element when the user’s mouse is over it.

  • :nth-child(): Targets an element based on its position (e.g., every second item in a list).

/* Changes button color when hovered */
button:hover {
  cursor: pointer;
  opacity: 0.8;
}

/* Colors every even-numbered list item */
li:nth-child(even) {
  background-color: #f2f2f2;
}

Selector Specificity: Who Wins?

What happens if you tell a paragraph to be red using a class, but blue using an ID? CSS has a ranking system called Specificity.

  1. ID Selectors (Highest power)

  2. Class Selectors

  3. Element Selectors (Lowest power)

Pro-tip: If your CSS isn't applying, it’s usually because another selector has higher specificity!


Summary Table

Selector Name

Syntax

Example

Element

tag

h1 { ... }

Class

.classname

.card { ... }

ID

#idname

#logo { ... }

Descendant

A B

nav a { ... }

Hover

:hover

a:hover { ... }


Ready to practice?

The best way to see selectors in action is to experiment. Open any website, right-click an element, select Inspect, and look at the "Styles" pane on the right. You'll see exactly which selectors are currently dressing that element!