Guest User

StackOverflow: Vertically centered element with minimum marg

a guest
Aug 24th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.04 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <meta charset="utf-8" />
  5.         <title>Demo</title>
  6.        
  7.         <style type="text/css">
  8.             *{ margin: 0; padding: 0; }
  9.            
  10.             div#head
  11.             {
  12.                 height: 200px;
  13.                 background: blue;
  14.             }
  15.            
  16.             div#foot
  17.             {
  18.                 height: 100px;
  19.                 background: red;
  20.             }
  21.            
  22.             div#slideshow
  23.             {
  24.                 height: 300px;
  25.                 background: green;
  26.             }
  27.         </style>
  28.        
  29.         <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
  30.         <script type="text/javascript">
  31.            
  32.             $(window).resize(fix);
  33.             $(document).ready(fix);
  34.            
  35.             function fix()
  36.             {
  37.                 var base = $("div#slideshow").position().top;
  38.                 var middle = $(window).height() / 2;
  39.                 var hw = $("div#slideshow").height() / 2;
  40.                
  41.                 var destination = Math.max(middle - base - hw, 0);
  42.                 $("div#shell").offset({ top: destination });
  43.             }
  44.            
  45.         </script>
  46.     </head>
  47.    
  48.     <body>
  49.         <div id="shell">
  50.             <div id="head">Header.</div>
  51.             <div id="slideshow">Slideshow.</div>
  52.             <div id="foot">Footer.</div>
  53.         </div>
  54.     </body>
  55. </html>
Advertisement
Add Comment
Please, Sign In to add comment