Andreas Grech
Andreas Grech

Reputation: 108000

CSS : How do I vertically-center align text in a div ? (or any other elements which is not a table)

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 ?

Upvotes: 1

Views: 3102

Answers (3)

Bruno Reis
Bruno Reis

Reputation: 37832

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.

Upvotes: 2

Guffa
Guffa

Reputation: 700670

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

Upvotes: 2

Andreas Grech
Andreas Grech

Reputation: 108000

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.

Upvotes: 0

Related Questions