How to do vertical centering in CSS

Lots of people think it's hard or even impossible to vertically center things in CSS. It's not; here's how.

It feels like it should be a simple task but it's easy to tie yourself in knots if you don't know any good tricks for vertical centering. Jake Archibald has two neat ways for vertically centering things in CSS.

Here's his method for modern browsers, as hosted on the collaborative code pastebin JsBin:

Vertical centering in modern browsers, by Jake Archibald

Click on the image to see a larger version

And here's one that works for browsers going back to IE8:

Archibald's method for centering in old browsers

Click on the image to see a larger version

You can follow some further discussion about these and other methods on Twitter, here.

Liked this? Read these!