【发布时间】:2020-03-08 12:57:45
【问题描述】:
我正在寻找垂直对齐列表项中的文本。我正在使用display: flex; 和align-items: center;。但是我的代码包含一个strong 标记,它被解释为一个单独的弹性项目,因此两边的空格都被折叠了。
我可以用  破解它,但是有没有一种正确的方法让我的文本和内联元素垂直居中对齐?
- 我知道我可以将文本包装在
<p>中。我正在努力避免这种情况。 - 我的文字可能足够长,可以换行,所以
line-height不是一个选项。 - 一旦display: contents; 得到广泛支持,这将是我的首选解决方案。
li {
display: flex;
align-items: center;
height: 8em;/* just for example */
border: 2px solid red;/* just for example */
}
<ul>
<li>It is <strong>Very important</strong> that this text is vertically centered!</li>
<li>Also <strong>Very important</strong> that there are spaces in the text.</li>
</ul>
【问题讨论】:
-
I know I could wrap the text in a <p>. I'm trying to avoid that.--> 你应该这样做,并且永远不要将文本容器变成弹性盒子容器,如果你有更多的行,你所拥有的将一文不值。相关:stackoverflow.com/a/54903923/8620333