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 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2011
    Location
    England
    Posts
    62
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Div Postitioning

    Hi

    I am currently building a website as part of my FinalMajor project for college.I have laid out all the divs where i want them all to be however i would like to position them all in the center of the browser instead of the left where they are now.

    Is there a simple piece of code i can use to do this?

    I am using a an external CSS sheets as well if that helps.

    Any help is greatly appreciated

    Adam
    Last edited by designer21; 04-06-2012 at 09:13 PM.

  • #2
    Master Coder Excavator's Avatar
    Join Date
    Dec 2006
    Location
    Alaska
    Posts
    9,673
    Thanks
    22
    Thanked 1,827 Times in 1,811 Posts
    Hello designer21,
    To center an element you need three things:
    1. a valid DocType
    2. an element with a width
    3. that elements right/left margins set to auto



    Typically, for an entire site, you would center a containing element and place your divs inside that.
    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

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    6
    Thanked 1 Time in 1 Post
    What Excavator said is exactly right.

    Just follow steps one and two and for step three just type
    margin-left: auto;
    margin-right: auto; in your css

  • Users who have thanked Myskillzownu for this post:

    designer21 (04-06-2012)

  • #4
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by designer21 View Post

    Is there a simple piece of code i can use to do this?
    Code:
            
            <style type="text/css">
                #wrapper{
                    width: 900px;
                    margin: 10px auto 0px auto;
                    border : 1px solid blue;
                }
            </style>
    Code:
        
        <body>
            <div id="wrapper">
                Page contents go here
            </div>
        </body>

  • #5
    Master Coder Excavator's Avatar
    Join Date
    Dec 2006
    Location
    Alaska
    Posts
    9,673
    Thanks
    22
    Thanked 1,827 Times in 1,811 Posts
    I always like to supply valid code when providing examples to a newbie that may have no idea about DocTypes or how an .html document is laid out. We can always address linked/attached CSS later if needed.

    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 {
    	margin: 0;
    	background: #fc6;
    }
    #container {
    	height: 600px; /*for demo only*/
    	width: 800px;
    	margin: 30px auto;
    	background: #999;
    }
    </style>
    </head>
    <body>
        <div id="container">
    Your very interesting website would go here
        <!--end container--></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:

    designer21 (04-06-2012)

  • #6
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by Excavator View Post
    We can always address linked/attached CSS later if needed.
    probably not needed as the op said they are currently using an external stylesheet so the contents in style blocks can go in the external css.

  • #7
    New Coder
    Join Date
    Nov 2011
    Location
    England
    Posts
    62
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Thanks for your help guys, for some reason when i tried putting a wrapper div it didn't center it with thhe margins set to auto howeer when i did the divs seperately they did, thanks again


  •  

    Tags for this Thread

    Posting Permissions

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