Results 1 to 4 of 4

Thread: position an absolute div in middle of screen

  1. #1
    Join Date
    Dec 2005
    Posts
    133
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default position an absolute div in middle of screen

    I am opening a hover div and I want it to be always in the middle of my screen

    this is the css for that hover div:
    .div_watchedit{position:absolute;visibility:visible;width:735px;height:381px;
    margin: auto;z-index:10;background:#fff;}

    but the div is positioned left and top 0px. how do i make it in the middle of the screen for every screen resolution???

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Try to add highlighted:
    Code:
    .div_watchedit{
    	position:absolute;
    	visibility:visible;
    	width:735px;
    	height:381px;
    	margin: auto;
    	z-index:10;
    	background:#fff;
    	top:50%;
    	left:50%;
    	margin-top:-190px;
    	margin-left:-367px;
    }
    For further reading:
    http://www.wpdfd.com/editorial/thebox/deadcentre4.html

    Hope that helps.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. The Following User Says Thank You to rangana For This Useful Post:

    emanuelle (01-29-2009)

  4. #3
    Join Date
    Mar 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    margin: 0 auto;

    simply it will make your div on center of the screen

    example... Div on center of the screen

    kerry

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

    Default

    Jerrymarke's solution only works horizontally.
    Rangana's css works well except with small screen resolutions (smaller than 367x190), due to
    margin-top:-190px; margin-left:-367px;
    which will make some parts of the text in the div invisible with small windows.

    So here's another solution (works well if you don't mind about the actual size of the div; the size of the div adapts to the size of the window):
    <style type="text/css">
    .centerDiv {
    position: fixed;
    left: 20%; top: 20%; right: 20%; bottom: 20%; //you can adapt this to your requirements; values in pixels are also possible;
    border: 1px solid black;
    background:#fff;
    z-index: 10;
    }
    </style>

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
  •