...

View Full Version : margin auto to align center



johnmerlino
05-07-2010, 12:24 AM
Hey all,

I read that you can use the css property margin: auto auto; to align content in the center of its container. I have the following structure:
<div id="main">
<form>
<input id="deployment_name" type="hidden" />
<input id="return_url" name="return_url" type="hidden" />
<div class='field'>
<label for="email">Email</label>
<input id="email" name="email" type="text" />
So what I want to do is align the label and the input field in the center of the main container. However, when I try this:
#main input, #main label {
margin: 0 auto;
}

It does not center the lable and input field in the center of the container. Thanks for any response.

Excavator
05-07-2010, 03:17 AM
Hello johnmerlino,
I'm not much of a form guy but to center an block level element you need three things:

a DocType Declaration
an element with a width
that elements left/right margins set to auto


So, using the rules listed above, when you give .field a width you are then able to center it with margin: 0 auto;

Something like this (using your invalid code) -
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
html, body {
font: 100% "Comic Sans MS";
background: #FC6;
}
* {
margin: 0;
padding: 0;
}
#main {
width: 800px;
margin: 30px auto;
padding: 25px 0 300px;
background: #999;
overflow: auto;
font-size: 1em;
}
.field {
width: 200px;
margin: 0 auto;
}
</style>
</head>
<body>
<div id="main">
<form>
<input id="deployment_name" type="hidden" />
<input id="return_url" name="return_url" type="hidden" />
<div class='field'>
<label for="email">Email</label>
<input id="email" name="email" type="text" />
<!--end .field--></div>
</form>
<!--end main--></div>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum