主体溢出:滚动无法正常工作

时间:2018-10-25 00:53:39

标签: html css

在以下代码段中,我希望body元素具有滚动条,但是它的行为不符合预期。我将html元素设置为height:100%,它获取初始包含块的高度,而body为100%,获取html元素的高度。现在,我设置溢出:在主体上滚动以获取滚动条,因为我不希望在主体外部看到这些框,但它们会保留在同一位置。我想念什么?这只是一个测试,我正在学习和阅读CSS规范,以了解html和body的高度如何工作。谢谢。

body {
  background-color: red;
  height:100%;
  overflow: scroll;
}

html {
  background-color: yellow;
  height: 100%;
}

.box {
  width: 300px;
  height: 300px;
  background-color: blue;
  margin-bottom: 20px;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

检查以下问题的第一个答案-> why body overflow not working?

您需要将overflow: auto添加到html元素中。

这里有一支笔:https://codepen.io/sebaLinares/pen/NOOdJM