简体   繁体   English

有没有块级父级的内联元素是否有效?

[英]Is it valid to have an inline element without a block-level parent?

I want to know if block-level, parent elements are technically-required for inline elements in an HTML document. 我想知道,HTML文档中的内联元素在技术上是否需要块级父元素。 For example, the following HTML ... 例如,以下HTML ...

<html>
    <head>
    </head>
    <body>
        <b>Some Text</b>
    </body>
</html>

Is this a valid HTML document? 这是有效的HTML文档吗? Or must the <b> and </b> tags be encapsulated in a block-level element to be considered valid, like <p> or <div> ? 还是必须将<b></b>标记封装在块级元素中以被视为有效,例如<p><div> (Hopefully the answer is the same for HTML4 and HTML5?) (希望答案对于HTML4和HTML5是相同的吗?)

Many people ask a similar question: Can I place a block-level element inside an inline element? 很多人问一个类似的问题: 我可以在一个内联元素中放置一个块级元素吗? I want to know the opposite: Can inline elements go anywhere besides in a block-level element in an HTML document? 我想知道相反的内容: 内联元素除了HTML文档中的块级元素之外,还可以放在其他地方吗?

I am seeing this in an RFC: 我在RFC中看到了这一点:

Certain HTML elements that may appear in BODY are said to be "block-level" while others are "inline" (also known as "text level").... 某些可能出现在BODY中的HTML元素被称为“块级”,而另一些则是“内联”(也称为“文本级”)。

Generally, block-level elements may contain inline elements and other block-level elements. 通常,块级元素可以包含内联元素和其他块级元素。 Generally, inline elements may contain only data and other inline elements. 通常,内联元素可以仅包含数据和其他内联元素。 https://www.w3.org/TR/html401/struct/global.html#h-7.5.3 https://www.w3.org/TR/html401/struct/global.html#h-7.5.3

But, it doesn't totally come out and say it. 但是,它并不能完全说出来。

You can place a inline element anywhere, inside a block level, outside, wherever, even outside of the body if you wanted to! 如果需要,您可以将内联元素放置在块级内部,外部,任何位置甚至body外部的任何位置! They'll render just the same, only without any padding/margin that you would expect of block level elements. 它们将呈现相同的效果,只是没有您期望块级元素的任何填充/边距。

In the MDN reference for inline elements, they are always - in the two examples give - shown inside of block-level elements, but that doesn't mean that they can't stand alone, outside of a block element. MDN内联元素参考中,它们始终(在两个示例中给出)显示在块级元素内部,但这并不意味着它们不能独立于块元素之外。 It just means that it generally isn't done because it is sematically wrong - ie, you have block level elements that contain inline elements, not lone-standing inline elements. 这只是意味着通常不会完成,因为它在语义上是错误的-即,您具有包含内联元素的块级元素,而不是单独存在的内联元素。

Although technically, the example you provided is not an example of an inline element outside a block element because the body is a block element. 尽管从技术上讲,您提供的示例不是块元素外部的内联元素的示例,因为body是块元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM