[英]CSS: Icon / span in div prevents text from being aligned vertical center. How to fix this?
I encountered a strange behavior.我遇到了一个奇怪的行为。 Let's say you have a
div
that contains text and you want the text to be vertically centered.假设您有一个包含文本的
div
,并且您希望文本垂直居中。 To achieve this you would set the height
and line-height
of the container to the same value.为此,您需要将容器的
height
和line-height
设置为相同的值。 Everything works splendidly.一切都很顺利。
But now you decide to add an icon (via an icon font) to the mix.但是现在您决定在组合中添加一个图标(通过图标字体)。 And you want this icon to be much larger than the text.
并且您希望此图标比文本大得多。 Should be no problem.
应该没问题。 Just add a
span
or i
and set the font-size
to whatever you want.只需添加一个
span
或i
并将font-size
设置为您想要的任何值。 Should work.应该管用。
But it doesn't.但事实并非如此。 The text now suddenly hangs somewhere close to the bottom.
文字现在突然挂在靠近底部的某个地方。 Until you change the icon
font-size
to be the same as the container elements.直到您将图标
font-size
更改为与容器元素相同。
What is going on here?这里发生了什么?
#container { line-height: 30px; height: 30px; font-size: 15px; border: 1px solid black; } #icon { font-size: 30px; } #icon2 { font-size: 15px; }
<link href="https://use.fontawesome.com/releases/v5.4.2/css/all.css" rel="stylesheet"/> <div id="container"> Centered Text </div> <div id="container"> <span id="icon"><i class="fas fa-arrows-alt-v"></i></span> Centered Text </div> <div id="container"> <span id="icon2"><i class="fas fa-arrows-alt-v"></i></span> Centered Text </div>
Below I'm using a flexbox for alignment.下面我使用flexbox进行对齐。 This way I don't have to take care of height and/or line-height.
这样我就不必照顾高度和/或线高。
#container { font-size: 15px; border: 1px solid black; display: flex; align-items: center; /* Vertical alignment */ } #icon { font-size: 60px; } #icon2 { font-size: 30px; }
<link href="https://use.fontawesome.com/releases/v5.4.2/css/all.css" rel="stylesheet"/> <div id="container"> Centered Text </div> <div id="container"> <span id="icon"><i class="fas fa-american-sign-language-interpreting"></i></span> Centered Text </div> <div id="container"> <span id="icon2"><i class="fas fa-american-sign-language-interpreting"></i></span> Centered Text </div>
All text is aligned to the bottom of the container.所有文本都与容器底部对齐。 As your icon got larger it made the container larger and the rest of the text stayed aligned to the bottom.
当你的图标变大时,它会使容器变大,其余的文本保持与底部对齐。
What you want to do is set the icon to be vertically aligned to the middle of the container:您要做的是将图标设置为与容器中间垂直对齐:
#icon {
vertical-align: middle;
}
#container { line-height: 30px; height: 30px; font-size: 15px; border: 1px solid black; } #icon { font-size: 30px; vertical-align: middle; } #icon2 { font-size: 15px; }
<link href="https://use.fontawesome.com/releases/v5.4.2/css/all.css" rel="stylesheet" /> <div id="container"> Centered Text </div> <div id="container"> <span id="icon"><i class="fas fa-arrows-alt-v"></i></span> Centered Text </div> <div id="container"> <span id="icon2"><i class="fas fa-arrows-alt-v"></i></span> Centered Text </div>
The more generic solution:更通用的解决方案:
#container > span {
vertical-align: middle;
}
#container { line-height: 30px; height: 30px; font-size: 15px; border: 1px solid black; } #container > span { vertical-align: middle; } #icon { font-size: 30px; } #icon2 { font-size: 15px; }
<link href="https://use.fontawesome.com/releases/v5.4.2/css/all.css" rel="stylesheet" /> <div id="container"> Centered Text </div> <div id="container"> <span id="icon"><i class="fas fa-arrows-alt-v"></i></span> Centered Text </div> <div id="container"> <span id="icon2"><i class="fas fa-arrows-alt-v"></i></span> Centered Text </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.