Attribute Selector ([…])

W3C CSS Snapshot 2026

Use brackets to match an attribute by its presence or by a pattern in its value.

On this page

Syntax and values

Value or syntaxWhat it does
[required]The attribute exists.
[type="email"]The value is email.
[class~="notice"]notice is a whole word in a whitespace-separated list.
[lang|="en"]The value is en or starts with en-.
[href^="https:"]The value starts with https:.
[href$=".pdf"]The value ends with .pdf.
[href*="guide"]The value contains guide.
[data-region="mm" i]The i flag makes ASCII letter matching case-insensitive. Use s for case-sensitive matching where supported.

Try the example

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Attribute Selector ([…])</title>
  <style>
input[type="email"] { border: 2px solid teal; }
[required] { background: #eff9ff; }
  </style>
</head>
<body>
<label>Email <input type="email" required></label>
<label>Name <input type="text"></label>
</body>
</html>

What to expect The email field gets a border and a pale background. The text field matches neither rule.

Official references