Saturday 04 February 2012

Center Absolute Position DIV

iStock_000015512331XSmall

Centering a div on the screen with absolute position is something may at first seem very complicated, but with a little thinking and simple addition to the position element you can easily center an absolute position div in the middle of the screen.

This can be helpful for creating a wrapper to contain and center the contents of your entire site on the page.

Lets say that you have a simple page like this:

<div id="xyz_main">
    <div id="xyz_header"> Header </div>
    <div id="xyz_body"> Body </div>

    <div id="xyz_footer"> Footer </div>
</div>

And you wish to center the ‘main’ div in the center of the page as an absolute position center div. You can do this by adding the following positional style attributes to the ‘main’ div:

#xyz_main {
    width: 140px;
    border: 1px solid #666666;
    text-align: left;
    position: absolute;
    top: 0px;
    left: 50%;
    margin-left: -70px;
}

By setting the left position to 50% it will set the div to start in the center of the screen. By adjusting the left margin by a value half of the size of the div we are centering, we can move the entire div to the left and place it directly in the center of the page.

Your page would now look something like this:


Header
Body content here…

That’s it! That is all you have to do to center an absolute position div on the page… Enjoy!







NOTE: I have added a new CSS Template Cheats section to the site. This new resource has even more compliant methods of centering DIVs.





About the author

Founder of Eos Designs and co-founder of iPrintEasy and a number of other web businesses - an entrepreneur with over 10 years of experience in Internet Marketing - Zach understands what it takes to be successful online and specializes in the development of advertising campaigns and marketing plans for start-ups.
  • Amber

    This is genius. Totally fixed my dilemma! Thank you!

  • http://www.servermasterz.com Enes

    This trick has really hepled me a lot dude! Thanks for your effort and keep it up =)

  • http://ratemygames.blogspot.com/ arnold

    What the hell never even crossed my mind. This is great

  • pointstone

    Thanks, just used your idea to fix a issue with the css of http://www.pointstone.com

  • http://www.shoppinghermes.com hermes kelly bag

    Kinda surprised that no one has pointed this out yet (or maybe I missed it) – Do all of you really do your layouts in pixels and never want to vertically align something?

    It’s a nice solution for people who do *fixed-pixel* layouts, but folks who do elastic layouts (based on percentages, which is often preferred for presentation on a variety of screen resolutions) this does NOT work WITH HEIGHTS. It does still work with percentages and widths, but from all my testing, the results are very unexpected when using something like:

    height: 90%;

    top: 50%

    margin-top: -45%;

    • Kjlj

      I think percentages are preferred because then it doesn’t matter what resolution the user’s screen is

  • Brian

    This worked for me and I don't have to know the sizes of anything:

    left: 50%;
    margin-left: -25%;

    • Asd

      anybody know if this is cross-browser friendly? Using percentages in margin?

  • http://michaelstorer.com mike storer

    Can someone please sum up everything above this comment for us dummies?

    I mean, write out an entire version that works with all browsers and allows for any chosen amount of blank space above the top and below the bottom of the page?

    I don't like to have my pages start at the exact top of the browser window. I think they look cleaner if, say, 100px of body background-image or background-color is visible at the top as well as at the page's bottom.

  • Eric

    I love you! No need to keep pulling my hair out. I am using a carousel behind the site and need to have the images clickable all around. This was the answer I needed!

    Thanks!

  • http://pro-xy.org matej

    absolute horizontal centering:

    margin: 0 auto;

  • EYla.ela

    hey super thanks with this!! i was freaking out here, thinking how to center my div. coz every time i open my created web page in different comp with diff. size of monitor, it rumbles. thanks!! :) chao.

  • owmonths

    thank u bro… it's useful for me

  • gai

    simple and great. Thanks

  • Cagline

     #xyz_main {
    width: 140px;
    border: 1px solid #666666;
    text-align: left;
    position:absolute;
    right: 0;
    left: 0;
    margin:0 auto;
    }

    • http://wescook.ca/ Wes Cook

      Thank you very much Cagline, this was very helpful to me.

    • http://www.redfez.com.au/ David Jardine

      Me too man, that solved a nightmare for me thanks a bunch!!

    • Ronverbal732

      so much better than the solution up there *thumbs up*

  • Oscardominguez

    Thank you so much for the div centering material.
    Oscar

  • Salmanlp

    stupid example this is css hack fucker!!!!

  • Sunilgandle

    how to mainwrapper div align center in ie8?

  • http://www.ytsweb.net Yehonatan

    Thanks!!!

     realy helped me!

  • Øyvind Knobloch-Bråthen

    This worked perfectly for my needs. Thanks a lot :)

  • No

    YAY!!!!!

  • Estrangementx2

    Absolute Genius. This is one of those simple things that once you think about it, it’s a slap in the face!

  • PumpkinPatch

    VERY helpful and clever! thanks so much!

  • http://www.facebook.com/HARVS1789UK Joe Harvey

    Can’t believe I had never thought of this. Thank you very much :-)

  • Xfdxfdfdfdfdf

    hokay

  • Joseareland

    Nice solution and inspiring for me. Thank you.

  • Jantje

    Can some people speak Dutch?

  • Peter Griffin

    Thank you very much! this helped me alot :D

  • Jessica

    You are my hero! I was trying to get a div height to be the full height of the browser screen, but the background image wasn’t filling the screen completely to the bottom in Firefox. This fixed it for me.

  • Gregor Gresak

    Thanks! This is a great trick!

  • Daniel Milstein

    Good Share.I hope more people discover your blog because you really know what you’re talking about.  Can’t wait to read more from you!

  • Greatful Reader

    Thank you, great idea. Just saved me as my JS version had an annoying jump while it was calculating position

  • Ndznkj

    Thanks a lot

Powered by WordPress | Privacy Policy | Web Design Miami