vertical-align
, or "How (Not) To Vertically Center Content"A FAQ on various IRC channels I help out on is How do I vertically center my stuff inside this area?
This question is often followed by I'm using
vertical-align:middle
but it's not working!
The problem here is three-fold:
vertical-align:middle
isn't doing what is desired want is because the author doesn't understand what it's supposed to do, but …vertical-align
is used to specify two completely different behaviors depending on where it is used.vertical-align
in table cellsWhen used in table cells, vertical-align
does what most people expect it to, which is mimic the (old, deprecated) valign
attribute. In a modern, standards-compliant browser, the following three code snippets do the same thing:
<td valign="middle"> <!-- but you shouldn't ever use valign --> </td>
<td style="vertical-align:middle"> ... </td>
<div style="display:table-cell; vertical-align:middle"> ... </div>
Shown in your browser, the above (with appropriate wrappers) display as:
<td> using valign="middle" |
<td> using valign="bottom" |
<td> using vertical-align:middle |
<td> using vertical-align:bottom |
<div>
using display:table-cell; vertical-align:middle
<div>
using display:table-cell; vertical-align:bottom
vertical-align
on inline elementsWhen vertical-align
is applied to inline elements, however, it's a whole new ballgame. In this situation, it behaves like the (old, deprecated) align
attribute did on <img>
elements. In a modern, standards-compliant browser, the following three code snippets do the same thing:
<img align="middle" ...>
<img style="vertical-align:middle" ...>
<span style="display:inline-block; vertical-align:middle"> foo<br>bar </span>
In your browser, here's how the above code renders:
In this paragraph, I have two images— and —as examples.
In this paragraph, I have two images— and —as examples.
In this paragraph, I have a cute little <span>
display:inline-block
vertical-align:middle and display:inline-block
vertical-align:text-bottom as an example.
vertical-align
on other elementsTechnically, this CSS attribute doesn't go on any other kinds of elements. When the novice developer applies vertical-align
to normal block elements (like a standard <div>
) most browsers set the value to inherit to all inline children of that element.
If you are reading this page, you're probably not as interested in why what you were doing is wrong. You probably want to know how to do it properly.
The following example makes two (non-trivial) assumptions. If you can meet these assumptions, then this method is for you:
If you can accept the above necessities, the solution is:
position:relative
or position:absolute
.position:absolute
and top:50%
on the child container to move the top down to the middle of the parent.margin-top:-yy
where yy is half the height of the child container to offset the item up.An example of this in code:
<style type="text/css">
#myoutercontainer { position:relative }
#myinnercontainer { position:absolute; top:50%; height:10em; margin-top:-5em }
</style>
...
<div id="myoutercontainer">
<div id="myinnercontainer">
<p>Hey look! I'm vertically centered!</p>
<p>How sweet is this?!</p>
</div>
</div>
In your browser, the above example renders as:
Hey look! I'm vertically centered!
How sweet is this?!
This method requires that you be able to satisfy the following conditions:
If you can accept the above necessities, the solution is:
line-height
of the parent element to the fixed height you want.An example of this in code:
<style type="text/css">
#myoutercontainer2 { line-height:4em }
</style>
...
<p id="myoutercontainer2">
Hey, this is vertically centered. Yay!
</p>
In your browser, the above example renders as:
Hey, this is vertically centered. Yay!