水平垂直居中的四种方式
html 布局结构如下
<div class="outerBox">
<div class="innerBox"></div>
</div>
第一种 使用flex
.outerBox {
height: 400px;
background-color: rgb(12, 243, 232);
display: flex;
justify-content: center;
align-items: center;
}
.innerBox {
width: 200px;
height: 200px;
background-color: pink;
}
第二种 position + margin:auto
.outerBox {
height: 400px;
background-color: rgb(12, 243, 232);
position: relative;
}
.innerBox {
width: 200px;
height: 200px;
background-color: pink;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
第三种 position + margin负边距
.outerBox {
height: 400px;
background-color: rgb(12, 243, 232);
position: relative;
}
.innerBox {
width: 200px;
height: 200px;
background-color: pink;
position: absolute;
left: 50%;
top: 50%;
margin-left: -100px;
margin-top: -100px;
}
第四种 transform: translate
.outerBox {
height: 400px;
background-color: rgb(12, 243, 232);
position: relative;
}
.innerBox {
width: 200px;
height: 200px;
background-color: pink;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
遇见问题,这是你成长的机会,如果你能够解决,这就是收获。
作者:晚来南风晚相识
出处:https://www.cnblogs.com/IwishIcould/
本文版权归作者所有,欢迎转载,未经作者同意须保留此段声明,在文章页面明显位置给出原文连接
如果文中有什么错误,欢迎指出。以免更多的人被误导。
出处:https://www.cnblogs.com/IwishIcould/
想问问题,打赏了卑微的博主,求求你备注一下的扣扣或者微信;这样我好联系你;(っ•̀ω•́)っ✎⁾⁾!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,或者关注博主,在此感谢!
万水千山总是情,打赏5毛买辣条行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主(っ•̀ω•́)っ✎⁾⁾!
想问问题,打赏了卑微的博主,求求你备注一下的扣扣或者微信;这样我好联系你;(っ•̀ω•́)っ✎⁾⁾!

支付宝

微信
如果文中有什么错误,欢迎指出。以免更多的人被误导。