CSS: Vertically center with margin: auto

Updated . Posted . Visible to the public. Deprecated.

We have card with all CSS centering options. You probably want to head over there and get an overview over what techniques are available for your use case and browser requirements.

Check out the jsFiddle Demo Show archive.org snapshot .

CSS

.absoluteCenterWrapper {
 position: relative; /* Declare this element as the anchor point for centering */
}

/* Positioning */
.absoluteCenter {
 margin: auto; /* Required */
 position: absolute; /* Required */
 top: 0; bottom: 0; /* Aligns Vertically */
 left: 0; right: 0; /* Aligns Horizontally */
}

/* Make sure the centered element fits into its container. If you know that's the case, you can omit this part. */
.absoluteCenter {
 max-height: 100%;
 max-width: 100%;
}

HTML

<div class="absoluteCenterWrapper">
 <img src="PATHTOIMAGE" class="absoluteCenter">
 <p>Paragraph goes here.</p>
</div>
Martin Straub
Last edit
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Martin Straub to makandra dev (2014-03-26 13:49)