StylizedWeb

Subscribe to our updates:

A Design, WordPress and Tutorials Blog.

Dedicated to helping you learn the art and science of the web.

Vertical align div

One of current CSS left outs is vertical align div. And before CSS 3 comes we have to use some tricks to solve this problem. I looked over for some solutions and it all comes up to defining it as table and using vertical align. The second one appears a lot and it’s a nice solution, but it has two faults, I’ll tell you later about them, here’s the code first.

Idea is to place absolute div 50% left and top and then to move margin left and top half if it’s size.

Vector people

 
 .wrapper {
width: 100px;
height: 100px;
position: absolute;
top: 50%;
left: 50%;
margin-left: -50px;
margin-top: -50px;
}
 
Problem?

This works only with defined height and when div height is less then browser height so you can’t have scroll. Now you’ll ask why do anyone want vertical align when there’s a vertical scroll? Well for example you have container height 820px and you have it vertically centered in all larger resolutions, simple.

Solution

In this problem (when browser height is less than div height) at smaller resolutions 1/3 of div is not visible, it’s in negative top margin. So idea is to place some relative div that will prevent div to go into negative margin. Here is the code.

 
<div id="shim"/></div>
<div id="wrapper">
Content
</div>
 
html, body {
height: 100%;
margin: 0;
padding: 0;
}
 
* {
margin:0px auto;
padding:0;
}
 
div#shim {
visibility: hidden;
width: 100%;
height: 50%;
margin-top: -200px;
float: left;
}
 
div#wrapper {
width: 1000px;
height: 400px;
clear: both;
background:red;
position: relative;
top: -200px;
/* IE4ever Hack: Hide from IE4 **/
position: static;
/** end hack */
 
}
 
/* Hide from IE5mac \*//*/
div#shim {
display: none;
}
 
html, body {
height: auto;
}
/* end hack */
 
/* ]]> */
 

Only fault is again defined height and it can’t be solved with CSS, not right now, I hope new version will or some JS fix. Click here for demo

Leave a comment on Stylized Web Have some feedback? Leave a comment

103 Comments So Far

  1. This is a problem that has definitely plagued a lot of us designers/developers. I know that I certainly have tried to find a solution for this many times.

    Fortunately, many situations have allowed for me to set a height to the element I wish to apply the middle vertical align effect, and in a small area of the website, so it did not rise an issue with the scrolling of the page.

    I too hope that CSS3 will solve this, though I do believe that there is a JS fix for it, but I don’t know what it would be.

  2. By Berkana posted on February 1, 2008 at 11:46 am

    Should the code for div#wrapper say “margin-top:-200px;” rather than “top:-200px;” ?

  3. By Dejan posted on February 1, 2008 at 1:01 pm

    @ Berkana
    No, it has to be like this

    Click here for demo

  4. So in that demo, the red box is what is centered?

  5. By Dejan posted on February 1, 2008 at 10:23 pm

    @ Brian
    Yes that’s right
    Did you find answer from your forum question here?

  6. By Billgates posted on February 14, 2008 at 3:41 pm

    This is a working vertical DIV alignment (including clipping). No percentages used, and it works on both IE and FF.

    http://cssbeauty.com/skillshare/discussion/2607/real-working-div-vertical-align/

  7. By Dejan posted on February 14, 2008 at 6:14 pm

    @ Billgates
    Yes, but this is tableless code

  8. Sweet!
    I’ve used it, and it’s working!
    It’s not on the web yet though.
    I’ll let you know when.

    But let’s say I wanted to align it right – would all I have to include is:

    float:right

    ?

  9. By Dejan posted on February 16, 2008 at 1:18 am

    Well margin:0 auto; makes it on center, so remove that auto word and add right:1px;
    Try it and let me know if it works

  10. Pingback: 10 best CSS hacks

  11. Nice trick. But until css3 is perfected/implemented I would I would be fine w/ using a table because realistically it is more stable.

  12. Pingback: links for 2008-02-19 | dies und das | XSBlog2.0beta

  13. Pingback: 10 best CSS hacks « DELE DURO!!!

  14. Pingback: Los 10 mejores hacks CSS — Freak Group

  15. Pingback: Thatgrafix Web Design Portland Oregon

  16. Pingback: NilDesign.ru » 10 лучших CSS хаков + кроссбраузерность

  17. Pingback: CSS-FAQ » Blog Archive » CSS Hacks for You! (First Serving)

  18. Pingback: 10 best CSS hacks « Share4Vn.com Blog

  19. I agree with everybody else: awesome story.! Much food for thought… It really made my day. Thank you.

  20. That IS progress. Your blogging skills are getting better and better. I had a great time reading this post.

  21. Pingback: 10 buenos hacks para CSS « SylarPoOoX

  22. Hi. Many thanks for interesting guides. I’ve been looking to this page from time to time, and I’m really amazed about your skills! Thanks & take care!

  23. By Dejan posted on May 8, 2008 at 12:09 pm

    you are welcome ;)

  24. Works perfectly! You are very talented person Dejan! I’m glad I could visit your page. Thank you. :)

  25. By Dejan posted on May 19, 2008 at 7:38 pm

    thank you very much :D

  26. Hi There! I’m from Poland and came to you website by google, as I’m here, I would like to thank you for all thoose great tips! Keep up this good work! :)

  27. Your site is a lifesaver, I scraped the internet for hours on end looking for a cross-browser solution. I even started brute-forcing CSS combination, the closer I got on one browser, the further away I got on another.

    I Love You

  28. Pingback: vertical align

  29. please give the rounded shape box on table less design

  30. Useful article, but mainly I love the design of your site! The use of sky, sea and seabed for the three main sections of a page is really sweet.

  31. By Dejan Cancarevic posted on June 18, 2008 at 4:20 pm

    Geat,
    heh thanks dude!

  32. By sanjay posted on June 26, 2008 at 8:36 am

    Hi dude,

    i want to learn css. Please provide tricky things

  33. Pingback: Email:luvAdobe@gmail.com » 10 best CSS hacks

  34. Pingback: Best Css hacks · Css Edge :: Your best CSS resource guide

  35. Pingback: 10 Great CSS Hacks « Thatgrafix Blog

  36. It can be much, much easier:

    html, body, #table { height:100%; }
    body {
    text-align:center;
    display:table;
    width:100%;
    }
    #table {
    display:table-cell;
    vertical-align: middle;
    }

    The IE fix:

    #container {
    margin-top:expression(((offsetParent.offsetHeight-offsetHeight)/2)+’px’); }

  37. How to align two images in the same line?

  38. skldfjoisdufoawjrflksjdfljsdlfjoiasdufoweaurouweorjldflkdjfldjfauruweioruf

  39. how to align two images in the same line

  40. By Bijomon posted on December 10, 2008 at 7:55 am

    #outer {width: 100%; height:60%; text-align:center;}
    #inner {width: 402px; margin: 20% auto 20% auto;}

    u can set the [outer] height’s size as per your requirement

  41. Hello ,

    Using negative margin brings some problem if the screnn height or wigth is les than the centered element , and it is a quiet old trick .

    Min-width and min-height can avoid these ‘accessibilty ” troubles , a turn around is even possible in IE6 if you use a standard mode .

    As long as IE7 will be around , you won’t be abble to beat the table “element” for real efficient or non buggy vertical-align elsewhere you have a td or 2 inline elements to vertical center .

    Beside the table elements , i propose 4 ways to center a div element or whatever in a page without a table : http://yidille.free.fr/plux/valign/index.php?004/Css-gabarits
    (a demo inline is there and then down the bottom links to full template html page , to show it works either for a page or a portion of a page … i’m french , so the page i written in french , … css doesn’t care about the language you speak , so look at the source if youre curious :) )

    For each and if needed , i propose an IE alternative .
    The exemple are using a valid doctype because it’s now end of 2008 :)

    (sorry , but i hate this negative margin if you let the content able to diseappear on top or left side , it just doesn’t help )

    GC

  42. Hi ,

    I still believe , negative margin (with absolute or relative positionning ) is not ‘accessible’ nor really efficient , browsers are improving ther behaviors :) ..

    Nowdays , beside te real table (must be styled with CSS ) you can decently look after : inline-block and get used to display:table;

    Display:table; will finally be understood by IE 8 , it’s about time to learn to use it and do not confuse table & css , IE6 & 7 will survive for a while . So not really usable in a professionnal context or website yet .

    Inline-block , is now understood too bye FF3 , FF2 has a work around easy to set up (& usershould not take years to update) ,
    Conditionnel Comment needed for IE 7 and below of course to serve an inline-block css compatible with block elements in standard mode (it’s all about haslayout ).

    Where it is more efficient , is that you don’t need to know or set the height of the element to center. … you can set a few elements aside and vertical aligned . (inline-block requiert 2 elements :o ne that takes heights of screen or minimum height to set in a box wherever i, a page , a second one wich the vertical centered elements container.

    The CSS rules used for FF2 and below , needs an extra wrapper to control the stakking effect , this element is useful too to contain white-space in IE if used … because of that one pixel jog .

    see it here (css inside head section) : http://yidille.free.fr/plux/valign/uploads/centre-inline-stack.html

    last call :)
    respects

    GC

  43. oups , too many post , just keep one if you like

    Regards (a french css player)

  44. Hey dude nice hacks. Thanx 4 da tips.

  45. By scalesy posted on February 10, 2009 at 5:06 pm

    Thanks for the code – have been looking for somthing like this for a while… making a website for a friend and it’s embarrasing to not know how to vertically center something on a page!!!

    I still have a little problem though… works great in FF3, but in IE7 the div won’t center horizontally – seems to be glued to the left of my broswer window.

    Can anyone suggest what the problem might be?

  46. By scalesy posted on February 10, 2009 at 6:33 pm

    Ignore that last post – I’d set the wrapper div’s width wrong… guess I’m a bit of a div.

  47. Pingback: Vertical align div | StylizedWeb.com | CSS Tutorials - CSSHelper.net

  48. Turkisch free mp3 download.

  49. Pingback: PureCSS » Blog Archive » 关于绝对居中问题

  50. Отлично написано, приятно полистать ваш сайт!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>