#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
CSS3 Fundamentals

CSS Attr Selectors

Attribute selectors let CSS target elements based on their HTML attributes and the values of those attributes, not just their tag name, class or id. They are handy for styling form inputs by type, marking external or downloadable links, or styling elements that carry data- or ARIA attributes, all without adding extra classes to the markup.

The seven forms of attribute selector

Each form is written inside square brackets after an optional element name.

SelectorMatches when the attribute
[attr]exists, with any value
[attr="v"]equals v exactly
[attr~="v"]is a space-separated list containing the word v
[attr|="v"]is v or starts with v- (used for language codes)
[attr^="v"]starts with v
[attr$="v"]ends with v
[attr*="v"]contains v anywhere

Practical examples

/* Style text-like inputs but not checkboxes or buttons */
input[type="text"],
input[type="email"],
input[type="password"] {
  border: 1px solid #c7c7c7;
  padding: 0.5rem;
  border-radius: 4px;
}

/* Links that point to another site */
a[href^="http"]:not([href*="example.com"])::after {
  content: " \2197";
}

/* Links to PDF files */
a[href$=".pdf"] {
  font-weight: bold;
}

/* Required fields get a marker on their label via a data attribute */
label[data-required]::after {
  content: " *";
  color: #c62828;
}

/* State driven by ARIA */
button[aria-expanded="true"] {
  background: #e3f2fd;
}

The ARIA example is worth copying. If your JavaScript already updates aria-expanded for accessibility, you can drive the visual state from the same attribute and skip adding a separate "open" class.

Case sensitivity and the i flag

Whether a value match is case-sensitive depends on the attribute and the document. To be safe, add i before the closing bracket to match regardless of case: a[href$=".pdf" i] catches .PDF too. There is also an s flag to force a case-sensitive match.

How specific are they?

An attribute selector has the same specificity as a class selector. So input[type="email"] counts as one element plus one class-level part. That matters when it competes with a rule like .form-field input, which has the same weight; whichever comes later in the stylesheet wins.

Common mistakes

The *= operator is easy to overuse. [class*="col"] matches col-6, but also collapse and colour-box, which may not be what you meant. Prefer ^=, ~= or plain classes when you need precision.

Another trap is the difference between the attribute and the live property. An input's value attribute holds the initial value written in the HTML; typing in the field does not change it. So input[value=""] will not track what the user types. For that, use pseudo-classes such as :placeholder-shown, :invalid or :checked.

Finally, remember quotes. Unquoted values work only for simple identifiers. Values with spaces, dots or slashes, such as file extensions or URLs, need quotes.

Attribute selectors also combine well with each other and with pseudo-classes. input[type="checkbox"]:checked + label styles the label that follows a ticked checkbox, and a[target="_blank"][rel~="noopener"] requires both attributes at once. Chaining keeps rules precise without inventing new class names, but very long chains become hard to read, so stop at two or three parts.

When to use them

Use attribute selectors when the attribute already says something meaningful about the element, such as its type, link target, language or ARIA state. Keep using classes for purely visual variations that the markup does not otherwise describe.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course