Horisontal CSS Centering for Absolute Positioning

April 6th, 2005

A new and better way to center your elements horisontally has been found. Read all about it on AkaXakA’s website.

In my daily work, I needed to center a div horisontally. Usually this is easy. Not so when using absolute positioned elements.

Basically, absolute positioning allows you to type in coordinates for your div, coordinates from the top-left of the screen.

I found the following solution to work fairly well, and I’m posting it here in case someone else might gain from it, as well as a reminder to myself.

Give then Take

If you need to center your absolutely positioned #content div, wrap it in a new #content-container div and apply the following CSS.

This will move the #content-container div’s left side to the exact center. After that, the #content div will be moved half the width of the #content-container div’s width to the left, thereby centering it.

It works reasonably well in both Mozilla and IE, as long as the browser is larger than the centered content.

Update: Doesn’t work as well as I initally thought. If scale the browser to be smaller than the the width of the #content-container, it’ll bork. Suggestions are welcome.

Update: AkaXakA’s method is better. Read it in the comments.

Feel free to post your solutions in the comments.


Comment pages: « 1 [2]

  1. There must be something else I’m doing wrong. 

    I love your give and take method, it works without effecting the content, haven’t found the downside yet though I’m sure there is one.

    Donethat

  2. The give and take method goes a bit wonky when the window is smaller than the size of the wrapper - and if you work with percentages or em’s for your width you tend to get rounding errors.

  3. Haven’t had that problem yet. 

    When I have a little more time I shall play with the various methods.

    Thanks for your contributions.

    Donethat

  4. Wow, reading the comments here solved a huge number of problems I was having with css layout. For example, valign is as simple as:

    #container {
    position: relative;

    }

    #bottom {
    position: absolute;

    bottom: 0px;

    top: 0%;

    }

      

    similarly, using a container with position: relative/absolute and children with position: relative, the traditional struts & springs style of layout is possible, which I had not previously realized.

    Quote Michael Johnston said July 22nd, 2006, 22:00:
  5. Micheal

    Can you please show me how the HTML for this might work and have you tried it in IE yet?

    Take care

    Donethat

  6. I run the following, that is tested on IE, Firefox and Opera:

    body{
    margin: 0px;

    }

    DIV#container{

    position: absolute;

    margin-right: auto; 

    margin-left: auto;

    top: 0px;  your content

    Note however that this will cover any content on both sides of #content.

    Example of this is http://www.evasunderklader.se. It’s the top bar that is formatted with the above.

  7. Oops… the above was incomplete. Lets try again:

    body{
    margin: 0px;

    }

    DIV#container{

    position: absolute;

    margin-right: auto; 

    margin-left: auto;

    top: 0px;   whatever you want

    left: 0px;   VERY important

    width: 100%;   VERY important

    }
    DIV#content {

    width: 881px;   free of choice

    margin-right: auto;

    margin-left: auto;

    text-align: center;

    }

    the HTML is:
    your content

    Note however that this will cover any content on both sides of #content.

  8. OMG This really helped me a lot!!!

    Cheers to you!

    Quote Harley C. Dangan said November 26th, 2007, 07:56:
  9. I’m centering a transparent png logo on top of my header page using the absolute positioning. You have the right method, for the #content just replace the “px” with an elastic “em” and it will work both on IE and Firefox. It works absolutely fine on mine.

    Thanks and more power!!

    Quote Chad (subscribed) said May 16th, 2008, 11:25:
  10. Thanks so much. Lina’s code did the trick for me to enable me to display some progress bars that appear while a simulation is crunching away, than using the code I could cover up those progress bars once the simulation was finished and results could be displayed as if a new page is being rendered.

Comment

(required)

(required)

Formatting Your Comment

The following XHTML tags are available for use:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

URLs are automatically converted to hyperlinks.

Textile

Textile is a method that uses simple symbols to quickly write rich text markup. These are the most common:

  • _emphasis_
  • *strong*
  • -deleted text-
  • !imageurl.gif!

Preview: