Usually we all want our main content window to keep floating in middle of window when user resize browser or change display resolution and here is the small trick I used in my website (http://www.lakkakula.info)

Logic in CSS goes like

#yourdivid {
width : x px;
position: relative;
left: 50%;
margin-left: -x/2 px;
}

For example your div id is container and width is 600px
#container {
width: 600px,
position: relative;
left : 50%;
margin-left: -300px;
}

5 Responses to “CSS- How to keep a Div always center to Window?”

  1. Alex Says:

    Your solution is not working with IE6 (test news.php on my website). Do you have a “IE6 friendly” solution?

    The Flash header sould be centered all the time (even if window width < Flash width).

  2. shaggy Says:

    .Container {
    *text-align:center; /* if you want ie<6 to center, i usually leave this out*/
    }
    .Main {
    *text-align:left; /* ‘*’ hides from smart browsers */
    max-width:1000px;
    _width:1000px; /* for ie ie<7 */
    margin:auto;
    }
    /* semi stretchy layouts see. romack.net */

  3. Dion Says:

    I am not getting it,
    The first one works on Firefox.
    The second one doesn’t work at all?
    Can you please explain this more clearly?

  4. Dion Says:

    I think this is a better solution.

    #container {
    width: 600px,
    position: absolute;
    left : 50%;
    margin-left: -300px;
    }

  5. Zach Mayberry Says:

    I tend to use the solution:

    margin: 0 auto;

    It always works for me…

Leave a Reply