:defined
Quick Summary for :defined
The :defined CSS pseudo-class represents any element that has been defined. This includes any standard element built in to the browser, and custom elements that have been successfully defined (i.e. with the CustomElementRegistry.define() method).
Code Usage for :defined
/* Selects any defined element */ :defined {   font-style: italic; }  /* Selects any instance of a specific custom element */ simple-custom:defined {   display: block; } 
More Details for :defined

:defined

The :defined CSS pseudo-class represents any element that has been defined. This includes any standard element built in to the browser, and custom elements that have been successfully defined (i.e. with the CustomElementRegistry.define() method).

/* Selects any defined element */ :defined {   font-style: italic; }  /* Selects any instance of a specific custom element */ simple-custom:defined {   display: block; } 

Syntax

:defined

Examples

Hiding elements until they are defined

The following snippets are taken from our defined-pseudo-class demo (see it live also).

In this demo we define a very simple trivial custom element:

customElements.define('simple-custom',   class extends HTMLElement {     constructor() {       super();        let divElem = document.createElement('div');       divElem.textContent = this.getAttribute('text');        let shadowRoot = this.attachShadow({mode: 'open'})         .appendChild(divElem);   } }) 

Then insert a copy of this element into the document, along with a standard <p>:

<simple-custom text="Custom element example text"></simple-custom>  <p>Standard paragraph example text</p> 

In the CSS we first include the following rules:

/* Give the two elements distinctive backgrounds */ p {   background: yellow; }  simple-custom {   background: cyan; }  /* Both the custom and the built-in element are given italic text */ :defined {   font-style: italic; } 

Then provide the following two rules to hide any instances of our custom element that are not defined, and display instances that are defined as block level elements:

simple-custom:not(:defined) {   display: none; }  simple-custom:defined {   display: block; } 

This is useful if you have a complex custom element that takes a while to load into the page — you might want to hide instances of the element until definition is complete, so that you don't end up with flashes of ugly unstyled elements on the page

Specifications

Specification
HTML Standard # selector-defined

See also

Web components Select your preferred language English (US)EspañolFrançais日本語한국어Русский中文 (简体) Change language

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Categories in CSS
css
Search CSS
Search CSS by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

You could also follow me on twitter. I have a couple of youtube channels if you want to see some video related content. RuneScape 3, Minecraft and also a coding channel here Web Dev.

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote
When I realized that, no individual step is hard in any process. Building this airport I'm standing in right now started with a guy writing the architectural plans on paper. That's not hard for him to do. Then laying the first beam isn't had. The whole thing is really hard. So, just take each step kind of piece by piece and when I was able to do that and stop trying to chase this prize and started putting in the work, things just started coming together.
Unknown
Random CSS Property

align-tracks

Experimental: This is an experimental technologyCheck the Browser compatibility table carefully before using this in production.
align-tracks css reference