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
You could use relative positioning:
http://jsfiddle.net/a879W/
#formpanel {
position: relative;
...
}
#loginForm {
position: absolute;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -100px;
width: 200px;
height: 200px;
background-color:blue;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With