Are custom elements valid HTML5?

后端 未结 12 1251
执念已碎
执念已碎 2020-11-22 09:27

I\'ve been unable to find a definitive answer to whether custom tags are valid in HTML5, like this:

Hello!

12条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 09:46

    N.B. The answer below was correct when it was written in 2012. Since then, things have moved on a bit. The HTML spec now defines two types of custom elements - "autonomous custom elements" and "customized built-in elements". The former can go anywhere phrasing content is expected; which is most places inside body, but not e.g. children of ul or ol elements, or in table elements other than td, th or caption elements. The latter can go where-ever the element that they extend can go.


    This is actually a consequence of the accumulation of the content model of the elements.

    For example, the root element must be an html element.

    The html element may only contain A head element followed by a body element.

    The body element may only contain Flow content where flow content is defined as the elements: a, abbr, address, area (if it is a descendant of a map element), article, aside, audio, b, bdi, bdo, blockquote, br, button, canvas, cite, code, command, datalist, del, details, dfn, div dl, em, embed, fieldset, figure, footer, form, h1, h2, h3, h4, h5, h6, header, hgroup, hr, i, iframe, img, input, ins, kbd, keygen, label, map, mark, math, menu, meter, nav, noscript, object, ol, output, p, pre, progress, q, ruby, s, samp, script, section, select, small, span, strong, style (if the scoped attribute is present), sub, sup, svg, table, textarea, time, u, ul, var, video, wbr and Text

    and so on.

    At no point does the content model say "you can put any elements you like in this one", which would be necessary for custom elements/tags.

提交回复
热议问题