After writing HTML for 31 years
I switched from writing DIV-soup:
<div class="article-header">Hello, sad man</div>
to writing Semanctic HTML:
<article-header>Hello, happy man</article-header>
No JavaScript required!
Only CSS required, to replicate DIV block behaviour
article-header {
display:block;
}
This works,
because modern browsers treat a <tag-name>
(with a dash) as a valid HTMLElement
Also read: https://dev.to/dannyengelman/not-a-div-insidein-sightsite-18lj
Top comments (4)
I would not use custom tags to remove every div. Custom tags are not semantic because they are seen an a generic html element.
As you mention you need a style for each custom tag to behave as a div, so that styles' selector list is going be as long as there are div like custom tags.
As for the example the semantic tags already exist.
This is much better because browsers and crawlers understand those tags and they can do faster rendering and parsing respectively.
This is Tailwind mentality. utility classes are great, lets make every class a utility class.
:not(:defined) { display:block }
And my favorite Tailwind like usage
Nice! I didn't know you could target custom elements like that.
On the other hand it seems like a footgun selector. If you have a web-component that is used inline you have to override the display style.