div实现在屏幕水平垂直居中的方法
实现div在屏幕水平垂直居中的4个方法:
CSS:
<style>
*{
margin: 0;
padding: 0;
}
.div1 {
position:absolute;
width:200px;
height:200px;
top:50%;
left:50%;
margin-left:-100px;
margin-top:-100px;
background: yellow;
}
.div2 {
width: 200px;
height: 200px;
background-color: pink;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
.div3{
width: 200px;
height: 200px;
background-color: skyblue;
position: absolute;
top: calc(50% - 100px); /* 必须要有空格 */
left: calc(50% - 100px);
}
.div4{
width: 200px;
height: 200px;
background-color: red;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>HTML:
<!-- <div class="div1"></div> -->
<!-- <div class="div2"></div> -->
<!-- <div class="div3"></div> -->
<div class="div4"></div>如果你有更多更好的方法,欢迎亲们补充。
相关推荐
PioneerFan 2020-06-10
HSdiana 2020-03-28
FZfeng 2018-04-04
Ivy 2016-08-14
drdrsky 2020-06-16
葉無聞 2020-03-23
lyg0 2020-03-07
冰蝶 2020-01-10
玫瑰小妖 2019-12-31
行吟阁 2019-12-08
aiolos 2016-04-15
loverlucky 2016-03-15
WebVincent 2019-07-01
云端漂移 2019-07-01
renpinghao 2019-06-30