SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Welcome to CSS
Selecting ElementsCombinatorsAttribute SelectorsPseudo-ClassesPseudo-ElementsSpecificity and InheritanceResolving Style Conflicts Matching selectors and tracing the cascade
Selectors and the Cascade

Attribute Selectors

Match elements by their attributes and group selectors.

An attribute selector selects elements by an HTML attribute. Square brackets can check that an attribute exists or compare its value.

Check for an attribute

Consider two links:

a[title] {
  color: green;
}

Only the first link matches. Read a[title] in two parts:

  1. a limits the match to links.
  2. [title] keeps the links that have a title attribute.

The attribute's value can be empty or any other text; this selector checks presence, not a particular value.

Match an exact value

Use = to match an exact attribute value:

a[href="docs.html"] {
  font-weight: bold;
}

This matches href="docs.html", but not href="docs.html#delivery". The second value contains extra text, so it is not an exact match.

Complete the selector before reading the explanation:

a[href__="docs.html"] {
  font-weight: bold;
}

The missing operator is =. Start by asking whether the whole value must match; then choose the operator that expresses that relationship.

Match words and parts of a value

When an exact match is too narrow, choose the operator by the kind of match you need:

SelectorMatches when the value…
~=Contains value as a whole word in a space-separated list.
`=`
^=Starts with value.
$=Ends with value.
*=Contains value anywhere.

These examples show both the match and the reason it works:

/* The whole word note is present in the class list. */
[class~="note"] {
  background-color: yellow;
}

/* en-GB begins with en followed by a hyphen. */
[lang|="en"] {
  color: green;
}

/* The URL starts with https:// and ends with .pdf. */
[href^="https://"][href$=".pdf"] {
  font-weight: bold;
}

The first rule matches class="note featured" but not class="notebook": ~= checks words, while *= would allow a match inside a larger word. The language rule matches en and en-GB, but not english. The last rule combines two conditions on the same link, so its href must start with https:// and end with .pdf.

The exact value note matches only note. A word match also finds note featured. A substring match additionally finds notebook.

For a class name, .note expresses the same whole-class match as [class~="note"]. Use the attribute form when the attribute itself, or the kind of value match, is what matters.

Match either condition or both

A selector list lets different matches share declarations:

a[href$=".pdf"],
a[title] {
  font-weight: bold;
}

The first selector matches the first two links. The second matches the first and third. The comma means or, so all three links become bold because each matches at least one selector.

Without a comma, conditions are joined on one element:

a[title][href$=".pdf"] {
  color: purple;
}

Only the first link matches this rule because it has both the title attribute and an href ending in .pdf.

Report and Manual have PDF links. Info and Manual have titles. Only Manual meets both conditions.

Choose the operator

Before writing a selector, name the relationship you need:

  • The attribute exists: [title].
  • The whole value matches: [href="docs.html"].
  • A whole word appears in a list: [class~="note"].
  • The value starts, ends, or contains text: use ^=, $=, or *=.
  • Two conditions must hold on one element: join selectors without a comma.
  • Different alternatives can share a rule: separate selectors with a comma.

Try it yourself

Try it yourself

Mark the downloadable resources

HTML + CSS

Loading code editor…

Live preview

Updating…

Final output

No comparison yetChecks after 3s without edits
Loading your draft…

Key Takeaways

  • Attribute selectors: Square brackets test an attribute's presence or value.
  • Operators: Match an exact value, a whole word, or text at the start, end, or anywhere in a value.
  • Class matching: A whole class name differs from an exact attribute value or a substring.
  • Selector lists: Commas group alternatives so they can share declarations.

How is this lesson?

Previous lessonCombinatorsNext lessonPseudo-Classes
Edit on GitHubReport on GitHub
On this page
%

On this page

Check for an attributeMatch an exact valueMatch words and parts of a valueMatch either condition or bothChoose the operatorTry it yourselfKey Takeaways