Alex Rivera | Logout

CSS - How to center nested divs?

Asked 2011-12-07T14:54:37.890
8

New to CSS.

I am trying to center nested divs using the code below

HTML

<html>
    <head>
        <title>My website</title>
        <link rel="stylesheet" type="text/css" href="css/main.css" />
    </head>
    <body>
        <div id="wrapper">
            <div id="formpanel">
                <div id="loginForm">
                </div>
            </div>
        </div>
    </body>
</html>

CSS

body {
    margin: 0;
    background : #90ADB7 url('images/background.png') repeat-x;
    font-family: verdana, sans-serif;
    font-size: 0.85em;

}

#wrapper {
    width: 960px;
    margin: 0 auto;
    border-style:solid;
    padding: 190px 0;
}

#formpanel {
    width: 400px;
    height: 400px;
    background-color: yellow;
    margin: auto;
}

#loginForm {
    margin: auto;
    width: 50%;
    height: 50%;
    background-color:blue;
}

the problem is that the innermost div (#loginForm) flushes with the top edge of the outer div (#formpanel). How should I center the inner div ?

Screenshot enter image description here

Edit
Report

1 Answer

0

You can use absolute positioning:

#formpanel {
    width: 400px;
    height: 400px;
    background-color: yellow;
    margin: auto;
    position:relative;
}

#loginForm {
    position: absolute;
    width: 200px;
    height:200px;
    left: 100px;
    top: 100px;
    background-color:blue;
}
answered 2011-12-07T14:58:56.967

Your Answer