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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with centering div using css

    I have a website I am working on and am attempting to center horizontally and vertically a hidden div element so that it is always centered. any ideas?

    <style type="text/css">
    #layer1 {
    position: fixed;
    visibility: hidden;
    width: auto;
    background-color: #ffffff;
    border: 1px solid #000;
    padding: 10px;
    margin-left: 150px;
    margin-right: 150px;


    }


    #close {
    float: right;
    }
    </style>


    <div id="layer1">
    CONTENT
    </div>

  • #2
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts
    To horizontally center the DIV element you need to give the DIV a defined width, and then you can add margin: 0 auto; which basically means it automatically calculates the left and right margin to center the element.

    Vertically centering the element is a bit tricker, because of Internet Explorer, but I suggest you take a read of this for a bit more information on how it can be done.

  • #3
    Senior Coder effpeetee's Avatar
    Join Date
    Feb 2007
    Location
    Clapham Junction - London SW
    Posts
    4,885
    Thanks
    228
    Thanked 204 Times in 203 Posts
    There is some help here. Use the search facility.

    More to the point

    Specifically here.


    Frank
    Last edited by effpeetee; 05-24-2010 at 09:29 PM.
    * Sources (updated: 21.11.2012.
    Using Windows 8 Professional. 64bit with HP Photosmart 5510 printer Very useful site here.

  • #4
    Master Coder Excavator's Avatar
    Join Date
    Dec 2006
    Location
    Alaska
    Posts
    9,739
    Thanks
    22
    Thanked 1,836 Times in 1,820 Posts
    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


  •  

    Posting Permissions

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