ATTRIBUTE SELECTORS /// [TARGET="_BLANK"] /// STARTS WITH ^= /// ENDS WITH $= /// CONTAINS *= /// ATTRIBUTE SELECTORS ///

CSS Attributes Selectors

Target elements precisely. Style inputs, filter links, and wield the wildcard syntax like a senior developer.

selectors.css
1 / 12
12345
๐ŸŽฏ

Tutor:Classes and IDs are great, but sometimes you want to style elements based on their HTML attributes. Enter CSS Attribute Selectors.


Skill Matrix

UNLOCK NODES BY MASTERING SELECTORS.

Concept: Presence Selector

Using [attribute] targets elements simply for having the attribute, like [disabled].

System Check

Which selector targets any element with a `title` attribute?


Community Holo-Net

Showcase Your Selectors

ACTIVE

Found a crazy use-case for a wildcard attribute selector? Share it with the crew!

CSS Attribute Selectors: Precision Targeting

Front End Instructor in Code Syllabus

Pascual Vila

Frontend Instructor // Code Syllabus

Classes and IDs are the bread and butter of CSS targeting, but what happens when you need to style elements based on their HTML attributesโ€”like styling all links pointing to PDFs, or inputs of a specific type? That's where CSS Attribute Selectors come in.

The Basic Forms

Attribute selectors live inside square brackets [].

  • Presence: [attr] - Matches any element that simply *has* the attribute. Example: [disabled] targets all disabled inputs or buttons, regardless of value.
  • Exact Match: [attr="value"] - Matches an element whose attribute exactly equals the string. Example: input[type="submit"] targets only submit buttons.

The Magic Wildcards

Often borrowed from regular expression logic, CSS allows string matching within attribute values.

  • Starts With: [attr^="value"] - Useful for finding elements where the attribute begins with a certain string. Example: a[href^="https"].
  • Ends With: [attr$="value"] - Targets the end of the string. Perfect for file extensions! Example: img[src$=".png"].
  • Contains (Substring): [attr*="value"] - The most flexible. Matches if the string appears *anywhere* in the attribute. Example: [class*="button-"] targets classes like "button-primary" and "large-button-red".
View Less Common Selectors (~= and |=)+

Space-Separated List: [attr~="value"] - Useful for matching individual words in attributes that contain space-separated values, like custom `data-tags="cool fast new"`.

Hyphen-Separated Prefix: [attr|="value"] - Rarely used, mostly for language codes. It matches an exact value, or a value immediately followed by a hyphen. Example: [lang|="en"] matches "en" and "en-US".

Selector Glossary

[attr]
Presence Selector. Targets elements holding the specified attribute, whatever its value may be.
snippet.css
[attr="val"]
Exact Value Selector. Targets elements where the attribute exactly matches the specified value.
snippet.css
[attr^="val"]
Prefix (Starts With) Selector. Targets elements where the attribute begins with the specified string.
snippet.css
[attr$="val"]
Suffix (Ends With) Selector. Targets elements where the attribute ends with the specified string.
snippet.css
[attr*="val"]
Substring (Contains) Selector. Targets elements where the attribute contains the specified string anywhere inside it.
snippet.css
[attr~="val"]
Space-Separated List Selector. Targets elements where the attribute contains the value as a distinct word in a space-separated list.
snippet.css