So I know we can center a div horizontally if we use margin:0 auto;
. Should margin:auto auto;
work how I think it should work? Centering it vertically as well?
Why doesn't vertical-align:middle;
work either?
.black {
position:absolute;
top:0;
bottom:0;
left:0;
right:0;
background:rgba(0,0,0,.5);
}
.message {
background:yellow;
width:200px;
margin:auto auto;
padding:10px;
}
<div class="black">
<div class="message">
This is a popup message.
</div>
</div>
Here's the best solution I've found: http://jsfiddle.net/yWnZ2/446/ Works in Chrome, Firefox, Safari, IE8-11 & Edge.
If you have a declared
height
(height: 1em
,height: 50%
, etc.) or it's an element where the browser knows the height (img
,svg
, orcanvas
for example), then all you need for vertical centering is this:You'll usually want to specify a
width
ormax-width
so the content doesn't stretch the whole length of the screen/container.If you're using this for a modal that you want always centered in the viewport overlapping other content, use
position: fixed;
for both elements instead ofposition: absolute
. http://jsfiddle.net/yWnZ2/445/Here's a more complete writeup: http://codepen.io/shshaw/pen/gEiDt
Those two solution require only two nested elements.
First - Relative and absolute positioning if the content is static (manual center).
https://jsfiddle.net/GlupiJas/5mv3j171/
or for fluid design - for exact content center use below example instead:
https://jsfiddle.net/GlupiJas/w3jnjuv0/
You need 'min-height' set in case the content will exceed 50% of window height. You can also manipulate this height with media query for mobile and tablet devices . But only if You play with responsive design.
I guess You could go further and use simple JavaScript/JQuery script to manipulate the min-height or fixed height if there is a need for some reason.
Second - if content is fluid u can also use table and table-cell css properties with vertical alignment and text-align centered:
and
Works and scale perfectly, often used as responsive web design solution with grid layouts and media query that manipulate the width of the object.
https://jsfiddle.net/GlupiJas/4daf2v36/
I prefer table solution for exact content centering, but in some cases relative absolute positioning will do better job especially if we don't want to keep exact proportion of content alignment.
You can't use:
vertical-align:middle
because it's not applicable to block-level elementsmargin-top:auto
andmargin-bottom:auto
because their used values would compute as zeromargin-top:-50%
because percentage-based margin values are calculated relative to the width of containing blockIn fact, the nature of document flow and element height calculation algorithms make it impossible to use margins for centering an element vertically inside its parent. Whenever a vertical margin's value is changed, it will trigger a parent element height re-calculation (re-flow), which would in turn trigger a re-center of the original element... making it an infinite loop.
You can use:
A few workarounds like this which work for your scenario; the three elements have to be nested like so:
JSFiddle works fine according to Browsershot.
Since this question was asked in 2012 and we have come a long way with browser support for flexboxes, I felt as though this answer was obligatory.
If the display of your parent container is
flex
, then yes,margin: auto auto
(also known asmargin: auto
) will work to center it both horizontally and vertically, regardless if it is aninline
orblock
element.Note that the width/height do not have to be specified absolutely, as in this example jfiddle which uses sizing relative to the viewport.
Although browser support for flexboxes is at an all-time high at time of posting, many browsers still do not support it or require vendor prefixes. Refer to http://caniuse.com/flexbox for updated browser support information.
Update
Since this answer received a bit of attention, I would also like to point out that you don't need to specify
margin
at all if you're usingdisplay: flex
and would like to center all of the elements in the container:Using Flexbox:
HTML:
CSS:
View result
I know the question is from 2012, but I found the easiest way ever, and I wanted to share.
HTML:
and CSS: