Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

Attribute presence selector

The [attr] selector matches elements with the named attribute, regardless of its value, including an empty value.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

[attr] { property: value; }

[attr] matches an element having an attribute named attr. Replace attr with the actual name, such as [data-note]. Presence matters: both data-note="ready" and data-note="" match. An element without that attribute does not match.

Do not confuse presence with value comparison: [data-note="ready"] requires the specified value. Writing p[data-note] restricts matching to p elements; [data-note][title] requires both attributes on the same element. By itself, [attr] has specificity (0,1,0), the same as a class selector.

Examples

<!doctype html>
<meta charset="utf-8">
<title>Attribute presence</title>
<style>
  p { font-weight: 400; }
  [data-note] { font-weight: 700; }
</style>
<p id="a" data-note="ready">A</p>
<p id="b" data-note="">B</p>
<p id="c">C</p>

Result

a: 700; b: 700; c: 400

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources