另一个标签可以在 HTML 5 标题中吗(即 h1、h2、h3 等...)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5895082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can another tag be inside a HTML 5 heading (i.e. h1, h2, h3, etc...)?
提问by Shaoz
I'm not sure about the convention regarding headings in HTML 5 but I wanted to know if I could add a <small>
in a <h3>
, like this (this could apply to any tag inside any heading tags):
我不确定 HTML 5 中关于标题的约定,但我想知道是否可以在 a<small>
中添加a <h3>
,像这样(这可以应用于任何标题标签内的任何标签):
<h3>Payment details <small>(this is your default card)</small></h3>
Many Thanks
非常感谢
采纳答案by Ian Oxley
Yes, that markup validates. You can check it yourself on http://validator.w3.org/
是的,该标记有效。您可以在http://validator.w3.org/上自行检查
Something to be aware of with HTML5 though is a change to the notion of block-level elements: https://developer.mozilla.org/en/HTML/Block-level_elements
使用 HTML5 需要注意的是块级元素概念的变化:https: //developer.mozilla.org/en/HTML/Block-level_elements
回答by icyrock.com
The specs for heading elements:
标题元素的规格:
says that it has phasing content model:
说它有分阶段内容模型:
Content model:
Phrasing content.
Looking at the part of the specs for phasing content model:
查看分阶段内容模型的规范部分:
It says this:
它是这样说的:
Note: Most elements that are categorized as phrasing content can only contain elements that are themselves categorized as phrasing content, not any flow content.
注意:大多数被归类为短语内容的元素只能包含本身被归类为短语内容的元素,而不是任何流内容。
Aside from this, you cannot put heading in another heading - this is from W3C validator:
除此之外,您不能将标题放在另一个标题中 - 这是来自W3C 验证器:
Error Line 1, Column 23: Heading cannot be a child of another heading.
<!DOCTYPE html><h1><h2></h2></h1>
Although I could not find in the specs where it explicitly says this, there are restrictions that seem rather specific. This can yield to some very odd behavior such as that the above HTML would actually be parsed as
虽然我在规范中找不到明确说明这一点的地方,但有些限制似乎相当具体。这可能会导致一些非常奇怪的行为,例如上面的 HTML 实际上会被解析为
<h1></h1><h2></h2>
e.g. see this SOq for an example of indirect issues it can cause:
例如,请参阅此 SOq 以了解它可能导致的间接问题的示例:
Just in case someone runs into this...
以防万一有人遇到这个......
回答by planetjones
I don't think there's any restriction on this in the spec. Only Void elements cannot have children. See http://dev.w3.org/html5/markup/syntax.html#void-element
我认为规范中对此没有任何限制。只有 Void 元素不能有子元素。见http://dev.w3.org/html5/markup/syntax.html#void-element
Although generally the H1, H2, H3, etc. would be rendered in a single size and for a sub header like your "(this is your default card)" you might use a lower H e.g. H4.
尽管通常 H1、H2、H3 等会以单一大小呈现,并且对于像“(这是您的默认卡)”这样的子标题,您可能会使用较低的 H,例如 H4。
回答by automanualrepair ina
I don't think there's any restriction on this in the spec. Only void elements cannot have children. See http://dev.w3.org/html5/markup/syntax.html#void-element
我认为规范中对此没有任何限制。只有空元素不能有子元素。见http://dev.w3.org/html5/markup/syntax.html#void-element
A void element is an element whose content model never allows it to have contents under any circumstances. Void elements can have attributes.
The following is a complete list of the void elements in HTML:
void 元素是其内容模型在任何情况下都不允许其具有内容的元素。空元素可以有属性。
以下是 HTML 中 void 元素的完整列表:
area, base, br, col, command, embed, hr, img, input, keygen, link,
meta, param, source, track, wbr
Although generally the h1, h2, h3, etc. would be rendered in a single size. For a sub header like your "(this is your default card)" you might use a smaller header e.g. h4.
尽管通常 h1、h2、h3 等会以单一尺寸呈现。对于像“(这是您的默认卡)”这样的子标题,您可以使用较小的标题,例如 h4。