Connecting Tech Pros Worldwide Help | Site Map

Position Absolute Center of Page

  #1  
Old July 17th, 2007, 09:47 PM
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,077
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4.  
  5. <head>
  6.     <meta http-equiv="Content-Language" content="en-us" />
  7.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8.     <title>DivCenter Experiment</title>
  9. </head>
  10.  
  11. <style>
  12. body, html
  13. {
  14.  width: 100%;
  15.  height: 100%;
  16.  margin: 0px; 
  17.  padding: 0px;
  18.  background-color: #FFFFFF;
  19. }
  20.  
  21. .favBgStyle
  22. {
  23.  border: 0px;
  24.  padding: 0px;
  25.  margin: 0px;
  26.  width: 100%;
  27.  position: absolute;
  28.  background-color: #000000;
  29.  opacity: 0.5;
  30.  filter: Alpha(opacity:50);
  31.  z-index: 0;
  32. }
  33.  
  34. .favAreaBg
  35. {
  36.  border: 0px;
  37.  padding: 0px;
  38.  position: absolute;
  39.  background-color: #FFFFFF;
  40.  z-index: 1;
  41. }
  42.  
  43. .occupySpace
  44. {
  45.  height: 1500px;
  46. }
  47. </style>
  48.  
  49. <script>
  50. function OffWindowH()
  51. {
  52.  var OffWindowH=0;
  53.  
  54.  window.scrollTo(0,10000000);
  55.  
  56.  if(typeof self.pageYOffset!='undefined')
  57.  OffWindowH=self.pageYOffset;
  58.  else if(document.compatMode && document.compatMode != 'BackCompat')
  59. OffWindowH=document.documentElement.scrollTop;
  60.  else if(document.body && typeof(document.body.scrollTop)!='undefined')
  61. OffWindowH=document.body.scrollTop;
  62.  window.scrollTo(0,0);
  63.  
  64.  return OffWindowH;
  65. }
  66.  
  67. function WindowHeight() 
  68. {
  69.   var WindowHeight = 0;
  70.   if( typeof( window.innerWidth ) == 'number' ) 
  71.   WindowHeight = window.innerHeight;
  72.   else if (document.documentElement &&  document.documentElement.clientHeight) 
  73.   WindowHeight = document.documentElement.clientHeight;
  74.   else if(document.body && document.body.clientHeight) 
  75.   WindowHeight = document.body.clientHeight;
  76.  
  77.   return WindowHeight;
  78. }
  79.  
  80. function pHeight()
  81. {
  82.   var pHeight = OffWindowH() + WindowHeight();
  83.   return pHeight;
  84. }
  85.  
  86. function favBg()
  87. {
  88.   var favBg = document.getElementById('favBg');
  89.   favBg.style.height = pHeight() + 'px';
  90. }
  91. var cdiv = "blank";
  92. function favArea() {
  93.   if (cdiv == "blank") { cdiv = window.setInterval("favArea()", 50); }
  94.  var Element = document.getElementById("favArea");
  95.  objh = parseFloat(Element.style.height)/2;
  96.  objw = parseFloat(Element.style.width)/2;
  97.  Element.style.top = Math.floor(Math.round((document.documentElement.offsetHeight/2)+document.documentElement.scrollTop)-objh)+'px';
  98.  Element.style.left = Math.floor(Math.round((document.documentElement.offsetWidth/2)+document.documentElement.scrollLeft)-objw)+'px';
  99. }
  100. </script>
  101.  
  102. <body onload="favBg(); favArea();">
  103. <div id="favArea" class="favAreaBg"  style="width: 500px; height: 400px;"><table width="100%" height="100%"><tr><td valign="middle" align="center">This is dead center</td></tr></table></div>
  104. <div id="favBg" class="favBgStyle"></div>
  105. <div class="occupySpace"></div>
  106. </body>
  107. </html>
  108.  

This is another example written by me that will put a div dead center of the window.



  #2  
Old February 20th, 2009, 05:07 PM
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North :)
Posts: 4,940

re: Position Absolute Center of Page


Where are you using the OffWindowH() and the WindowHeight() methods?
You've declared them but I don't actually see them being used.
  #3  
Old February 21st, 2009, 12:17 PM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,487

re: Position Absolute Center of Page


they are used in pHeight(), line #82.
  #4  
Old February 22nd, 2009, 05:07 PM
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North :)
Posts: 4,940

re: Position Absolute Center of Page


Oh, I see, thanks :)
  #5  
Old April 10th, 2009, 09:28 PM
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609

re: Position Absolute Center of Page


But you can use the CSS alternative too.
Expand|Select|Wrap|Line Numbers
  1. body {
  2.     margin: 0;
  3. }
  4. #center_div {
  5.     position: fixed;
  6.     height: Hpx;
  7.     width: Wpx;
  8.     top: 50%;
  9.     left: 50%;
  10.     margin: -H/2px -W/2px;
  11. }
  #6  
Old May 7th, 2009, 05:47 PM
Newbie
 
Join Date: May 2009
Posts: 1

re: Position Absolute Center of Page


Hi hsriat,
Thanks for the CSS, it worked perfectly for what I needed.
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Footer on bottom of page heban answers 2 October 14th, 2007 05:36 PM
How to show the UpdateProgress contents at the center of monitor by using only CSS Max2006 answers 3 August 27th, 2007 04:05 AM
<center> and Style Position:Absolute with changing window Size? jobs answers 1 August 16th, 2007 04:35 AM
part of page should not scroll with rest Peter Crom answers 1 July 20th, 2005 03:25 PM