How can I know where is the center of the window?

Discussion in 'JavaScript' started by Betty_S, Jan 13, 2007.

  1. #1
    Hallo,
    I have a <div> with a table and I would like to be posed exactly in the center of the window. Different user have different screen configuration.
    How can I know where exactly the center of the window is, and pose the div there?

    Thanks.:confused:
     
    Betty_S, Jan 13, 2007 IP
  2. roaryyyj

    roaryyyj Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Well im not sure how you find the center point with javascrict, but this method works fine to centre a div
    Just assign this styles to the div container:
    
    .c1 {
         overflow:auto; 
         position:absolute; 
         left: 50%; 
         top: 50%;
         width: 200px;
         height: 200px;
         margin-top: -100px;
         margin-left: -100px;
        }
    
    HTML:
    The margin left needs to be half of the width, and the margin top needs to be half of the height, but then a negative sign added to it
     
    roaryyyj, Jan 14, 2007 IP
  3. rich_uk

    rich_uk Peon

    Messages:
    321
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3

    Hi Betty, after recently spent time on a centered popup, I'm sure you can safely borrow some parts of this code to make it work for what you want.

    
    <!--Advertise PopUp -->
    function preview2(URL) {
      var previewWindow;
      var width = screen.width;
      var height = screen.height;
      var boxwidth = '368';
      var boxheight = '250';
      var xpos = (width / 2) - (boxwidth / 2);
      var ypos = (height / 2) - (boxheight / 2);
      previewWindow = window.open(URL, 'preview', 'width='+boxwidth+',height='+boxheight+',left='+xpos+',top='+ypos+',resizable=no,scrollbars=yes,toolbar=no,location=no,status=no,menubar=no,copyhistory=no');
    }
    
    Let me know if it's of ANY use.
    
    Regards,
    
    Rich.
    
    Code (markup):
     
    rich_uk, Jan 16, 2007 IP
  4. Betty_S

    Betty_S Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks folks! :)
     
    Betty_S, Jan 22, 2007 IP
  5. pbmods

    pbmods Guest

    Messages:
    18
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Here's how I got it to work on IE awhile ago:

    <body onLoad="document.body.style.paddingTop=(Math.max(parseInt((document.body.clientHeight - document.getElementById('docMargins').offsetHeight) / 2), 0) + 'px');document.body.style.visibility='visible';" style="visibility:hidden"><div id="docMargins" style="border:1px solid black">
    Code (markup):
     
    pbmods, Jan 28, 2007 IP