DevLab
CSS

CSS Selectors: The Complete Guide

Learn every type of CSS selector — from basic to advanced — with examples and specificity explanations.

Basic Selectors

CSS selectors determine which HTML elements a rule applies to. They range from simple to highly specific.

  • * — Universal selector: matches everything
  • p — Type selector: matches all <p> elements
  • .class — Class selector: matches elements with that class
  • #id — ID selector: matches the element with that ID
  • [attr] — Attribute selector: matches elements with that attribute

Combinators

Combinators describe the relationship between selectors:

  • div p — Descendant: any <p> inside a <div>
  • div > p — Child: direct child <p> of <div>
  • h1 + p — Adjacent sibling: <p> immediately after <h1>
  • h1 ~ p — General sibling: any <p> after <h1>

Pseudo-classes

Pseudo-classes select elements based on state or position:

  • :hover, :focus, :active — Interaction states
  • :first-child, :last-child, :nth-child(n) — Position
  • :not(.class) — Negation
  • :is(h1, h2, h3) — Forgiving selector list
  • :where(header, footer) — Like :is() but zero specificity

Pseudo-elements

Pseudo-elements create virtual elements you can style:

  • ::before, ::after — Insert content
  • ::first-line, ::first-letter — Text formatting
  • ::placeholder — Style input placeholder text
  • ::selection — Style selected text

Attribute Selectors

Attribute selectors offer powerful matching for HTML attributes:

  • [href] — Has href attribute
  • [href="url"] — Exact value
  • [href^="https"] — Starts with
  • [href$=".pdf"] — Ends with
  • [class*="btn"] — Contains

Practice with these tools

More Learning Topics

RegexRegex Basics: A Complete Beginner's GuideRegexRegex Special Characters: Complete ReferenceRegexRegex Groups and Captures ExplainedRegexRegex Quantifiers: Complete GuideCSSCSS Specificity: Why Your Styles Aren't ApplyingJSONJSONPath Syntax: Query JSON Like XPathTimeUnix Timestamps ExplainedEncodingBase64 Encoding ExplainedEncodingJWT Structure and How It WorksEncodingJWT vs Session Tokens: Which Should You Use?EncodingJWT Refresh Tokens ExplainedCryptoHash Functions Explained: MD5, SHA-256, and When to Use EachEncodingURL Encoding Explained: What %20 Actually MeansJSONJSON Schema Explained: Validate Your JSON DataJSONJSON vs YAML: Which Should You Use?JSONJSON.stringify and JSON.parse: Edge Cases You Should KnowRegexRegex Lookahead and Lookbehind: Match Without ConsumingRegexRegex for Email Validation: The Right ApproachCSSThe CSS Box Model: margin, padding, border, and contentCSSFlexbox vs CSS Grid: When to Use EachCSSCSS Custom Properties (Variables) ExplainedTimeISO 8601 Explained: The Right Way to Format DatesTimeUnix Timestamps vs ISO 8601: Which to Use in Your API?EncodingUTF-8 Explained: How Computers Store TextTextCORS Explained: Why Your API Call is BlockedTextHTTP Status Codes: A Practical Developer GuideRegexNamed Capture Groups in Regex: Clean ExtractionsColorsColor Spaces Explained: RGB, HSL, HEX, and BeyondColorsColor Contrast for Developers: WCAG Rules and How to CheckNumbersNumber Bases Explained: Binary, Octal, Decimal, and HexNumbersBitwise Operations for Web DevelopersHTMLHTML Semantic Elements: A Complete ReferenceTextText Encoding for Developers: ASCII, UTF-8, and UnicodeRegexWhat is Regex? Complete Guide for DevelopersJSONJSON Format Explained: Structure, Syntax, and Common ErrorsEncodingHow JWT Works: Header, Payload, Signature DecodedHTMLHow to Convert Any Website to Markdown (for LLMs, RAG & Docs)TextPreparing Website Content for RAG: Clean Markdown Pipelines