CSS Selectors

Beginner
⏱️ 14 min read
📚 Updated: Jul 2026
🎯 59 Tutorials
.class · #id · :hover

What You’ll Learn

CSS selectors are patterns that choose which HTML elements receive styles. This hub covers 59 selector tutorials — from basic p and .class to combinators, pseudo-classes, pseudo-elements, and attribute matching.

01

Basic

Tag, class, ID.

02

Combinators

Child, sibling.

03

:pseudo

States.

04

::parts

Fragments.

05

[attr]

Attributes.

06

59 guides

Full index.

Introduction

CSS selectors are patterns used to select elements on a web page and apply styles. They define the look and layout of a website by targeting specific HTML elements — without changing the HTML structure itself.

What Are CSS Selectors?

A selector tells the browser which elements get the declarations in a rule. You can target by element type (p), class (.card), ID (#header), attributes ([disabled]), structure (div > p), or state (a:hover).

💡
Beginner Tip

Start with .class for reusable styles and element for base typography. Add :hover and :focus when you style buttons and links.

Selectors vs Properties

  • Selectorwho to style — .btn
  • Propertywhat to change — background-color
  • Valuehow to change it — #2563eb
  • Rule.btn { background-color: #2563eb; }

📝 Syntax

Every CSS rule pairs a selector with a declaration block:

CSS
/* Element */
p { color: #334155; }

/* Class */
.highlight { background: #fef08a; }

/* ID */
#footer { text-align: center; }

/* Pseudo-class */
a:hover { text-decoration: underline; }

Selector families

p .class #id div p li:first-child [href^="https"] ::before
CategoryExampleTargets
ElementpAll <p> elements
Class.cardElements with class="card"
ID#mainThe element with id="main"
Combinatornav aLinks inside <nav>
Pseudo-classbutton:disabledDisabled buttons
Attributeinput[type="email"]Email inputs

⚡ Quick Reference

SelectorMeaningExample use
*Universal — every elementCSS reset
.nameClass matchReusable components
#nameID match (unique)Page landmarks
A BDescendantNested content
A > BDirect childImmediate children
:hoverPointer over elementInteractive feedback

Understanding Specificity

When two rules conflict, the browser picks the winner by specificity (then source order):

  • Inline style — highest (avoid overusing)
  • ID selectors#nav beats classes
  • Classes, attributes, pseudo-classes.btn, [open], :hover
  • Elements and pseudo-elementsp, ::before

Prefer low-specificity selectors — especially classes — so your CSS stays easy to override and maintain.

When to Use Which Selectors

  • Reusable components.class on buttons, cards, and badges.
  • Page structureheader, main, footer element selectors.
  • Unique regions#skip-link sparingly for one-off landmarks.
  • Navigation menusnav a or nav > ul > li combinators.
  • Forms:focus, :invalid, :checked, [required].
  • Links:link, :visited, :hover, :active (LVHA order).

👀 Live Preview

A highlighted box styled via .sel-preview class selector:

.sel-preview — class selector in action

CSS Selector Tutorial Index

Search by selector name or browse by category. Every card links to a full guide with four try-it examples and FAQs.

Basic Selectors

5 tutorials

Target elements by tag name, class, ID, or every element on the page.

Combinators

5 tutorials

Style elements based on their relationship to other elements in the DOM.

Pseudo-Classes

35 tutorials

Target element states, positions, and form validation — prefixed with a single colon.

:active
Tutorial

Styles an element while the user is pressing or clicking it.

:checked
Tutorial

Targets checked checkboxes, radio buttons, and selected options.

:default
Tutorial

The default option in a form control group.

:disabled
Tutorial

Form elements that cannot be interacted with.

:empty
Tutorial

Elements with no children and no text content.

:enabled
Tutorial

Form elements that are active and editable.

:first-child
Tutorial

The first child among its siblings.

:first-of-type
Tutorial

The first element of its type within its parent.

:focus
Tutorial

Elements receiving keyboard or pointer focus.

:fullscreen
Tutorial

The element currently displayed in fullscreen mode.

:has()
Tutorial

Parent-aware selector — style based on descendants or siblings.

:hover
Tutorial

When the pointer is over an element.

:in-range
Tutorial

Inputs whose value falls within min/max range.

:indeterminate
Tutorial

Checkboxes or radios in a mixed/indeterminate state.

:invalid
Tutorial

Form fields that fail validation constraints.

:lang()
Tutorial

Elements with a given language code — e.g. p:lang(es).

:last-child
Tutorial

The last child among its siblings.

:last-of-type
Tutorial

The last element of its type within its parent.

:link
Tutorial

Unvisited hyperlinks.

:not()
Tutorial

Elements that do not match the argument selector.

:nth-child()
Tutorial

Elements by index among siblings — e.g. li:nth-child(2).

:nth-last-child()
Tutorial

Elements counted from the end among siblings.

:nth-last-of-type()
Tutorial

Elements of a type counted from the end.

:nth-of-type()
Tutorial

Elements of a type by index — e.g. p:nth-of-type(2).

:only-child
Tutorial

The sole child of its parent.

:only-of-type
Tutorial

The only element of its type among siblings.

:optional
Tutorial

Inputs without the required attribute.

:out-of-range
Tutorial

Inputs with values outside the allowed range.

:read-only
Tutorial

Non-editable form fields.

:read-write
Tutorial

Editable user-input elements.

:required
Tutorial

Form fields that must be filled before submit.

:root
Tutorial

The document root — usually <html> — ideal for CSS variables.

:target
Tutorial

The element matching the URL fragment — e.g. #news:target.

:valid
Tutorial

Form fields that pass validation.

:visited
Tutorial

Hyperlinks the user has already visited.

Pseudo-Elements

7 tutorials

Style specific parts of an element — prefixed with two colons.

Attribute Selectors

7 tutorials

Match elements by the presence or value of HTML attributes.

Examples Gallery

Five starter snippets covering the most common selector types. Open linked tutorials for live try-it editors.

🔠 Basic Selectors

Element, class, and ID selectors together.

Example 1 — Element, Class & ID

Three fundamental ways to target HTML elements.

CSS
p { color: #334155; }
.highlight { background: #fef08a; }
#footer { text-align: center; }
.class Tutorial

How It Works

p targets all paragraphs. .highlight targets any element with that class. #footer targets the single element with that ID.

Example 2 — Descendant Combinator

Style links only inside a navigation block.

CSS
nav a {
  color: #2563eb;
  text-decoration: none;
}

How It Works

The space between nav and a is the descendant combinator. Only links inside <nav> receive the rule — not links elsewhere.

👉 Interactive & Attribute

Pseudo-classes and attribute matching.

Example 3 — :hover Pseudo-Class

Give visual feedback when the pointer is over a link.

CSS
a:hover {
  color: #7c3aed;
  text-decoration: underline;
}
:hover Tutorial

How It Works

:hover applies while the pointer rests over the element. Pair it with :focus for keyboard accessibility.

Example 4 — Attribute Selector

Style external HTTPS links differently.

CSS
a[href^="https"] {
  color: #059669;
}

How It Works

[href^="https"] matches anchors whose href starts with https. The ^= operator is the “starts with” attribute matcher.

Example 5 — ::before Pseudo-Element

Insert decorative content before an element.

CSS
.note::before {
  content: "📝 ";
}
::before Tutorial

How It Works

::before creates a virtual element as the first child. The content property is required for it to appear.

💬 Usage Tips

  • Prefer classes — Reusable, low specificity, easy to maintain.
  • Keep selectors short.card-title beats div div h3 span.
  • LVHA for links — Style in order: :link, :visited, :hover, :active.
  • DevTools inspect — See which selectors match and which rules win.
  • Search this index — Jump to any of 59 tutorials instantly.

⚠️ Common Pitfalls

  • Overusing IDs — High specificity makes overrides painful.
  • Deep descendant chainsdiv div div p is fragile and slow.
  • Universal selector resets* affects every element; scope carefully.
  • :hover-only interactions — Touch devices need :focus and tap targets too.
  • Missing ::before content — Pseudo-elements need a content value to render.

♿ Accessibility

  • :focus styles — Never remove focus outlines without a visible replacement.
  • :focus-visible — Consider keyboard-only focus rings where supported.
  • Color-only :invalid — Pair validation color with text or icons.
  • :visited privacy — Only safe properties can be styled on visited links.
  • Meaningful labels — Selectors style elements; accessible names come from HTML.

🧠 How CSS Selectors Work

1

Browser parses HTML

The DOM tree is built from your HTML elements, classes, IDs, and attributes.

DOM
2

Selectors match nodes

Each rule’s selector finds matching elements in the tree.

Match
3

Cascade resolves conflicts

Specificity and source order pick the winning declaration for each property.

Cascade
=

Styled elements

Matched elements receive colors, spacing, layout, and effects.

🖥 Browser Compatibility

Core selectors — element, class, ID, combinators, and common pseudo-classes — have universal browser support. Newer selectors like :has() have dedicated notes on their tutorial pages.

Baseline · Universal support

Core CSS selectors

Element, class, ID, descendant, child, and :hover work in every major browser. Check individual tutorials for newer selectors.

100% Core selectors
Google Chrome All versions
Full support
Mozilla Firefox All versions
Full support
Apple Safari All versions
Full support
Microsoft Edge All versions
Full support
Opera All modern versions
Full support
Core CSS selectors 100% supported

Bottom line: p, .class, #id, div p, and :hover are production-safe everywhere. For :has() and other newer selectors, read the compatibility section on each tutorial page.

🎉 Conclusion

CSS selectors are fundamental to web development — they let you precisely target and style HTML elements. Master basic selectors first, then explore combinators, pseudo-classes, and attributes as your layouts grow.

Use this hub to compare selector types, then open any of the 59 tutorials for try-it examples and detailed FAQs. Pair selectors with properties and media queries for complete, maintainable stylesheets.

💡 Best Practices

✅ Do

  • Use classes for reusable styles
  • Keep specificity low
  • Style :focus alongside :hover
  • Use semantic HTML first
  • Group selectors with commas to reduce repetition

❌ Don’t

  • Overuse ID selectors
  • Chain many levels of descendants
  • Rely on * for everything
  • Remove focus outlines without replacement
  • Memorize all 59 names at once

Key Takeaways

Knowledge Unlocked

Five things to remember about CSS selectors

Your gateway to 59 selector tutorials.

5
Core concepts
> 02

Child

Structure.

Combo
:h 03

:hover

States.

Pseudo
[] 04

[attr]

Match.

Attr
59 05

Index

Search all.

Ref

❓ Frequently Asked Questions

CSS selectors are patterns that tell the browser which HTML elements to style. They appear before the declaration block: selector { property: value; }. Examples include p, .card, #header, and a:hover.
A class selector (.name) can match many elements and is reusable. An ID selector (#name) should match only one element per page. Prefer classes for most styling; reserve IDs for unique landmarks or JavaScript hooks.
Specificity decides which rule wins when multiple selectors target the same element. IDs beat classes, classes beat elements, and inline styles beat stylesheet rules. Lower specificity is easier to maintain.
Pseudo-classes (:hover, :focus) describe element state or position in the tree. Pseudo-elements (::before, ::first-letter) style a specific part of an element. Pseudo-classes use one colon; pseudo-elements use two.
Combinators connect selectors to express relationships: descendant (space), child (>), adjacent sibling (+), and general sibling (~). They let you style elements based on where they sit in the HTML structure.
Read the overview, try the five examples, then open .class or element from Basic Selectors. Use the search box to jump to any of the 59 selector tutorials.

Start Your First Selector Tutorial

Open .class or element from Basic Selectors, or search the full index above.

.class tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful