How to vertically align text to the middle inside div?

Sometimes its a headache aligning the text elements inside div to the middle. The easiest solution is to add line-height value equal to the height of the div element.

Problem:
Suppose we have

<style>
#box{
  float:left;
}
#btn{
  height:50px;
}
</style>
<button id="btn">Click me!</button><div id="box">This is a text</div>

unaligned text with button

This does not look good because it is not vertically aligned to the middle. So the solution is to add line and line-height to the box div.

<style>
#box{
  float:left;
  height:50px;
  line-height:50px;
}

</style>

The final corrected output likes this:
aligned text with button

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

*