DEV Community

Cover image for Web Components without JavaScript?!
Danny Engelman
Danny Engelman

Posted on

Web Components without JavaScript?!

You can replace all <div></div>

with <tag-name></tag-name>


(since 2018) ANY tagname with a dash is a valid HTMLElement

Let me repeat that for clarity:

๐Ÿ‘‰ ANY ๐Ÿคฏ tagname with a dash is a valid HTMLElement

NOT HTMLUnknownElement as some claim


That means you can replace EVERY <div> with valid HTML

Then this DIV-soup:

DIV soup

Becomes valid HTML:

No DIVs semantic HTML


๐Ÿง  Good to know

โœ… Absolutely NO JavaScript required, its only HTML and CSS

โœ… This is standard behaviour in all browsers since 2020
(When Microsoft Edge switched to the Chromium Engine)
and already in Chrome, Safari, Firefox many moons before 2020

โœ… The W3C HTML Validator accepts all <tag-name> Custom Elements with a dash as HTMLElement.
It does not accept <tagname> (no dash), those are HTMLUnknownElement

โœ… The UA - UserAgent StyleSheet (Browsers default stylesheet)
defines CSS [hidden] { display:none }.
But Custom Elements do not inherit the default stylesheet;
so you have to add that behaviour yourself in your stylesheet.

โœ… <DIV> is display:block only in the UA StyleSheet
You have to set ๐™™๐™ž๐™จ๐™ฅ๐™ก๐™–๐™ฎ: ๐™—๐™ก๐™ค๐™˜๐™  / ๐™ž๐™ฃ๐™ก๐™ž๐™ฃ๐™š-๐™—๐™ก๐™ค๐™˜๐™ ; on these Custom Elements yourself
(You will forget this 20 times, then you never make the mistake again)

โœ… The CSS :๐™™๐™š๐™›๐™ž๐™ฃ๐™š๐™™ pseudo selector targets standard HTML tags and JavaScript defined Custom Elements

โœ… Thus the CSS :๐™ฃ๐™ค๐™ฉ(:๐™™๐™š๐™›๐™ž๐™ฃ๐™š๐™™) pseudo selector targets the ๐™ช๐™ฃ๐™™๐™š๐™›๐™ž๐™ฃ๐™š๐™™ Custom Elements; they are still valid HTMLElement, CSS applies like any element

โœ… <you-are-not-bound-to-one-dash>

โœ… Declarative ShadowDOM:
<template shadowrootmode="open"> creates undefined Custom Elements with a shadowDOM

โœ… The Custom Elements API (MDN) requires JavaScript to convert undefined Custom Elements (but valid HTMLElement!) into defined Custom Elements.


๐ŸŽ‰๐ŸŽ‰๐ŸŽ‰

No more hunting for that closing </div> tag

</tag-name> is just there!


Are they Web Components?

Technologies NOT used:

  • <template>
  • shadowDOM
  • Custom Elements API (create defined elements with JavaScript)

๐Ÿ›  Prove ANY tagname with a dash IS a HTMLElement

for:

        <div></div>
        <tagname></tagname>
        <tag-name></tag-name>
        <tag-name-foo></tag-name-foo>
        <tag-name-bar></tag-name-bar>
Enter fullscreen mode Exit fullscreen mode


NO MORE DIV-SOUP!

DIV soup

โœ… If you believe AI over a random Dev.to post:

https://i.imgur.com/LPM8BKB.png

Note: every evergreen browser knows what a Custom Element is, just ask yourself:

(after reading: https://web.dev/articles/declarative-shadow-dom)

  • What happens with <tag-name>, and CSS applied, when you omit the <template>?
<tag-name>
  <template shadowrootmode="open">
  </template>
</tag-name>
Enter fullscreen mode Exit fullscreen mode













Top comments (0)