什么原因导致此处出现垂直滚动条? http://codepen.io/anon/pen/QGByOQ?editors=1100
正如我建议的,.container
是body
的100%
高度,body继承html
,html
是 viewport
的 100%,因此每个 .chart
都应该完美契合,因为它的高度是 .container
的一半的。尽管如此,还是会出现 y 滚动条。这背后的行为是什么?
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div class="container">
<div class="chart1"></div>
<div class="chart1"></div>
<div class="chart1"></div>
<div class="chart1"></div>
</div>
</body>
</html>
CSS:
.container {
width: 100%;
height: 100%;
border: 5px dashed black;
box-sizing: border-box;
font-size: 0;
overflow: auto;
}
html, body {
height: 100%;
}
body {
margin: 0;
}
.chart1 {
display: inline-block;
width: 50%;
height: 50%;
border: 1px solid red;
box-sizing: border-box;
}
最佳答案
这是由于容器的红色边框造成的。使用 CSS calc()
函数,例如:
.chart1 {
height: calc(100% - 1px); /* 1px for the extra border */
}
看看下面的代码片段:
.container {
width: 100%;
height: 100%;
border: 5px dashed black;
box-sizing: border-box;
font-size: 0;
overflow: auto;
}
html, body {
height: 100%;
}
body {
margin: 0;
}
.chart1 {
display: inline-block;
width: 50%;
height: calc(50% - 1px);
border: 1px solid red;
box-sizing: border-box;
}
<div class="container">
<div class="chart1"></div>
<div class="chart1"></div>
<div class="chart1"></div>
<div class="chart1"></div>
</div>
希望这有帮助!
关于html - 垂直滚动条出现原因不明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41063070/