What's stopping me from using arbitrary tags in HTML?

前端 未结 7 931
礼貌的吻别
礼貌的吻别 2021-01-11 15:36

Even the new HTML5 tags aren\'t enough to describe structures without falling back to divs. What\'s stopping me from changing:

7条回答
  •  暖寄归人
    2021-01-11 16:29

    Good news

    • Nothing is stopping you
      Use as many custom tags as you like, browsers will know what you mean. Not just the modern ones but IE7+ too.
    • No default look
      Browsers will treat your tag as unknown and apply no default style to it. No weird margins, paddings, no surprises across clients and platforms. The display property is not implied either so give it a value, but after that, everything is as usual. DOM contains your node, querySelector works, styles apply, etc.
    • Super readable
      You'll love the new look of your HTML source.

    Things to consider

    1. Self-closing or not - to make your tag self-closing, use the good old format, as you would with
      for example. It's important, otherwise browsers go look for the closing tag. I mean, how could they not.

    2. Future collisions - that's the only good point from custom tag skeptics: just as we have a lot of new tags in HTML5, it's a possibility that you name a tag "icon" and it will mean something in the next HTML standard, even with a bunch of rendering defaults, and that can mess your page up badly. So I'd say, if you want to be on the safe side, use for your tags, they will never ever mess with dashed tag names thanks to the naming of Web Components. Also check out § 4.13 in HTML standard itself.

    3. Blame magnet effect - seriously, that's an issue with custom tags, I've been down this road. Use custom tags only if everyone working on the same project is on board with it. Otherwise, whenever something breaks it will be your fault "because of your stupid custom tags", and you'll have to change every instance to the usual

      , only to find later that the real issue was totally unrelated.

    TLDR:

         Yes you can use them 
    

提交回复
热议问题