Skip to main content

How to Select Elements by Attribute in CSS

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

An attribute selector picks elements by the value of one of their attributes. button[type="submit"] styles the submit button of a form and leaves every other button as it is.

Syntax​

Syntax
<element>[<attribute>="<attribute value>"] {
<declarations>
}
PartWhat it does
<element>The element name, such as button. Leave it out to match every element with the attribute.
[<attribute>="<attribute value>"]Keeps only the elements whose <attribute> has exactly this value. It goes straight after the element name, with no space.
<declarations>The styles to apply, such as color: #ffffff;.

MDN's Attribute selectors page lists the other forms, such as matching only the start or the end of a value.

The Example​

The example is a form with two buttons, Cancel and Send. One rule with an attribute selector colors Send, the button whose type is submit, and leaves Cancel alone.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a style element holding one rule that gives buttons whose type attribute is submit a blue background and white text, and a form with a Cancel button of type button and a Send button of type submit.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Attribute selector</title>
<style>
button[type="submit"] {
background: #0f6cbd;
color: #ffffff;
}
</style>
</head>
<body>
<form>
<button type="button">Cancel</button>
<button type="submit">Send</button>
</form>
</body>
</html>

The selector button[type="submit"] has two parts: button picks the button elements, and [type="submit"] keeps only the ones whose type attribute is submit. The declarations give that button a blue background and white text.

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: a light grey Cancel button and a blue Send button with white text.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • Only Send turned blue. It's the only button with type="submit", so Chromium gave it the background rgb(15, 108, 189), which is #0f6cbd, and white text.
  • Cancel kept the browser's own look, a light grey background, rgb(239, 239, 239), because its type is button.

Common Mistakes​

MistakeWhat happensDo this instead
Putting a space before the bracket: button [type="submit"]The space changes the meaning to "an element with type="submit" inside a button", so nothing matched and Send kept its normal lookWrite the bracket straight after the element name
Leaving out the element name: [type="submit"]It matches every element with that value: an <input type="submit"> on the same page was styled tooKeep the element name when only one kind of element should change

Troubleshooting​

SymptomLikely causeFix
The rule doesn't applyThe value in the selector isn't exactly the value in the HTMLCopy the attribute and its value from the HTML into the selector
Elements of another kind changed tooThe selector has no element nameAdd the element name in front, such as button
Nothing changed, and the selector has a space before [The space makes it a selector for elements inside the elementRemove the space

FAQ​

Do I need the quotes around the value?​

Not for a single word: button[type=submit] matched Send just the same. MDN's page says the value must be quoted if it contains spaces or special characters, so quoting it every time is a safe habit.

How do I select every element that has an attribute, whatever its value?​

Leave out the equals sign and the value. On a page with two text boxes, input[required] matched only the box with the required attribute.

Which attributes can I use?​

Any of them. MDN lists the attributes of HTML and the other languages, data-* attributes, and attributes you make up yourself.

Conclusion​

Write the attribute and its value in square brackets straight after the element name, and the rule styles only the elements whose attribute has that value. Leave out the value to match every element that has the attribute.

Additional Resources​