css使⼦元素在⽗元素居中的各种⽅法html结构:
<div class="parent">
<div class="child"></div>
</div>
⽅法⼀: display:flex
.parent {
width: 500px;
height: 500px;
background: red;
display: flex;
align-items: center;
justify-content: center;
}
.child {
width: 100px;
height: 100px;
background: blue;
}
⽅法⼆:display:table-cel
.parent{
width: 500px;
height: 500px;
background: red;
display: table-cell;
vertical-align: middle;
}
.child{
width: 100px;
height: 100px;
background: blue;
margin: auto;
}
⽅法三:绝对定位和0
.
parent{
width: 500px;
height: 500px;
background: red;
position: relative;
}
.child{
width: 100px;
height: 100px;
background: blue;
margin: auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
⽅法四:负边距
.parent{
width: 500px;
height: 500px;
background: red;
position: relative;
}
css设置文字垂直居中.child{
width: 100px;
height: 100px;
background: blue;
position: absolute;
top: 50%;
left: 50%;
margin-left: -50px;
margin-top: -50px;
}
以上四种⽅法都可以完成⽤css实现⼦元素在⽗元素实现⽔平和垂直居中。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。