我想把我的按钮定位在一个div的底部或屏幕底部(但在一个非固定的位置)。我的代码结构如下所示:
我想把按钮放在div 1的底部,这个高度是用jQuery设置的(高度是屏幕的高度,所以将按钮放在屏幕底部也可能是一个解决方案)。
到目前为止我尝试过的是:
CSS
.button {
position: fixed;
bottom: 10px;
left: 50%;
margin-left: -104.5px; /*104.5px is half of the button's width*/
}这是中心按钮(我想要的),它把它放在屏幕的底部,但位置是固定的,所以如果我向下滚动按钮也向下。我还尝试过将按钮的position设置为absolute,而div-1的position设置为relative,这也不起作用。
编辑:div的高度是可变的,所以边距可能不是很好的选择
发布于 2016-03-30 08:15:13
只需按一下按钮的位置:绝对的,而不放div相对论处。
.button {
position: absolute;
bottom: 10px;
left: 50%;
margin-left: -104.5px; /*104.5px is half of the button's width*/
}
.test{
height:1000px;
}<div class="test">
<div>
<div>
<button class="button">
test
</button>
</div>
</div>
</div>
发布于 2019-02-11 04:18:23
如果宽度为50%,则左侧必须为25%,如果宽度为70%,则左侧必须为15%。
.center{
position:fixed;
bottom:20px;
left:20%;
width:60%;
}
.center .btn{
background:red;
width:100%;
color:white;
font-weight:bold;
border-radius: 64px;
padding:10px;
}<div class="center">
<button class="btn">Login</button>
</div>
发布于 2016-03-30 08:08:18
https://stackoverflow.com/questions/36302937
复制相似问题