Spans vertical align with float

只谈情不闲聊 提交于 2019-12-11 04:16:46

问题


I have a problem with vertically align 3 spans inside a div. It's easy to achieve, but vertical align doesn't work when i use float. I want that lightblue bar to be vertically centered. Code:

.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">&nbsp;</span>
  <span class="text-2">Text 2</span>
</div>

Thank you very much for your help.

JSFiddle


回答1:


You can use display: inline-block; along with vertical-align: middle; on your <span> elements instead of float. This way they are positioned next to each other too and you can apply the vertical alignment:

.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">&nbsp;</span>
  <span class="text-2">Text 2</span>
</div>


来源:https://stackoverflow.com/questions/40481205/spans-vertical-align-with-float

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!