我有三个div与中心对齐,HTML和正文区域延伸到窗口之外,以容纳最后一个div,它还添加了一个滚动条。
My code
html,
body {
margin: 0;
border: 0;
padding: 0;
width: 100%;
height: 100%;
position: relative;
}
p {
margin: 0;
padding: 0;
border: 0;
}
.top {
position: absolute;
margin: 0;
padding: 0;
left: 50%;
top: 0%;
transform: translate(-50%, 0%);
}
.center {
position: absolute;
margin: 0;
padding: 0;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.bottom {
position: absolute;
margin: 0;
padding: 0;
left: 50%;
top: 100%;
transform: translate(-50%, -0%);
}
<html>
<head>
<title>Position</title>
</head>
<body>
<div class="container">
<div class="top">
<p>I am top div</p>
</div>
<div class="center">
<p>I am center div</p>
</div>
<div class="bottom">
<p>I am bottom div</p>
</div>
</div>
</body>
</html>
发布于 2018-07-16 00:37:11
这是在页面底部的绝对定位,你绝对用top:100%
来放置页脚,它通过将元素扩展到body
的边界之外来seems to cause overflow。
我注意到.bottom
上的top:95%
“修复”了这个问题。
但一个更简单的解决方案是只使用flexbox,因为这个场景就是它的用途,而且它的工作量更少:
body,
html {
margin: 0;
padding: 0;
}
.container {
height: 100vh;
}
.container {
display: flex;
flex-direction: column; /*specifies main axis to be vertical*/
justify-content: space-between; /*main axis positioning*/
align-items: center; /*cross axis (horizontal) positioning*/
}
<html>
<head>
<title>Position</title>
</head>
<body>
<div class="container">
<div class="top">
<p>I am top div</p>
</div>
<div class="center">
<p>I am center div</p>
</div>
<div class="bottom">
<p>I am bottom</p>
</div>
</div>
</body>
</html>
https://stackoverflow.com/questions/51349857
复制相似问题