CSS and Properly Centering

Many times I get asked “How do I center something in CSS?”
My first instinct would be to tell anyone that to properly align an element center in CSS, you must look at the elements margins. In most browsers using margin:auto; will align elements to the center of its parent element. However one must be careful when using margins of auto because in some versions of IE this simply will not work leaving us confused and baffled. Here is a non fixed centered object that will work fine in all other browsers but not IE5.

Example of problem centering

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.

How do we fix this issue you might ask? Simple but first we must understand how IE thinks. When you declair a margin:auto; you are basically doing two things. First you are writing short hand code that applys our “auto” to all four sides to our elements. Other short hand methods would be something like this:

margin:10px 0px 10px 0px;

This short hand method is applying 10 pixels of margin to our elements top and bottom. So think of this short hand method as this.

margin:top right bottom left;

Always in that order! So again, when we use margin:auto; we are ignoring having to write all four sides by stating it once and only once for all of our four sides.
Why is this important to us? Simple because some versions of IE don’t like to obey short hand styles and only want to listen to non short hand methods ONLY. So what would a non short hand method look like? Well due to people constantly using short hand methods some people have forgotten or never learned about.

margin-top:;
margin-right:;
margin-bottom:;
margin-left:;

So to fix our margin:auto; problem we do away with short hand methods and declare both the left and right margins to have an auto. There is no need to declare the margin top and bottom if we are trying to align center because top and bottom are for vertical alignment whereas left and right are horizontal.

margin-left:auto;
margin-right:auto;

Next step to fixing the align issue is to dig even deeper into the creepy world of how some versions of IE think. These wacky versions of IE will only recognize that you are trying to uses these margins of auto for a centering purpose if they specifically told to do so. How do we tell IE what we are using the margins of auto for? Simple but annoying, the answer is actually not in the element of question, but rather its parent element. To inform IE that we wish to use these margins of auto for aligning purposes we must go to its parent node and apply a text-align rule. Because our ultimate goal is to center our element we apply a text-align of center to our parent element.

#parentNode {text-align:center;}
#childElement{margin-left:auto; margin-right:auto;}

Perfect now we got IE just where we want it! But one last thing to remember, because we set its parent to have a text-align of center, all other elements nested in this parent elements will also have a text-align of center unless told otherwise. To do this we create a simple selector to all elements in this parent node. Something like this.

#parentNode div{text-align:left;}

Or for all paragraph elements

#parentNode p{text-align:left;}

Knowing what elements will be nested in your parentNode is completely up to you, for me however I always use div elements and or paragraph elements for content and structure. Again for me the above two rules seems to reach most of if not all of my nested elements who will contain text or rather content.
And that is how we properly center elements across all browsers!

Example of fixed centering

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.

Devin R. Olsen

Devin R. Olsen

Located in Portland Oregon. I like to teach, share and dabble deep into the digital dark arts of web and game development.

More Posts

Follow Me:
TwitterFacebookGoogle Plus