Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - How to center nested divs?

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

like image 287
Rahul Avatar asked Dec 07 '11 14:12

Rahul


2 Answers

You could use relative positioning:

http://jsfiddle.net/a879W/

like image 72
ptriek Avatar answered Oct 23 '22 15:10

ptriek


#formpanel {
    position: relative;
    ...
}

#loginForm {
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -100px;
    margin-left: -100px;
    width: 200px;
    height: 200px;
    background-color:blue;
}
like image 2
U-DON Avatar answered Oct 23 '22 17:10

U-DON