🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Expert Masterclasses.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

CSS Attribute Selectors

Target elements with precision. Learn to match specific attributes, values, and wildcards.

style.css
/* Exact match */
input[type="text"] {
border: 2px solid blue;
}
/* Starts with */
a[href^="https"] {
color: green;
}
🎯
style.css
1 / 8
🎨

Tutor:Attribute selectors allow you to target elements based on the presence or value of an attribute. The simplest form is [attribute], which selects elements that have the attribute, regardless of its value.


CSS Mastery

Unlock nodes by learning selectors and matching patterns.

Concept 1: Exact Matching

The [attribute="value"] selector targets an element only if the attribute value matches the string exactly. This is ideal for form inputs, e.g., input[type="submit"].

System Check

Which selector targets an input with type='password'?


Community Snippets

Share Selector Patterns

Found a clever way to select elements without classes? Share your attribute tricks.

Enjoying this guide?

Codesyllabus is 100% free and open-source. Support our mission, pay for server infrastructure, and fuel new tutorials by buying us a coffee!

CSS Attribute Selectors

Author

Pascual Vila

Frontend Instructor.

Attribute selectors allow you to select elements based on the presence or value of a given attribute. This is a powerful way to style elements without adding extra classes or IDs, keeping your markup clean and semantic.

Exact vs Partial Matching

The most common syntax is [attribute="value"], which requires an exact match. However, real power comes from partial matches:

  • [attr^="val"]: Starts with (e.g., secure links https).
  • [attr$="val"]: Ends with (e.g., file types .pdf).
  • [attr*="val"]: Contains substring (e.g., part of a class name).

Common Use Cases

Forms: Target input types individually (`text` vs `submit` vs `checkbox`) without classes.

Links: Add icons to external links or download links based on the `href` attribute.

Debugging: Highlight elements with missing alt tags using img:not([alt]).

Best Practices

Attribute selectors have the same specificity as a class selector. They are robust and perform well in modern browsers. Use them to decouple your CSS from specific class names when the logic relates to the element's data or configuration.

Selector Syntax Glossary

[attribute]
Selects elements with the specified attribute, regardless of its value.
[attribute="value"]
Selects elements with the specified attribute and exact value.
[attribute^="value"]
Selects elements whose attribute value begins with the specified value.
[attribute$="value"]
Selects elements whose attribute value ends with the specified value.
[attribute*="value"]
Selects elements whose attribute value contains the specified substring.