Vertically center text using CSS

PostFeb 13th, 2006 | Comments (13)
One of the big problems with using CSS is that there is no easy way to vertically center text in a <div>. I've seen various hacks using negative margins, but sometimes there is a much easier way using the "line-height" property.

NOTE: This only works if you know the height of the <div> and there is only one line of text.

Here is a typical <div> box:

.box {
    height: 75px;
    width: 100px;
    border: 1px solid #666;
    text-align: center;
}

It looks like this:



To vertically center the text, all we need to do is add a line-height value equal to the height of the box:

.box {
    height: 75px;
    line-height: 75px;
    width: 100px;
    border: 1px solid #666;
    text-align: center;
}

Now the text is vertically centered:



This solution is certainly limited, but in certain situations it is very useful.

Comments

kiranMar 26, 2006
hi it work fine, thank u :)
0h4crying0utloudMar 27, 2006
It's a crying shame that this is so kludgy in CSS. Thanks so much for the tip, this will work for me too.
boboSep 30, 2006
And when the user with poor site resizes the text in their browser, it all goes to hell.
3ndSep 3, 2007
awesome - but it only appears to work in IE - d'oh!
Jamie TibbettsSep 3, 2007
It works in every browser.
KejlsnOct 26, 2007
Tnx. Worked out perfectly for my tiny problem :)
DeepaNov 23, 2007
Sir,
I am tryig to vertically align text (to the right-bottom) Foll is the stylesheet
.author
{
background-image:url(Feather.jpg);
background-repeat: no-repeat;
background-attachment:fixed;
background-position: right bottom;
height:100px;
width:110px;
align:right;
vertical-align: bottom;
border:1px solid black;
}

and the HTML code is
<div class="author">by AUTHOR</div>

but somehow the text is not getting displayed at the right bottom postion of the div area

Can you please help me solve this problem
thank you
Deepa
Jamie TibbettsNov 23, 2007
Deepa-

The vertical-align property isn't what you think it is. It's more akin to the old "valign" property for <img> tags.

To place text at the bottom of an element using the line-height technique from this post, change the line-height to double the height of your element minus half the size of the font.

Line-height = 2 x div height - (font size/2)

So if you were using 12px text, your line-height would be:

2 x 100px - (12/2) = 194px;

The drawback to this solution (as well as my original post) is things get all out of whack when/if the user changes his font display size via his/her browser.
c00chDec 15, 2007
Another disadvantage of using line-height is of course that it doesn't work if you got a line break, i.e. the text is on two lines rather than one.
jimFeb 2, 2008
Worked Perfect! Thanks
MicoMar 11, 2008
I was looking for this solution 2 days! Now my site is standard compliant. Thank you maaaan!
philMay 13, 2008
awesome. thanks so much!
bradAug 10, 2008
thanks man i was trying so hard to figure this out

Post a comment

Name
URL
Email
Comment