Results 1 to 8 of 8

Thread: Positioning a div

  1. #1
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default Positioning a div

    Hi,

    How can I put a div always on the center of the screen? (vertically and horizontally)


    Thanks!

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Are you looking for how to center the div, or how to keep it static (not moving) while the rest of the page moves around it (scrolling, etc.), or perhaps both?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default

    I'm looking for how to center the div

    Thanks

  4. #4
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Example:
    Code:
    <div style="position:absolute; left:50%; top:50%; width: 300px;  height:400px; margin-left:-150px; margin-top: -200px; border: 1px solid black"></div>
    margin-left is the negative counterpart of width/2; margin-top is the negative counterpart of height/2.
    ===
    Arie Molendijk.

  5. The Following User Says Thank You to molendijk For This Useful Post:

    d-machine (03-26-2010)

  6. #5
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default

    Hi, thank you molendijk,
    but it doesn't appear on the middle (horizontally yes, but vertically no) of the screen.

    My page is long, so you can scroll there, maybe thats the reason.

    I want that the div will appear at the center of the screen, wherever I am in the page, without any need for further scrolling.

  7. #6
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Code:
    <!--[if IE]>
    <style ="text/css">
    .center{position:absolute;	
    top: expression(offsetParent.scrollTop +  ((offsetParent.clientHeight-this.clientHeight)/2) +'px'  );
    left: expression(offsetParent.scrollLeft +  ((offsetParent.clientWidth-this.clientWidth)/2) + 'px'); width:200px; height:200px}
    body{background: url(foo) fixed}
    </style>
    <![endif]-->
    
    <!--[if !IE]><!-->
    <style type="text/css">
    .center{position: fixed; top:50%; left: 50%; width:200px; height:200px; margin-left:-100px; margin-top: -100px}
    </style>
    <!--<![endif]-->
    <div class="center" style="border:1px solid black">centered</div>
    ===
    Arie

  8. The Following User Says Thank You to molendijk For This Useful Post:

    d-machine (03-30-2010)

  9. #7
    Join Date
    Sep 2008
    Location
    Seattle, WA
    Posts
    135
    Thanks
    1
    Thanked 11 Times in 11 Posts

    Default

    1) make sure the div you want displayed in the center has a set width
    2) use margin: auto;

  10. The Following User Says Thank You to simcomedia For This Useful Post:

    d-machine (03-30-2010)

  11. #8
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    This is better than what I proposed above, because it does not use IE regular expressions:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    </head>
    <body>
    <div style="position:fixed; border: 1px solid black; height:80%; top:10%; left:20%;width:60% ">Centered and fixed</div>
    </body>
    </html>
    Works in non-IE and in IE7 and above. For IE, you have to use a valid doctype.
    ===
    Arie.

  12. The Following User Says Thank You to molendijk For This Useful Post:

    d-machine (03-30-2010)

Bookmarks

Posting Permissions

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