我试图按照flexbox示例进行操作,但我的行高不适用于此问题。
文本应垂直居中,但不应该居中。它仅在水平居中的位置保持在顶部。
在代码段中,它似乎可以正常工作,但只是为了证明这是奇怪的行为,我将给出我所看到的图像。
我在Chrome和IE中都对此进行了测试,但它并未垂直居中。
我确实更改了代码段的大小,但我认为不会这样做。
.flex-container {
display: flex;
flex-flow: row wrap;
justify-content: space-around;
padding: 0;
margin: 0;
list-style: none;
display: -webkit-box;
display: moz-box;
display: -ms-flexbox;
display: -webkit-flex;
-webkit-flex-flow: row wrap;
}
.flex-item {
background: tomato;
padding: 5px;
width: 50px;
height: 50px;
margin-top: 10px line-height: 50px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;
}
<ul class="flex-container">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
<li class="flex-item">6</li>
</ul>
要在每个伸缩项目中垂直和水平居中放置文本,请对您的代码进行以下调整:
.flex-container {
display: flex;
flex-flow: row wrap;
justify-content: space-around;
padding: 0;
margin: 0;
list-style: none;
display: -webkit-box;
display: moz-box;
display: -ms-flexbox;
display: -webkit-flex;
-webkit-flex-flow: row wrap;
}
.flex-item {
display: flex; /* create nested flex container */
justify-content: center; /* center text horizontally */
align-items: center; /* center text vertically */
background: tomato;
padding: 5px;
width: 50px;
height: 50px;
color: white;
font-weight: bold;
font-size: 3em;
}
<ul class="flex-container">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
<li class="flex-item">6</li>
</ul>
有关更多详细信息,请参见:Flexbox:水平和垂直居中
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句