html - 无法使用 CSS 定位将按钮定位在底部,而是在顶部
问题描述
我不明白这里发生了什么。我想在我的 div 底部放一个按钮。所以我将父元素的位置设置为absolute
,然后将我的元素设置为relative
位置。但相反,它位于顶部而不是底部。这太奇怪了,我不明白为什么会这样。
这是我的html
<!DOCTYPE html>
<html>
<head>
<style>
body{
background-color: #f3f2f4;
}
.header{
width: 105%;
height: 117px;
left: -11px;
position: absolute;
background-image: linear-gradient(#8bc2e0, #4f9bc6);
border-top-left-radius: 10px;
border-top-right-radius: 10px;
border-bottom-left-radius: 100% 70%;
border-bottom-right-radius: 100% 70%;
}
.container{
width:450px;
height:70vh;
background-color:white;
border-radius:6px;
position:absolute;
top:50%;
overflow:hidden;
left:50%;
transform:translate(-50%,-50%);
}
.logo {
width: 100px;
margin: 0 auto;
margin-top: 46px;
}
.button {
width: 80%;
margin: 0 auto;
padding: 15px 20px;
font-size: 17px;
color: #fff;
border-radius: 30px;
background: #428cb5;
display: flex;
align-items: center;
justify-content: center;
font-family: Arial;
cursor: pointer;
transition: .2s;
position: absolute;
bottom: 0px;
}
.button:hover {
background: #4895bf;
}
.body {
position: relative;
}
</style>
</head>
<body>
<div class="container">
<div class="body">
<div class="header">
<div class="logo">
</div>
</div>
<div class="button">Submit button</div>
</div>
</div>
</body>
</html>
这是一个小提琴: https ://jsfiddle.net/n35e6Lzh/
解决方案
你不应该在身体上方使用任何 div。删除标题和按钮中的绝对位置,并在容器中使用这 3 个属性:
display: flex;
flex-direction: column;
justify-content: space-between;
推荐阅读
- json - 如何将所有数据加载到选取器视图中
- firebase - 获取当前的 Firestore API 调用
- vb.net - vb.net 从文本框日期获取一年的第一天/上个月的最后一天/上个月的第一天
- flutter - Flutter:在屏幕之间传递多个数据
- amazon-web-services - AWS 网络输出
- jupyterhub - 为自己读/写 Jupyter notebook,为他人只读
- python - 处理无效的 Json
- android - 实现“com.github.parse-community.Parse-SDK-Android:parse:1.20.0”不起作用
- embedded - 如何使普通的 C 库在嵌入式环境中工作?
- swift - Swift 的艺术网