CSS Basics

CSS Selectors

Master the art of targeting HTML elements with precision using CSS selectors. Learn element, class, ID, attribute, and advanced selector techniques.

What are CSS selectors?

A selector is the pattern that chooses which HTML elements a rule styles. The basic ones are the type selector (p), the class selector (.card), the ID selector (#main) and the universal selector (*). You can combine them, select by attributes ([type="email"]), by state (:hover) and by relationship (nav a).

p          { }   /* every <p> */
.card      { }   /* class="card" */
#header    { }   /* id="header" */
nav a      { }   /* <a> inside <nav> */
ul > li    { }   /* direct children only */
a:hover    { }   /* state */
input[type="email"] { }   /* attribute */

CSS Selector Types

CSS Selectors are patterns used to select and target specific HTML elements for styling. They define which elements in the document tree will be affected by the CSS rules, ranging from simple element names to complex attribute-based patterns.

Element

Basic

Target all elements of a specific HTML tag

h1, p, div, span

Class

Common

Target elements with specific class attributes

.button, .card, .nav

ID

Unique

Target a single element with unique ID

#header, #main, #footer

Attribute

Advanced

Target elements based on attributes

[type="text"], [href]

Pseudo-class

Interactive

Target elements in specific states

:hover, :focus, :first-child

Combinator

Relationship

Target based on element relationships

div p, ul > li, h2 + p

CSS Selectors in Action

This comprehensive example demonstrates all major CSS selector types working together. Interact with elements to see how different selectors target specific parts of the HTML.

Code Editor

<ul class="menu">
  <li><a href="#" class="active">Home</a></li>
  <li><a href="#">Courses</a></li>
  <li><a href="#" data-new>Playground</a></li>
</ul>
<p id="note">Change a selector in the CSS tab and watch what updates.</p>

Preview

Basic Selectors

  • •Element selectors (h1, p, div)
  • •Class selectors (.card, .highlight)
  • •ID selectors (#header, #footer)
  • •Universal selector (*)

Advanced Selectors

  • •Attribute selectors [data-type]
  • •Pseudo-classes (:hover, :first-child)
  • •Descendant selectors (.card p)
  • •Child selectors (.container > .card)

Interactive Features

  • •Hover effects and transitions
  • •Click interactions via JavaScript
  • •Structural pseudo-classes
  • •Combined selector techniques

CSS Specificity

When multiple CSS rules target the same element, specificitydetermines which rule wins. Think of it as a scoring system where more specific selectors get higher scores.

0

Universal & Combinators

Score: 0
* { } /* Universal selector */ div p { } /* Descendant combinator */ h1 + p { } /* Adjacent sibling */

Lowest specificity - these selectors add no weight to the specificity calculation.

1

Element Selectors

Score: 1 each
h1 { } /* Score: 1 */ div p { } /* Score: 2 (div=1 + p=1) */ ul li a { } /* Score: 3 (ul=1 + li=1 + a=1) */

Each HTML element in the selector adds 1 point to the specificity score.

10

Class, Attribute & Pseudo-class

Score: 10 each
.button { } /* Score: 10 */ [type="text"] { } /* Score: 10 */ :hover { } /* Score: 10 */ .nav .item { } /* Score: 20 (10+10) */

Classes, attributes, and pseudo-classes each add 10 points to specificity.

100

ID Selectors

Score: 100 each
#header { } /* Score: 100 */ #nav .item { } /* Score: 110 (100+10) */ #main #content { } /* Score: 200 (100+100) */

ID selectors have high specificity - each ID adds 100 points.

1000

Inline Styles

Score: 1000
<div style="color: red;"> /* Score: 1000 */ <p style="font-size: 16px;"> /* Score: 1000 */

Inline styles have the highest specificity and will override external CSS.

Important Exception: !important

.override { color: blue !important; }

The !important declaration overrides normal specificity rules and should be used sparingly.

Selector Best Practices

Good Practices:

  • Use classes for reusable styles across multiple elements
  • Keep selectors as simple as possible for better performance
  • Use semantic class names that describe purpose, not appearance
  • Prefer classes over IDs for styling (save IDs for JavaScript)
  • Use descendant selectors to create component-scoped styles

Avoid These Mistakes:

  • Overusing !important - it makes CSS hard to maintain
  • Creating overly specific selectors that are hard to override
  • Using inline styles for anything beyond quick testing
  • Styling based on tag names alone for complex layouts
  • Creating deeply nested selector chains (limit to 3-4 levels)

Your Learning Path

Build expertise in CSS selectors systematically

What You've Learned

  • ▶Basic selector types (element, class, ID)
  • ▶Advanced selectors (attribute, pseudo-class)
  • ▶Combinator relationships and targeting
  • ▶CSS specificity and cascade rules
  • ▶Best practices for maintainable CSS

What's Next

  • ▶CSS Colors - Master color values and schemes
  • ▶CSS Backgrounds - Add visual depth and style
  • ▶CSS Typography - Control fonts and text styling
  • ▶CSS Box Model - Understand spacing and layout
  • ▶CSS Positioning - Master element placement

Pro Learning Tip

Practice combining different selector types to create precise targeting. Try recreating popular website layouts using only CSS selectors to master your skills.

CSS Selectors — step by step

1 / 4
1

Element, Class, and ID Selectors

Element selectors (p, h1) target all matching tags. Class selectors (.card) target elements with that class — reusable across many elements. ID selectors (#header) target one unique element and should be reserved for JavaScript hooks, not styling.

1 of 4
💡Prefer Classes Over IDs for Styling
IDs have a specificity of 100 — 10x a class. Once an ID rule exists, overriding it without another ID or !important is nearly impossible. Using classes for all styling keeps specificity flat, making your CSS easy to reason about and override.

🎯 Quick Check

Which combinator selects only the direct children of an element, not deeper descendants?

CSS Selectors: summary

Key points

  • Classes are the workhorse of CSS: reusable, low specificity and easy to override.
  • Avoid styling with IDs. Their high specificity makes later overrides painful.
  • :is() and :where() group selectors; :where() adds zero specificity.
  • :has() selects a parent based on its children, for example .card:has(img).

Common mistakes

  • Writing very long selectors like body div.main ul li a. They're slow to read and hard to override.
  • Confusing .a.b (one element with both classes) with .a .b (a .b inside an .a).

Frequently asked questions

›What is the difference between a class and an ID selector?

A class (.name) can be used on many elements and has low specificity. An ID (#name) must be unique on the page and has much higher specificity, which makes it harder to override.

›What is CSS specificity?

Specificity decides which rule wins when several target the same element. Inline styles beat IDs, IDs beat classes, attributes and pseudo-classes, and those beat element selectors. If specificity is equal, the later rule wins.

›Is there a parent selector in CSS?

Yes, :has(). For example, li:has(> a.active) selects a list item that contains an active link. It's supported in all modern browsers.