1

To vertically-center align text in a table-cell, I use vertical-align: middle in the td.

But the above doesn't work with divs, spans, header and other non-table elements. So how can I vertically-center align text in such elements ?

Andreas Grech
  • 105,982
  • 98
  • 297
  • 360

3 Answers3

2

If it's a single line of text, you can set the line height to be the same height as the element.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
2

There are many possibilities, each with it's advantages and drawbacks.

Here's a good article by Douglas Heriot explaining 5 others methods, and comparing them.

Bruno Reis
  • 37,201
  • 11
  • 119
  • 156
0

Just use the following style :

<style type="text/css">
    div {
            vertical-align: middle;
            display: table-cell;
        }
</style>

That way, you are "displaying" the div as a table-cell and since vertical-align works with table-cells, you're text will be center-vertically aligned.

Andreas Grech
  • 105,982
  • 98
  • 297
  • 360