在弹性项目中垂直和水平居中文本
Centering text vertically and horizontally in a flex item
我试图按照 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: center horizontally and vertically
你漏掉了一个“;”在 flex-item 的 margin-top 之后,这使得 line-height 无效
我试图按照 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: center horizontally and vertically
你漏掉了一个“;”在 flex-item 的 margin-top 之后,这使得 line-height 无效