CSS [attribute=value] Selector

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 4 Examples
Attribute Selectors

What You’ll Learn

The [attribute=value] selector matches elements when an attribute value is exactly equal to a given string. It is perfect for form controls, link targets, and other predictable HTML attributes.

01

Exact match

Full value only.

02

= operator

Equals operator.

03

Forms

type, target.

04

Buttons

submit vs button.

05

vs *=

Exact vs contains.

06

:focus

Combine states.

Introduction

The [attribute=value] selector in CSS is an attribute selector that targets elements whose specified attribute has a value that exactly matches the string you provide.

This allows precise styling based on HTML attributes like type, target, role, and lang — without adding extra classes to every element.

Definition and Usage

Write the attribute name in brackets, then = and the exact value in quotes: button[type="submit"]. The element matches only if the attribute value is identical to that string — no partial matches.

💡
Beginner Tip

= means “equals exactly.” Use [attr*=value] when the value only needs to contain a substring, or [attr^=value] / [attr$=value] for start/end matches.

📝 Syntax

The signature of the exact-match attribute selector is:

syntax.css
element[attribute="value"] {
  /* CSS properties */
}

Basic Example

target-blank.css
a[target="_blank"] {
  color: #dc2626;
  text-decoration: underline;
}

button[type="submit"] {
  background-color: #16a34a;
  color: #fff;
}

Syntax Rules

  • = requires the attribute value to match the quoted string exactly.
  • The match is case-sensitive.
  • Combine with element names: input[type="text"] is more specific than [type="text"].
  • Works with pseudo-classes: input[type="text"]:focus.
  • The element must have the attribute present for any match to occur.

Compare Attribute Operators

SelectorMatches when
[attr="x"]Value is exactly x
[attr^="x"]Value starts with x
[attr*="x"]Value contains x anywhere
[attr$="x"]Value ends with x

⚡ Quick Reference

QuestionAnswer
Operator= (exact match)
Match typeEntire attribute value must equal the string
Case sensitive?Yes
Common patternbutton[type="submit"]
vs contains*= matches a substring anywhere in the value
Browser supportAll modern browsers
a[target="_blank"] button[type="submit"] input[type="text"] input[type="checkbox"]

When to Use [attribute=value]

  • Form controls — Style input[type="text"], input[type="email"], or button[type="submit"] differently.
  • External links — Highlight links with target="_blank" so users know they open in a new tab.
  • Accessibility roles — Target elements with role="navigation" or role="alert" when needed.
  • Language — Style elements with lang="en" or other language codes.
  • Data attributes — Match predictable values like data-state="active" in component libraries.

👀 Live Preview

Links with target="_blank" are red and underlined; same-tab links keep default styling:

Examples Gallery

Practice exact matching with link targets, button types, text inputs, and checkboxes.

📜 Core Patterns

Match exact attribute values on links and form buttons.

Example 1 — Links that open in a new tab

Style anchors with target="_blank" so external links stand out visually.

target-blank.css
a[target="_blank"] {
  color: #dc2626;
  text-decoration: underline;
}
Try It Yourself

How It Works

Only the link with target="_blank" matches. The link with target="_self" has a different value and is not selected.

Example 2 — Submit vs regular buttons

Give submit and regular buttons distinct colors using their type attribute.

button-types.css
button[type="submit"] {
  background-color: #16a34a;
  color: #fff;
  border: 1px solid #16a34a;
}

button[type="button"] {
  background-color: #2563eb;
  color: #fff;
  border: 1px solid #2563eb;
}
Try It Yourself

How It Works

type="submit" and type="button" are different exact values. A class like btn-primary on the element would not affect the match.

📝 Form Inputs

Target specific input types for layout and focus styling.

Example 3 — Text input fields

Style text inputs and add a focus border for better form UX.

text-input.css
input[type="text"] {
  border: 1px solid #cbd5e1;
  padding: 0.5rem;
  border-radius: 6px;
}

input[type="text"]:focus {
  outline: none;
  border-color: #2563eb;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.2);
}
Try It Yourself

How It Works

Combining [type="text"] with :focus limits the focus style to text fields only — not checkboxes or radio buttons.

Example 4 — Checkbox inputs

Enlarge and accent checkboxes using an exact type match.

checkbox-input.css
input[type="checkbox"] {
  width: 1.1rem;
  height: 1.1rem;
  accent-color: #7c3aed;
  cursor: pointer;
}

label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}
Try It Yourself

How It Works

input[type="checkbox"] matches only checkboxes. An email input with type="email" would not be selected by this rule.

⚠️ Common Pitfalls

  • Partial values[type="button"] does not match type="button-primary" or values containing extra text.
  • Case sensitivitytype="Submit" and type="submit" are different matches.
  • Missing attribute — If the attribute is absent, the selector never matches.
  • Wrong operator — Use ^=, *=, or $= when you need partial matching instead of exact equality.

♿ Accessibility

  • External link cues — Pair target="_blank" styling with visible text or an icon indicating a new tab.
  • Focus states — Always provide a visible :focus style on form inputs; do not remove outlines without a replacement.
  • Color alone — Do not rely only on color to distinguish submit vs cancel buttons; use clear labels.

🧠 How [attribute=value] Works

1

Read attribute value

The browser reads the full string from the HTML attribute.

DOM
2

Compare exactly

= checks that the entire value equals your quoted string.

Match
3

Apply styles

Elements with an exact match receive the CSS rule.

Render
=

Precise attribute targeting

Ideal for forms, link targets, and known attribute values.

Browser Compatibility

The [attribute=value] selector is supported in all modern browsers.

Universal · All browsers

Exact matching everywhere

Chrome, Firefox, Safari, Edge, and Opera all support the = attribute operator.

99% Browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions
Full support
Opera All versions
Full support
[attribute=value] selector 99% supported

Bottom line: Safe to use = for form types, link targets, and other exact attribute values.

Conclusion

The [attribute=value] selector gives you precise control over styling by matching attribute values exactly. It is especially useful for forms, links, and semantic HTML attributes.

Combine it with pseudo-classes like :focus, compare it with partial-match operators, and remember that only the full value counts — not a substring inside it.

💡 Best Practices

✅ Do

  • Use button[type="submit"] for form actions
  • Style a[target="_blank"] for external links
  • Combine with :focus on inputs
  • Include the element name for specificity
  • Use exact match when values are predictable

❌ Don’t

  • Expect partial matches with =
  • Ignore case in attribute values
  • Remove focus outlines without replacements
  • Use = when you need *= or ^=
  • Match attributes that are not in the HTML

Key Takeaways

Knowledge Unlocked

Five things to remember about =

Use these points when matching exact attribute values.

5
Core concepts
📝 02

type="submit"

Form buttons.

Pattern
🔗 03

target="_blank"

External links.

Uses
🔍 04

Case matters

submit ≠ Submit

Rule
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

It matches elements whose attribute value is exactly equal to the specified string. The entire value must match — not just part of it.
[attr="val"] requires an exact match. [attr*="val"] matches when val appears anywhere inside the attribute value.
Yes. Exact matching is case-sensitive, so type="Submit" and type="submit" are different.
Styling form controls like button[type="submit"], input[type="text"], or links with target="_blank".
Yes. All modern browsers support the = attribute selector operator.

Practice in the Live Editor

Open the HTML editor and experiment with button[type="submit"] and other exact attribute selectors.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful