我正在努力使HTML/CSS中的文本之间很好地匹配一个图标。
似乎这在display:inline-block;中是可能的。然而,由于幻影填充,这似乎总是出错。
<div style="">
<div style="display:inline-block;">Text here</div><img style="height:24px;display:inline-block" src="{{ url_for('static', filename= 'images/icon-unsorted.png' ) }}" />
</div>

如您所见,外部div变为25.64px高,而它的两个子元素都是24 As高。
此外,图像在底部对齐,而文本对齐到外部div的顶部。
不过,我希望两者都是垂直(中心)对齐。
如果我尝试将外部div强制到24 If :<div style="height:24px;">,则内容仍然不正确地对齐,并且图标只是溢出在其父项之下。

所以我想我的问题是:
如何使图像很好地适合于一行文本,其中图像与文本具有相同的高度,或者两者都垂直对齐?
发布于 2020-11-18 04:29:09
我可能会尝试一种更面向css的方法。下面我举了一个小例子。
.btnStyle {
position: relative;
display: inline-flex;
width: 110px;
height: 24px;
text-align: left;
font-family: Helvetica, sans-serif;
color: black;
background-image: url(https://extortionguild.com/images/icon-unsorted.png);
background-size: 17.5px 100%;
background-position: right center;
background-repeat: no-repeat;
margin-right: 20px;
}
table {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: calc(100% - 20px);
font-size: 18px;
}
table td {
vertical-align: middle;
}<div style="display:inline-block; width: 100% height: 40px;">
<div class="btnStyle"><!-- Button 1-->
<table>
<tr>
<td>
Text here
</td>
</tr>
</table>
</div>
<div class="btnStyle"><!-- Button 2-->
<table>
<tr>
<td>
Text here
</td>
</tr>
</table>
</div>
<div class="btnStyle" style="width: 200px;"><!-- Button 3 - wider-->
<table>
<tr>
<td>
Even More Text here
</td>
</tr>
</table>
</div>
</div>
·使用类还可以通过btnStyle类对所有按钮进行更改,而不必单独更改每个按钮。
·表有一个“内置”垂直对齐:中间;选项,这是一个非常简单的方法,使任何单元格内容垂直中心。-- 文档
·您需要将背景图像url编辑为背景图像:url(图像/图标-unsorted.png);以加载图像文件。
https://stackoverflow.com/users/10741568/development-ninja在使用"flex“时也走在正确的轨道上,但它需要是”内联-flex“,否则他们会尝试堆叠。-有关内联-flex浏览器支持信息,请单击https://caniuse.com/?search=display%3A%20inline-flex
https://stackoverflow.com/questions/64886296
复制相似问题