2

I stumbled upon a package that has no js or html, only css and it defines a custom element which can be used in html:

<status-indicator active pulse></status-indicator>

The only declaration I see for this is in the css file:

status-indicator {
  display: inline-block;
  border-radius: 50%;
  cursor: pointer;
  width: var(--status-indicator-size);
  height: var(--status-indicator-size);
  background-color: var(--status-indicator-color);
}

This is the package: https://www.npmjs.com/package/status-indicator

For the sake of learning - could anyone explain how and why this works or where should I refer to read about it?

4
  • This is same as html & CSS use with className and here same is applied but with a custom tag Commented Oct 12, 2021 at 12:41
  • Looks like the developer did not upload all his source code, a custom element can only be created with javascript Commented Oct 14, 2021 at 6:36
  • @Danny'365CSI'Engelman that sounds wrong since the package is installed from the code you see in github and all you need to make this element work is add the css file Commented Oct 14, 2021 at 6:46
  • 1
    You are right. run customElements.get("status-indicator") and you will get undefined. <status-indicator> isn't a Custom Element at all (yet). But it is perfectly fine to assign CSS to these elements; that is all the author does. Commented Oct 14, 2021 at 8:17

1 Answer 1

3

HTML allows custom elements and there are some DOM hooks to interface with them. The recommendation is to use a hyphen in the tag name so it does not overlap with future HTML tags.

As you can see from the CSS declaration, nothing is taken for granted. The display behavior is explicitly defined (the default is to treat an unknown element as inline text, just like <span>). It also uses CSS custom properties (variables).

Sign up to request clarification or add additional context in comments.

4 Comments

I knew about custom elements but had no idea one can be defined from CSS - which part refers to this in the spec?
I'm not sure what you mean by "defined" in this capacity. CSS defines behavior of elements regardless of whether they're in the HTML spec. When tinkering with a site in my browser's dev console, I often disable CSS by simply adding no as a parent element for some CSS declaration to disable it. There's nothing invalid about that.
As in there is no JS code to define the element window.customElements.define - only css code
Correct, as noted in the comments to the question. I don't think JS definition is necessary unless you've got some JS treatment that is complaining about that lack of a definition.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.