使用单标签时的div标签嵌套
所以我有以下 html:
<html>
<head>
<style>
.box
{
height: 100px;
width: 100px;
}
#box1
{
background-color: Red;
}
#box2
{
background-color: Red;
}
#box3
{
border-style: dotted;
}
#box4
{
background-color: Red;
}
</style>
</head>
<body>
<div id="boxes">
<div id="box1" class="box"></div>
<div id="box2" class="box"></div>
<div id="box3" class="box"></div>
<div id="box4" class="box"></div>
</div>
</body>
</html>
如果我将任何 div 标签更改为其单标签版本,例如:
<div id="box1" class="box"/>
后续标签将成为嵌套的,至少在 Chrome、Firefox 和 IE 中是这样。有谁知道有关此行为的交易吗?对我来说这似乎是一个错误。
So I have the following html:
<html>
<head>
<style>
.box
{
height: 100px;
width: 100px;
}
#box1
{
background-color: Red;
}
#box2
{
background-color: Red;
}
#box3
{
border-style: dotted;
}
#box4
{
background-color: Red;
}
</style>
</head>
<body>
<div id="boxes">
<div id="box1" class="box"></div>
<div id="box2" class="box"></div>
<div id="box3" class="box"></div>
<div id="box4" class="box"></div>
</div>
</body>
</html>
If I change any of the div tags to their single tag version, for example:
<div id="box1" class="box"/>
the subsequent tags become nested, at least in Chrome, Firefox and IE. Does anyone know the deal regarding this behavior? It seems like a bug to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不是 HTML 文档中的有效自闭合标记。如果您将其作为 XHTML 提供,那么它应该可以工作(如果不能,则这是一个浏览器错误)。
您还应该记住,Doctype 不会更改文档的解释方式,只会更改 mime-type(因此,如果您使用 Content-Type
application/xhtml+xml
提供文档,它将起作用) -您可以在这里阅读更多内容:http://www.webdevout.net/articles/beware-of-xhtml#content_type<div>
is not a valid self-closing tag in an HTML document. If you'd serve it as XHTML then it should work (if it doesn't, it's a browser bug).You should also keep in mind that Doctype doesn't change how the document is interpreted, only mime-type (so it will work if you serve the document with Content-Type
application/xhtml+xml
) - you can read more here: http://www.webdevout.net/articles/beware-of-xhtml#content_type该问题可能是由浏览器进入怪异模式引起的。
您可以尝试添加文档类型
the problem is maybe caused by the browser going to quirk mode.
You can try adding a doctype