QP School

Full Version: Attribute selectors in CSS3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Example:

<!DOCTYPE html>
<html>
<head>
  <title>Attribute Selectors Example</title>
  <style>
    /* Select input elements with a "required" attribute and style their borders */
    input[required] {
      border: 2px solid red;
    }
  </style>
</head>
<body>
  <input type="text" required placeholder="This input is required">
  <input type="text" placeholder="This input is not required">
</body>
</html>

Explanation:

Attribute selectors target elements based on their attributes. In this example, the input[required] selector selects all input elements with a "required" attribute and applies a red border to them. The first input element in the HTML has the "required" attribute, so it gets the red border, while the second input element without the "required" attribute remains unaffected.