我在 div 内垂直对齐 3 个跨度时遇到问题。这很容易实现,但是当我使用 float 时垂直对齐不起作用。我希望浅蓝色条垂直居中。代码:
.container {
}
.text-1 {
float: left;
padding-right: 10px;
}
.bar {
background-color: lightblue;
border-radius: 5px;
float: left;
height: 5px;
width: 150px;
}
.text-2 {
padding-left: 10px;
}
<div class="container">
<span class="text-1">Text 1</span>
<span class="bar"> </span>
<span class="text-2">Text 2</span>
</div>
非常感谢您的帮助。
最佳答案
您可以使用display: inline-block;
以及vertical-align: middle;
在您的<span>
上元素而不是 float 。这样它们也可以彼此相邻放置,您可以应用垂直对齐:
.container span {
display: inline-block;
vertical-align: middle;
}
.text-1 {
padding-right: 10px;
}
.bar {
background-color: lightblue;
border-radius: 5px;
height: 5px;
width: 150px;
}
.text-2 {
padding-left: 10px;
}
<div class="container">
<span class="text-1">Text 1</span>
<span class="bar"> </span>
<span class="text-2">Text 2</span>
</div>
关于html - 跨度与 float 垂直对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40481205/