Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the reasons NOT to use custom HTML tags?

Given current HTML5 specs that allows creating custom HTML elements (as long as their name contains a dash), and the fact that Web Components seem to be a feature that's here to stay, I'd like to know why is creating your own custom HTML elements frowned upon?

Note, that I'm not asking whether to use Web Components - which are still a moving target, and even with great polyfills like Polymer might not be ready for production yet. I'm asking about creating your own custom HTML tags and styling them, without attaching any JS APIs to them.

like image 531
Maciej Gurban Avatar asked Nov 03 '14 10:11

Maciej Gurban


People also ask

What is a custom tag in HTML?

Custom HTML tags are new tags that you name yourself in HTML to create new elements. Custom tags must contain a hyphen (-), start with an ASCII character (a-z), and be all lowercase for them to be valid. Custom elements make HTML simple to code, easier to read and keep your CSS well organized.

Can I create my own HTML tags?

Within the HTML spec, it is valid to create your own HTML tags, these are called custom elements and they must follow a strict naming convention for them to be valid. Custom elements must have an opening and a corresponding closing tag. Self-closing custom elements are not valid HTML. What Are Custom HTML Tags?

What is the importance of tags in HTML?

Well, tags are important in HTML because it is the syntax of HTML. That’s how code works. There is an opening tag and closing tag. like this <html></html>. Tags are the hidden keywords within a web page. A web page is made of tags.

What are the requirements for custom HTML tags?

Custom tags must contain a hyphen (-), start with an ASCII character (a-z), and be all lowercase for them to be valid. Custom elements make HTML simple to code, easier to read and keep your CSS well organized. Custom HTML elements must start with an ASCII character (a-z), contain at least one hyphen (-), and never contain any capital letters.


2 Answers

Short answer: I haven't heard any very compelling reasons to avoid them.

However, here are some recurring arguments I've heard made:

  • Doesn't work in old IE (just document.createElement("my-tag"); should fix that).
  • Global namespace clashes (same applies to class names, and custom elements in general).
  • CSS selector performance (doh, this is just about the last thing you should worry about).
  • Separation of functionality, meaning and presentation. This is actually the only argument I've heard that IMHO has any valid basis to it. You're of course better off with semantic HTML (search engines and all that), but if you were going to use a div for it otherwise, I don't see why you couldn't use a custom tag instead.
like image 125
quinnirill Avatar answered Oct 09 '22 23:10

quinnirill


One of the arguments against custom tags is their implied incompatibility with screen readers. This issue can be resolved with WAI-ARIA attributes.

There exists an issue in IE11, which breaks table layout if a custom element without display property is inserted inside a table cell. Check the plunker code. Therefore, it's the safest to declare all new elements explicitly, for example like so:

new-element {
    display: block;
}
like image 40
Maciej Gurban Avatar answered Oct 10 '22 00:10

Maciej Gurban