CKTIS / CSS
:state() — custom state pseudo-class
:state() selects custom elements by their custom states. Its argument is a CSS identifier, and matching is case-sensitive.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
:state( <ident> ):state() names a functional pseudo-class; the syntax :state( <ident> ) requires one CSS identifier, such as :state(ready). The parentheses cannot be empty, and the state name is not a quoted string.
The selector matches a custom element whose states set contains that name. Comparison is case-sensitive: the state ready does not match :state(READY). The document's host language, such as HTML, defines the exact matching behavior.
In the example, JavaScript adds ready through ElementInternals.states, and CSS selects the state-badge element. The example ran in Chromium and produced the expected console output. Verify the example in the target browser before deployment.
Examples
<!doctype html>
<meta charset="utf-8">
<title>Custom state</title>
<style>
state-badge { color: black; }
state-badge:state(ready) { color: green; }
</style>
<state-badge>Ready</state-badge>
<script>
customElements.define('state-badge', class extends HTMLElement {
constructor() {
super();
this.attachInternals().states.add('ready');
}
});
const badge = document.querySelector('state-badge');
console.log(badge.matches(':state(ready)'));
console.log(badge.matches(':state(READY)'));
</script>Result
true false
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.