CSS Techniques – Absolute Horizontal And Vertical Centering In CSS

We’ve all seen margin: 0 auto; for horizontal centering, but margin: auto;has refused to work for vertical centering… until now!  But actually (spoiler alert!). I’m not the pioneer of this method, and it may even be a common technique, however, most vertical centering articles never mention it and I had never seen it until I dug through the comments section of a particular article. There, Simon linked to this jsFiddle that blew every other method out of the water (the same method was also mentioned by Priit in the comments). Researching further, I had to use very specific keywords to find some other sources for this method.

 ADVANTAGES:

  • Cross-browser (including IE8-10)
  • No special markup, minimal styles
  • Responsive with percentages and min-/max-
  • Use one class to center any content
  • Centered regardless of padding (without box-sizing!)
  • Blocks can easily be resized
  • Works great on images

LIMITATIONS:

  • Height must be declared (see Variable Height)
  • Recommend setting overflow: auto to prevent content spillover (see Overflow)
  • Doesn’t work on Windows Phone

BROWSER COMPATIBILITY:

Chrome, Firefox, Safari, Mobile Safari, IE8-10.
Absolute Centering was tested and works flawlessly in the latest versions of Chrome, Firefox, Safari, Mobile Safari, and even IE8-10.

Explanation

After researching specs and documentation, this is my understanding of how Absolute Centering works:
  1. In the normal content flow, margin: auto; equals ’0′ for the top and bottom.
    W3.orgIf ‘margin-top’, or ‘margin-bottom’ are ‘auto’, their used value is 0.
  2. position: absolute; breaks the block out of the typical content flow, rendering the rest of the content as if that block weren’t there.
    Developer.mozilla.org…an element that is positioned absolutely is taken out of the flow and thus takes up no space
  3. Setting top: 0; left: 0; bottom: 0; right: 0; gives the browser a new bounding box for the block. At this point the block will fill all available space in its offset parent, which is the body or position: relative; container. Developer.mozilla.orgFor absolutely positioned elements, the top, right, bottom, and left properties specify offsets from the edge of the element’s containing block (what the element is positioned relative to).
  4. Giving the block a width or a height prevents the block from taking up all available space and forces the browser to calculate margin: auto based on the new bounding box.Developer.mozilla.orgThe margin of the [absolutely positioned] element is then positioned inside these offsets.
  5. Since the block is absolutely positioned and therefore out of the normal flow, the browser gives equal values to margin-top and margin-bottom centering the element in the bounds set earlier.
    W3.orgIf none of the three [top, bottom, height] are ‘auto’: If both ‘margin-top’ and ‘margin-bottom’ are ‘auto’, solve the equation under the extra constraint that the two margins get equal values. AKA: center the block vertically
Absolute Centering appears to be the intended use for margin: auto; based on the spec and should therefore work in every standards compliant browser.
TL;DR: Absolutely positioned elements aren’t rendered in the normal flow, so margin: auto;centers vertically within the bounds set by top: 0; left: 0; bottom: 0; right: 0;.view demo

Comments