vertical-align:middle
属性不适用于span
元素。我尝试将文本中心和中间对齐放在span
元素中。我尝试了以下代码:
span {
text-align: center;
vertical-align: middle; /* Not working */
height: 150px;
width: 150px;
border: 1px solid black;
display: block;
}
<span>center</span>
发布于 2018-04-24 03:48:46
您可以使用display: flex
来实现这一点。
span {
height: 150px;
width: 150px;
border: 1px solid black;
display: flex;
align-items: center;
justify-content: center;
}
<span>center</span>
vertical-align: middle
不适用于display: block
发布于 2018-04-24 03:55:20
你对此有很多选择。我提供了3个例子:
line-height
属性(在本例中,您指定行高与高度相同):
.线-高度-中心{显示:内联块;线-高度:150;}display: table
:
表-中心{显示:内联表;}.表-中心跨度{显示:表格单元格;}display: inline-flex;
:
显示:内联-flex;对齐-项目:中心;对齐-内容:中心;}
span {
text-align: center;
vertical-align: middle; /* Not working */
height: 150px;
width: 150px;
border: 1px solid black;
}
.line-height-center {
display: inline-block;
line-height: 150px;
}
.table-center {
display: inline-table;
}
.table-center span {
display: table-cell;
}
.flex-center {
display: inline-flex;
align-items: center;
justify-content: center;
}
<p>With line height property:</p>
<span class="line-height-center">center</span>
<p>With display table:</p>
<span class="table-center"><span>center</span></span>
<p>With display flex:</p>
<span class="flex-center">center</span>
发布于 2018-04-24 03:51:06
您使用的是显示:块;显示块具有框行为。
但你想要运行一个文本行为。
如果你有一个已知的高度,你可以只使用线的高度。
例如,如果高度为150 is:
line-height: calc(150px - 15px);
https://stackoverflow.com/questions/50000904
复制