我有一个用于固定位置div保持在视口底部的类。我正在尝试使宽度自动调整,以便当div宽度改变时,它仍然保持居中。
.box {
position: fixed;
width: 80%;
bottom: 20px;
left: 50%;
margin: 0 0 0 -40%;
max-height: 50%;
overflow: auto
}
有什么想法吗?我尝试过使用 text-align: center 和 display: inline 的容器,但是结果很奇怪。
.box {
position : fixed;
left : 10%;
right : 10%;
bottom : 20px;
max-height : 50%;
overflow : auto;
}
您可以同时使用left
和right
来使元素居中(而不是使用宽度)。
如果您想使用宽度,则可以这样做:
.box {
position : fixed;
left : 10%;
width : 80%;
bottom : 20px;
max-height : 50%;
overflow : auto;
}
如果你想将HTML居中在固定元素中,可以这样做:
.box > div {
width : 50%;
min-width : 150px;
margin : 0 auto;
text-align : center;
}
.centered { position: fixed; top: 50%; left: 50%; margin-top: -[1/2(element-height)]; margin-left: -[1/2(element-width)];}
这个工作。