CSS CSS Tutorial CSS Advanced CSS Responsive Web Design(RWD) CSS Grid CSS Properties Sass Tutorial Sass Functions



CSS Attr Selectors

CSS Attribute Selectors are a powerful tool that allows you to select HTML elements based on their attributes. This means that you can style elements based on their attributes, such as their class, ID, or even their data attributes. In this tutorial, we will explore the different types of CSS Attribute Selectors and how to use them.

Brief Explanation of CSS Attribute Selectors

CSS Attribute Selectors are used to select HTML elements based on their attributes. There are several types of CSS Attribute Selectors, including:

  • [attribute] - Selects all elements that have the specified attribute.
  • [attribute=value] - Selects all elements that have the specified attribute with a value equal to the specified value.
  • [attribute~=value] - Selects all elements that have the specified attribute with a value containing the specified word.
  • [attribute|=value] - Selects all elements that have the specified attribute with a value starting with the specified value, followed by a hyphen.
  • [attribute^=value] - Selects all elements that have the specified attribute with a value starting with the specified value.
  • [attribute$=value] - Selects all elements that have the specified attribute with a value ending with the specified value.
  • [attribute*=value] - Selects all elements that have the specified attribute with a value containing the specified value.

Code Examples

Let's take a look at some code examples to see how CSS Attribute Selectors work.

Selecting Elements with a Specific Attribute

The following code selects all <a> elements that have a target attribute:

  <a target> {
    color: red;
  }

Selecting Elements with a Specific Attribute Value

The following code selects all <a> elements that have a target attribute with a value of "_blank":

  <a target="_blank"> {
    color: red;
  }

Selecting Elements with a Specific Word in the Attribute Value

The following code selects all <a> elements that have a class attribute with a value containing the word "button":

  <a class~="button"> {
    color: red;
  }

Selecting Elements with a Specific Starting Value in the Attribute Value

The following code selects all <div> elements that have a class attribute with a value starting with "container":

  <div class|="container"> {
    background-color: yellow;
  }

Selecting Elements with a Specific Starting Value in the Attribute Value

The following code selects all <div> elements that have a class attribute with a value starting with "container":

  <div class^="container"> {
    background-color: yellow;
  }

Selecting Elements with a Specific Ending Value in the Attribute Value

The following code selects all <img> elements that have a src attribute with a value ending with ".png":

  <img src$=".png"> {
    border: 1px solid black;
  }

Selecting Elements with a Specific Value in the Attribute Value

The following code selects all <input> elements that have a type attribute with a value containing "email":

  <input type*="email"> {
    border: 1px solid blue;
  }

Conclusion

CSS Attribute Selectors are a powerful tool that allows you to select HTML elements based on their attributes. By using CSS Attribute Selectors, you can create more specific and targeted styles for your web pages.

References

Activity