在 <p> 中嵌套块级元素标签……对还是错? [英] Nesting block level elements inside the <p> tag... right or wrong?

查看:31
本文介绍了在 <p> 中嵌套块级元素标签……对还是错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标签内嵌套

或任何其他块级元素在语法和语义上是否正确.我说的是 HTML4 过渡 DTD.

如果不是,那么可以改为使用 <span style="display: block"> 吗?

解决方案

在语法上,p 中的 div 在所有 HTML 标准中都是无效的.此外,当使用符合标准的 HTML 解析器时,不可能将

元素放在 DOM 中的

内,因为开头的 <;div> 标签将自动关闭

元素.

从语义上讲,正确的选择取决于您要标记的内容.您至少需要显示一个完整的示例段落以及可能的围绕它的内容,以确保为确定正确的语义标记提供足够的信息.

然而,鉴于 <div> 都是语义自由的,并且 CSS 绝不会改变这一点,如果你确定的话<p> 标签的内容真正形成了一个段落,并且 <span style="display: block"> 为您提供了正在寻找,那么这是有效的 HTML 并且将是一个完全合适的解决方案.

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.

If not then is it OK to instead use <span style="display: block"> instead?

解决方案

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.

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.

这篇关于在 &lt;p&gt; 中嵌套块级元素标签……对还是错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆