Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center text vertically

How can I vertically center some simple text in a normal <div>? I have markup like this:

<div style="height:200px;">
   <span style="display:inline; vertical-align:middle;">ABOUT</span>
<div>

It doesn't work under either Firefox or Internet Explorer. Why not?

like image 942
Jacek Avatar asked Aug 28 '12 17:08

Jacek


People also ask

How do I center text vertically in HTML?

For vertical alignment, set the parent element's width / height to 100% and add display: table . Then for the child element, change the display to table-cell and add vertical-align: middle . For horizontal centering, you could either add text-align: center to center the text and any other inline children elements.

How do you center absolutely vertically?

To center an element both vertically and horizontally: position: absolute; left: 0; right: 0; top: 0; bottom: 0; margin: auto; But if you would like to understand how I came to these solutions, read further for more explanation.

How do I center text vertically in Word for Mac?

1 Select the text you want to center between the top and bottom margins. 2 On the Page Layout tab, click the Page Setup Dialog Box Launcher. 3 Select the Layout tab. 4 In the Vertical alignment box, click Center 5 In the Apply to box, click Selected text, and then click OK.


2 Answers

That's not what vertical-align does. You probably want line-height:

<div>
    <span style="line-height: 200px;">ABOUT</span>
</div>

Here's a demo.

like image 76
Ry- Avatar answered Sep 30 '22 07:09

Ry-


I have found using a spacing element to the most reliable method of centering any element you know the height of (image, text). This will center an element in a container of any height.

CSS:

#outer {
    background: grey;
    height: 200px; /* any height */
}

#outer > div {
    height: 50%;
    margin-bottom: -6px;
}

#outer span {
    font-size: 12px;
}

HTML:

<div id="outer">
    <div></div>
    <span>Example</span>
</div>
like image 32
MaxPRafferty Avatar answered Sep 30 '22 06:09

MaxPRafferty