是否可以使用flexbox在背景颜色方面实现水平垂直居中?
.wrapper {
display: flex;
width: 100vw;
height: 100vh;
text-align: center;
}
.item {
flex: 1;
background-color: green;
align-self: center;
justify-content: center;
}
<div class='wrapper'>
<div class='item'>
sdafsdfs
</div>
</div>
height: 100%
,文本会移回到顶部。
height: 100%
时,它会失败的原因是它没有设置高度的父元素,因此具有默认高度。 - Jessedisplay: flex; align-items: center; justify-content: center
。 - Jesse