在 <p> 中嵌套块级元素标签...对还是错?
在 <p>
标记内嵌套 <div>
或任何其他块级元素在语法和语义上是否正确.我说的是 HTML4 过渡 DTD.
Is it syntactically and semantically correct to nest <div>
or any other block level element inside the <p>
tag. I am talking about HTML4 Transitional DTD.
如果不是,那么是否可以改为使用 <span style="display: block">
?
If not then is it OK to instead use <span style="display: block">
instead?
推荐答案
从句法上讲,p
中的 div
在所有 HTML 标准中都是无效的.此外,当使用符合标准的 HTML 解析器时,不可能将 <div>
元素放在 DOM 中的 <p>
内,因为打开的 <div>
标签会自动关闭 <p>
元素.
Syntactically, a div
inside a p
is invalid in all standards of HTML. Moreover, when using a conforming HTML parser, it is impossible to place a <div>
element inside a <p>
in the DOM because the opening <div>
tag will automatically close the <p>
element.
从语义上讲,正确的选择取决于您要标记的内容.您至少需要展示一个完整的示例段落,可能还有围绕它的内容,以确保提供足够的信息来确定正确的语义标记.
Semantically, the correct choice depends on the content that you are marking up. You will need to show at least a sample full paragraph and possibly the content surrounding it to be sure of providing sufficient information for the correct semantic mark-up to be determined.
然而,鉴于 <div>
和 <span>
都是语义自由的,而且 CSS 永远无法改变这一点,如果你确定的话<p>
标签的内容真正形成了一个段落,并且 <span style="display: block">
给你带来了你想要的表现效果正在寻找,那么那是有效的 HTML,将是一个完全合适的解决方案.
However, given that both <div>
and <span>
are semantics free, and that CSS in no way can ever change that, if you are certain that the contents of the <p>
tag truly form a paragraph, and that <span style="display: block">
gets you the presentational effect that you are seeking, then that is valid HTML and would be a wholly appropriate solution.
相关文章