Custom Elements Donā€™t Require a Hyphen as a Separator

Scott Jehl reached out to help me resolve a conundrum in my post about what constitutes a valid custom element tag.

The spec says you can have custom elements with emojis in them. For example:

<emotion-šŸ˜></emotion-šŸ˜>

But for some reason the Codepen where I tested this wasnā€™t working.

Turns out, Iā€™m not very good at JavaScript and simply failed to wrap everything in a try/catch.

Whatā€™s funny about this is that <my-$0.02> isnā€™t a valid custom element but <my-šŸ’²0.02> is!

Anyhow, Iā€™ve since updated that post and now things work as the spec says. All is good with the world.

But thatā€™s not all.

In my convo with Scott, he pointed out that custom element tag names donā€™t need a hyphen as a separator of characters, they only need the hyphen.

This kinda blew my mind when I realized it. All this time Iā€™d been thinking about the rules for custom elements wrong.

You arenā€™t required to have the hyphen as a separator:

<my-tag></my-tag>

Youā€™re just required to have it:

<mytag-></mytag->

Those are both valid custom element tag names!

Which means, if you have a really simple element and canā€™t think of a better name than an existing HTML element, you can do this:

<h1->My custom heading</h1->

Or this:

<p->My custom paragraph</p->

Or, I suppose, even this:

<ul->
  <li>My custom unordered list</li>
  <li>That still uses normal liā€™s</li>
  <li>Because why not?</li>
</ul->

Iā€™m not saying you should do this, but I am saying you could ā€” you know, nothing ever went wrong doing something before stopping to thinking about whether you should.