css - doctype 会 100% 杀死但如果 doctype 关闭则不会?

标签 css html doctype

我正在尝试制作 100% 高度的东西,但添加文档类型会破坏它。这在 other questions 上进行了解释. 然而,我发现了一些奇怪的事情——如果我关闭 doctype 标签(我知道你不应该这样做)然后 100% 高度在 chrome 和 IE 上再次工作。那么到底发生了什么?关闭文档类型是否意味着文档类型被忽略?

    <!doctype html/> <!--You're not supposed to close doctype but if you do, then 
the 100% sizing works. Why?-->
<html>
<body>
    <!--This 100% height won't work with a normal doctype but closing
    the doctype makes it work?-->
    <div style="border:5px solid black;height:100%">This should be 100%</div>
</body>
</html>

最佳答案

根据 8.1.1 The DOCTYPE ,

A DOCTYPE must consist of the following components, in this order:

  1. A string that is an ASCII case-insensitive match for the string "
  2. One or more space characters.
  3. A string that is an ASCII case-insensitive match for the string "html".
  4. Optionally, a DOCTYPE legacy string or an obsolete permitted DOCTYPE string (defined below).
  5. Zero or more space characters.
  6. A ">" (U+003E) character.

因此,您的“封闭文档类型”是无效的文档类型。因此就像您根本没有文档类型一样。

关于css - doctype 会 100% 杀死但如果 doctype 关闭则不会?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32447412/

相关文章:

css - CSS 自定义属性可以保存另一个属性的值吗?

javascript - 单击虚拟键盘图标时保持对输入框的关注

声明 DOCTYPE 时,Javascript 在 Chrome 中不起作用

javascript - 如何将具有特定类的div的内容添加到其他页面?

html - 如何让 3 个输入具有与它们旁边的文本区域相同的组合高度?

css - Facebook 风格的内容容器

javascript - Replace a set of a html br elements with another in jQuery -- 用jQuery解析HTML

html - 当 ngIf 为 false 时构建 Angular4 ng-content

css - 将文档类型从 HTML 4 更改为 5 时添加了像素

css - 如何让 "position:fixed"css 在 IE 7+ 中使用 TRANSITIONAL 文档类型工作?