---
url: "http://www.jakpsatweb.cz/css/css-vertical-center-solution.html"
titulek: "Vertical Centering in CSS"
h1: "Vertical Centering in CSS"
popis: ""
jazyk: ""
kanonicka: ""
drobecky: []
zdroj: "live"
wayback_snapshot: ""
stazeno: "2026-09-14 16:18:09"
znaku: 5832
slov: 862
kodovani: "utf-8"
sha256: "833664804c117bdccb3ad5b04d8c5bc62025f6aad9a3e0e159e734817560b204"
---
any text
any height
any content, for example generated from DB
everything is vertically centered
[See this example in
browser](priklady/vertical-align-final-solution-en.html)
## Let's make it structural and without hacks
(NOTE: this valid solution described below doesn't work in Internet Explorer
7 (standard mode), because IE7 doesn't understand table- values in display
property. The centered object is too high. BUT: IE 7 is used by only about 3
% of users (2012) and the number will decrease. If you still do mind IE7
users, please use the non-valid solution above, write in quirk mode, or use
html conditional comments for separating the properties for IE 7 somehow.)
The first example above is not nice, but I hope you have understood it. It's
possible to write code differently. For example this way:
any text
any height
any content, for example generated from DB
everything is vertically centered
And the structured valid style:
**[See the valid
example in browser](priklady/vertical-align-valid-solution-en.html)** (it looks the same way as the last one).
Color legend:
`CSS style for Internet Explorer 6 only (or quirk)
CSS styles for standard browsers`
CSS2 selector `#value[id]` is equivalent to selector `#value`,
but Internet Explorer 6 ignores these types of selectors with [id].
Generally: syntax
`*[foo]` means any element with attribute `foo`. Any
HTML element #something must have the attribute id by definition set to "something".
That's the trick -- `#value[id]` works in standard browsers only
(similarly works .value[class])
There's CSS property position set to absolute or relative for Internet
Explorer. The code `position: static` get's it back to default
value in standard browsers (property top doesn't work then).
## Both vertical and horizontal centering
The core code will be the same, you just need to add some extra CSS rules.
If is your page in standard mode, add this code:
``
As you probably see, this is the most common horizontal centering method -
auto left and right margin. Because margins needs the space in Firefox and
Opera, you need to set a width to the #outer div. If 100% doesn't fit your
needs, use any other value.
The important thing is to set some proper width to #inner. This tutorial is
about vertical centering of an object with unknown height. I assume that **you know the width** of the object (in most cases you will simply decide
how wide it should be). You may use the pixel values, or the percentage
width. If the centered object is only an unknown-size image, you don't need
to set width.
If you use quirk mode page rendering (mode depends on !Doctype, as you know),
added code should be a bit longer, because quirk mode of Exploder doesn't
understand auto margins, but has one nice bug in text-align interpretation
instead. This code should work for both standard and quirk mode:
``
Please take note that this is just part of code, which you have to add to
the previous example. If you are lazy to combine codes, please see the
complete example in browser:
[vertical
and horizontal centering](priklady/vertical-horizontal-align-valid-solution-en.html). You know, I'm lazy too.
## How to center vertically on window's height
The same way, and just add the style:
``
It seems that #outer declaration should be sufficient, but it is not. Body
and html declaration sets 100% of the window's height as a base for next
percentage. Now it is better not to set overflow: hidden (like in the
examples above), because when the content would be taller than window, then
it would be impossible to scroll on.
## Related
Previous attempt to center vertically:
- [Single line vertical centering](http://wellstyled.com/css-single-line-vertical-centering.html)
### Examples:
- [Universal
vertical center solution](priklady/vertical-align-final-solution-en.html)
- [Vertical
centering with structured and valid code](priklady/vertical-align-valid-solution-en.html)
- [Vertical and horizontal centering](priklady/vertical-horizontal-align-valid-solution-en.html)
### Read in other language:
[Centralizando verticalmente com CSS](http://www.maujor.com/tutorial/centrar-vertical.php) - Brazilian Portuguese translation of
this article by Maurício Samy Silva
[Вертикальне центрування в CSS](http://a2goos.com/blog/vertukalne-centryvannja-v-css.html) - Ukrainian translation by Agnessa Petrova
from A2Goos team
[Вертикальное центрирование в CSS](http://bisquit-mag.ru/blogs/vertikalnoe-tsentrirovanie-v-css/) - Russian translation by Aleksandr Molochan
[Czech version of this article](css-vertikalni-centrovani.html)
(original, not updated)
All other articles on this website
[www.jakpsatweb.cz](http://www.jakpsatweb.cz) is in Czech.
## About
First published Sep 21, 2004, updated Oct 23, 2006 and May 25 2008. Major
update Dec 6 2012 including bugfix of code in valid example. June 2015 major
update about flex. I'll update this article with more information if you
wish.