Search code examples
csshtmlalignmentcss-positioncenter

How to center a div with a specific width and fixed positioning?


I am coding, and trying to center my div horizontally in the middle of the page. It's just a plain div, with nothing inside of it right now. My code is this:

#wrapper{
width:500px;
height:100%;
position:fixed;
background-color:#fff;
}

Basically, I just want a 500px width white rectangle centered in the page horizontally, that takes up 100% of the page's height.

Thanks.


Solution

  • you can assign left:50% and after margin-left (width-of-div / 2), in this way you can center div in fixed/absolute position

    try this:

    #wrapper{
       width:500px;
       height:100%;
       position:fixed;
       background-color:#fff;
       left:50%;
       margin-left:-250px;
    }
    

    DEMO