Skip to content

Courses / CSS3 Mastery: Beginner to Advanced / Attribute Selectors

2,098 people viewed this

Attribute Selectors

CSS Attribute Selectors

Learning Objectives

  • Select elements based on their attributes.
  • Use exact, prefix, suffix and substring matching.
  • Understand case-sensitive and case-insensitive matching flags.
  • Use :is(), :where() and :not() to simplify selectors.

1. Attribute Exists

input[required] {
  border-color: red;
}

Matches input elements with a required attribute.

2. Exact Attribute Value

input[type="email"] {
  background-color: #f5faff;
}

3. Starts With

a[href^="https://"] {
  color: green;
}

4. Ends With

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

5. Contains Substring

[class*="btn"] {
  cursor: pointer;
}

6. Word and Hyphen Matching

[class~="active"] { color: green; }
[lang|="en"] { font-family: Arial, sans-serif; }

The tilde-equals operator matches a whitespace-separated word. The pipe-equals operator matches an exact value or a value followed by a hyphen.

7. Case-Insensitive Matching

[data-state="open" i] {
  outline: 1px solid blue;
}

The i flag requests ASCII case-insensitive matching for attribute values.

8. :is(), :where() and :not()

:is(h1, h2, h3) { font-family: sans-serif; }
:where(header, footer) a { text-decoration: none; }
button:not(:disabled) { cursor: pointer; }

:is() matches the elements described by its selector list and uses the most specific argument for specificity. :where() has zero specificity. :not() excludes elements that match its argument.

9. :has() — Relational Selector

Rate this course

No ratings yet — yours can be the first.

Stars show on this course. Your written note stays private.

Your stars