大家来一起看看html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div style="width:300px;height:300px;" class="wrap">
<div style="width:100px;height:100px;" class="box">
</div>
</div>
</body>
</html>
话不多说,直接进入
第一种flex
.wrap{
display: flex;
justify-content: center;
align-items: center;
}
第二种table-cell
.wrap{
display: table-cell;
text-align: center;
vertical-align: middle;
}
.box{
display: inline-block;
}
第三种margin+transfrom
.wrap{
overflow: hidden;
}
.box{
margin: 50% auto;
transform: translateY( -50%);
}
第四种inline-block+vertical-align
. wrap{
text-align: center;
line-height: 300px;
}
.box{
display: inline-block;
vertical-align: middle;
}
第五种absolute+负margin
.wrap{
position : relative;
}
.box{
position: absolute;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -50px;
}
第六种abslute+margin:auto
.wrap{
position: relative;
}
.box{
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
第七种abslute+transform
.wrap{
position : relative;
}
.box{
position: absolute;
top: 50%;
left:50%;
transform: translate( -50%, -50%);
}
第八种最新的方法grid
.wrap{
display: grid;
}
.box{
align-self: center;
justify-self: center;
}
对css酷炫样式的有兴趣的博友可以去b站关注https://space.bilibili.com/1683165410