<div style="height:100px;width:100px;border:1px solid black"></div>
如何将此div置于中心位置?
答案 0 :(得分:1)
.parent {
position: relative;
}
.child {
width: 300px;
height: 100px;
padding: 20px;
position: absolute;
top: 50%;
left: 50%;
margin: -70px 0 0 -170px;
}
您可以找到here。
答案 1 :(得分:0)
在该div的父级上:
div.parent {
display: flex;
justify-content: center;
align-items: center;
}
答案 2 :(得分:0)
您可以使用top: 50%; left: 50%;
绝对定位该元素,然后使用translate()
将其向后移动/向上移动50%自己的宽度以将其置于绝对中心。
div {
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%,-50%);
}
&#13;
<div style="height:100px;width:100px;border:1px solid black"></div>
&#13;