CSS [attribute] Selector

CSS [attribute] selector selects all the elements that have a given attribute. For example, it can select all the <img> elements that have an alt attribute or select all the <a> elements that have a target attribute regardless of the value of the attribute.

In the example below, the [attribute] selector selects all the <a> elements that have a target attribute and sets their background color to yellow. Try it out:

Example:

a[target]{
  background: yellow;
}

Selects all the <img> elements that have an alt attribute and set their border-radius to 50%.

Example:

img[alt]{
  border-radius: 50%;
}

CSS Syntax

The [attribute] selector has the following syntax:

[attribute]{
   CSS styles
}

Related Pages

CSS Tutorials: CSS Attribute Selectors

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.