Zykson Zykson - 1 month ago 17
CSS Question

Spans vertical align with float

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

Answer

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>