CSS Reference ยท Pseudo-classes
The :defined pseudo-class
Reference for the CSS :defined pseudo-class: match built-in elements and custom elements that have been registered, and hide upgrade flashes.
The :defined pseudo-class matches every element that is defined. All built-in HTML elements are always defined. A custom element (a tag with a hyphen, such as <user-card>) is defined only once JavaScript has registered it with customElements.define().
Its main use is the short time before that happens: :not(:defined) matches a custom element the browser does not yet know, so you can hide it or show a placeholder until it upgrades.
user-card:not(:defined) { visibility: hidden; }
Try it
The badge starts red with a dashed outline because nothing defined it. After you press the button the element is upgraded, matches :defined, and turns teal.
Syntax
:defined { /* declarations */ }
No arguments; specificity (0,1,0).
Syntax and arguments
| Part | Description |
|---|---|
:defined |
Any built-in element, and a custom element that has been registered. |
:not(:defined) |
A custom element that is not registered (yet), or an unknown tag name with a hyphen. |
my-tag:defined |
A specific custom element after upgrade. |
Usage notes
- Avoid the flash of unstyled content. If a custom element renders its content from JavaScript, hide it with
:not(:defined)until the script runs, then it appears fully formed. - Reserve space.
visibility: hiddenkeeps the layout box, so the page does not jump.display: nonewould make it pop in. - Unknown tags. A tag without a hyphen, like
<foo>, is an unknown element, but it is still "defined" in this sense (anHTMLUnknownElement). A tag with a hyphen that never gets registered stays undefined. - Combine with
customElements.whenDefined()in JavaScript if you need to wait for the same moment from script. - Shadow DOM. Inside a shadow tree, the selector applies to elements in that tree like normal.
Browser support
Supported in all current browsers and Baseline widely available.
Related
:hostโ the shadow host from inside:state()โ custom element states::part()โ exposed shadow parts- Lessons: Pseudo-classes, Selectors