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 5 of 5
  1. #1
    Regular Coder tylerjca's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    161
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Vertically align a div in the page

    Hi, I'm trying to get a div..
    Code:
    body{
       height: 100%;
       text-align: center;
    }
     
    div.main{
       width: 700px;
       height: 400px;
    }
    ..to vertically center within the page.
    in other words, I need it dead center on the page!

    is this possible?

    Thanks in advance
    -Tyler

  • #2
    Regular Coder
    Join Date
    Oct 2006
    Posts
    164
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Try to add:

    Code:
    	margin: 0px auto 0px auto;
    to the css of the div

  • #3
    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 tylerjca,
    Sman's code will center your site horizontally (it can be shortened to margin: 0 auto; usually too).

    To center if vertically is a little different.
    Code:
    #layerwhatever {
     position: relative;
     top: 50%;
     margin-top: -???px;    /* replace the ??? with half the height of the div, ex: 200px high would be 100px */
     margin: 0 auto;
     }
    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

  • #4
    New Coder
    Join Date
    Mar 2006
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this will work as well. pretty much the same thing but I had to post because I wrote a big long one and it was taking forever to upload so I lost it. Meantime somebody else answered your question

    div.main
    {
    position:absolute;
    width: 700px;
    height: 400px;
    margin-top: -200px;
    top: 50%;
    }

  • #5
    Senior Coder
    Join Date
    Nov 2003
    Location
    Minneapolis, MN
    Posts
    2,879
    Thanks
    2
    Thanked 65 Times in 56 Posts
    Try this method; it's the best I've seen so far:

    http://exanimo.com/css/vertical-cent...a-floated-shim


  •  

    Posting Permissions

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