Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    189
    Thanks
    38
    Thanked 3 Times in 3 Posts

    margin auto to align center

    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.

  • #2
    Master Coder Excavator's Avatar
    Join Date
    Dec 2006
    Location
    Alaska
    Posts
    9,675
    Thanks
    22
    Thanked 1,827 Times in 1,811 Posts
    Hello johnmerlino,
    I'm not much of a form guy but to center an block level element you need three things:
    1. a DocType Declaration
    2. an element with a width
    3. 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) -
    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>
    Validate often DURING development - Use it like a splelchecker | Debug during Development |Write it for FireFox, ignore IE
    Use the right DocType | Validate your markup | Validate your CSS | Why validating is good | Why tables are bad

  • Users who have thanked Excavator for this post:

    johnmerlino (05-08-2010)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •