CSS [attribute=value] Selector

Definition and Usage

CSS [attribute=value] The selector is used to select elements that have a specified attribute and whose attribute values match completely.

Example

Example 1

Select and set the styles for all <a> elements with target="_blank". At the same time, select and set the styles for all <p> elements with lang="it":

a[target="_blank"] {
  background-color: yellow;
}
p[lang="it"] {
  background-color: salmon;
}

Try it yourself

Example 2

Set the width of the <input type="text"> element to 100px. However, when it gains focus, set its width to 250px:

input[type="text"] {
  width: 100px;
}
input[type="text"]:focus {
  width: 250px;
}

Try it yourself

CSS Syntax

[attribute = value] {
  css declarations;
}

Technical Details

Version: CSS2

Browser Support

The numbers in the table indicate the first browser version that fully supports this selector.

Chrome Edge Firefox Safari Opera
4.0 7.0 2.0 3.1 9.6

Related pages

CSS Tutorial:CSS attribute selector

CSS Tutorial:Detailed Explanation of CSS Attribute Selectors