假设我有一个bootstrap按钮,带有一个字体很棒的图标和一些文本:
<div>
<i class='icon icon-2x icon-camera'></i>
hello world
</div>
我如何使文本显示垂直居中? 文本现在与图标的底部边缘对齐:http://jsfiddle.net/V7DLm/1/
假设我有一个bootstrap按钮,带有一个字体很棒的图标和一些文本:
<div>
<i class='icon icon-2x icon-camera'></i>
hello world
</div>
我如何使文本显示垂直居中? 文本现在与图标的底部边缘对齐:http://jsfiddle.net/V7DLm/1/
当前回答
最简单的方法是设置垂直对齐css属性为middle
i.fa {
vertical-align: middle;
}
其他回答
我将文本包装在一个,所以你可以分别针对它。现在,如果同时浮动和左浮动,则可以使用line-height来控制对象的垂直间距。将它设置为与(30px)相同的高度,将使它在中间对齐。在这里看到的。
新的标记:
<div>
<i class='icon icon-2x icon-camera'></i>
<span id="text">hello world</span>
</div>
新CSS:
div {
border: 1px solid #ccc;
height: 30px;
margin: 60px;
padding: 4px;
vertical-align: middle;
}
i{
float: left;
}
#text{
line-height: 30px;
float: left;
}
一个flexbox选项-字体awesome 4.7及以下
足总4。x托管URL - https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css
div { display: inline-flex; /* make element size relative to content */ align-items: center; /* vertical alignment of items */ line-height: 40px; /* vertically size by height, line-height or padding */ padding: 0px 10px; /* horizontal with padding-l/r */ border: 1px solid #ccc; } /* unnecessary styling below, ignore */ body {display: flex;justify-content: center;align-items: center;height: 100vh;}div i {margin-right: 10px;}div {background-color: hsla(0, 0%, 87%, 0.5);}div:hover {background-color: hsla(34, 100%, 52%, 0.5);cursor: pointer;} <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <div> <i class='fa fa-2x fa-camera'></i> hello world </div>
小提琴
http://jsfiddle.net/Hastig/V7DLm/180/
使用flex和字体很棒
足总5。x托管URL - https://use.fontawesome.com/releases/v5.0.8/js/all.js
div { display: inline-flex; /* make element size relative to content */ align-items: center; /* vertical alignment of items */ padding: 3px 10px; /* horizontal vertical position with padding */ border: 1px solid #ccc; } .svg-inline--fa { /* target all FA icons */ padding-right: 10px; } .icon-camera .svg-inline--fa { /* target specific icon */ font-size: 50px; } /* unnecessary styling below, ignore */ body {display: flex;justify-content: center;align-items: center;height: 100vh; flex-direction: column;}div{margin: 10px 0;}div {background-color: hsla(0, 0%, 87%, 0.5);}div:hover {background-color: hsla(212, 100%, 63%, 1);cursor: pointer;} <script src="https://use.fontawesome.com/releases/v5.0.8/js/all.js"></script> <div class="icon-camera"> <i class='fas fa-camera'></i> hello world </div> <div class="icon-clock"> <i class='fas fa-clock'></i> hello world </div>
小提琴
https://jsfiddle.net/3bpjvof2/
对我来说,只要让元素显示网格,并对齐内容就可以完美地工作。简单的解决方案。
.yourfontawesome<i>Element{
display: grid;
align-content: center;
}
这个问题几年前就有人问过了。我认为技术已经发生了很大的变化,浏览器的兼容性也更好了。你可以使用垂直对齐,但我认为它的可伸缩性和可重用性较差。我建议使用flexbox方法。
下面是原始海报使用的相同示例,但使用了flexbox。它为单个元素设置样式。如果按钮大小因某种原因而改变,它将继续保持垂直和水平居中。
.button {
border: 1px solid #ccc;
height: 40px;
margin: 60px;
padding: 4px;
display: flex;
justify-content: space-around;
align-items: center;
}
例如:JsFiddle
当使用flexbox时,字体和文字旁边的图标垂直对齐是非常困难的。我尝试了页边距和填充,但这移动了所有项目。我尝试了不同的弯曲对齐,如中心,开始,基线等,没有任何效果。调整图标的最简单和最干净的方法是将其包含的div设置为position: relative;上图:XX;这件事做得很完美。