DEV Community

Cover image for Not a DIV inside/in sight/site
Danny Engelman
Danny Engelman

Posted on • Edited on

Not a DIV inside/in sight/site

use: <tag-name></tag-name>


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 Semantic HTML

Then this DIV-soup:

DIV soup

Becomes Semantic 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 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 default stylesheet
You have to set ๐™™๐™ž๐™จ๐™ฅ๐™ก๐™–๐™ฎ: ๐™—๐™ก๐™ค๐™˜๐™  / ๐™ž๐™ฃ๐™ก๐™ž๐™ฃ๐™š-๐™—๐™ก๐™ค๐™˜๐™ ; on these elements yourself
(You will forget this 20 times, then you never make the mistake again)

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

โœ… Thus CSS :๐™ฃ๐™ค๐™ฉ(:๐™™๐™š๐™›๐™ž๐™ฃ๐™š๐™™) targets the ๐™ช๐™ฃ๐™™๐™š๐™›๐™ž๐™ฃ๐™š๐™™ 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)


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

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














Top comments (0)