html - 为什么 body 溢出不起作用?

标签 html css

为什么 body overflow:hidden 在视口(viewport)高度大于 css 高度时不起作用?

ViewPort 高度 >700px 并且

css 高度为 300px

codepen http://codepen.io/vinaymavi/pen/aNMVYX

div{
  border: 1px dashed;
  height:55px;
}
body{
  padding:10px;
  width:100%;
  height:300px;
  border:2px solid red;
  overflow:hidden;
}
<html> 
  <body> 
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <div>7</div>
  </body>
</html

输出 enter image description here

最佳答案

来自CSS 2.2 spec for the overflow property

UAs must apply the 'overflow' property set on the root element to the viewport. When the root element is an HTML "HTML" element or an XHTML "html" element, and that element has an HTML "BODY" element or an XHTML "body" element as a child, user agents must instead apply the 'overflow' property from the first such child element to the viewport, if the value on the root element is 'visible'.

overflow:hidden设置在 body 元素上,改为应用于视口(viewport)。为避免这种情况发生,您可以设置 <html>元素不溢出:可见。

即添加 html { overflow:auto; }到 CSS。

关于html - 为什么 body 溢出不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41506456/

相关文章:

css - 固定盒子在 ipad 上跳转

html - CSS/JS 只显示适合容器的完整单词

html - 栏高忽略文本本身的高度

html - 带有子 UL LI 的 CSS 菜单

html - 是否有可能在 css3 中完美地复制它?

javascript - 如何在Jquery中删除Ol子元素没有Li的Li元素?

css - 修改grails中textarea(css)的背景颜色

html - Firefox 向右浮动行为

javascript - JQuery .html 选择器,图像未显示

html - 如何使用可变文本基线垂直对齐元素