code_junkie

Practical solution to center vertically and horizontally in HTML that works in FF, IE6 and IE7

Nov 10th, 2011
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>Centering</title>
  6. <style type="text/css" media="screen">
  7. body, html {height: 100%; padding: 0px; margin: 0px;}
  8. #outer {width: 100%; height: 100%; overflow: visible; padding: 0px; margin: 0px;}
  9. #middle {vertical-align: middle}
  10. #centered {width: 280px; margin-left: auto; margin-right: auto; text-align:center;}
  11. </style>
  12. </head>
  13. <body>
  14. <table id="outer" cellpadding="0" cellspacing="0">
  15. <tr><td id="middle">
  16. <div id="centered" style="border: 1px solid green;">
  17. Centered content
  18. </div>
  19. </td></tr>
  20. </table>
  21. </body>
  22. </html>
  23.  
  24. #horizon
  25. {
  26. text-align: center;
  27. position: absolute;
  28. top: 50%;
  29. left: 0px;
  30. width: 100%;
  31. height: 1px;
  32. overflow: visible;
  33. display: block
  34. }
  35.  
  36. #content
  37. {
  38. width: 250px;
  39. height: 70px;
  40. margin-left: -125px;
  41. position: absolute;
  42. top: -35px;
  43. left: 50%;
  44. visibility: visible
  45. }
  46.  
  47. <div id="horizon">
  48. <div id="content">
  49. <p>This text is<br><emphasis>DEAD CENTRE</emphasis ><br>and stays there!</p>
  50. </div><!-- closes content-->
  51. </div><!-- closes horizon-->
  52.  
  53. <style>
  54. body
  55. {
  56. text-align:left;
  57. }
  58. .MainBlockElement
  59. {
  60. text-align:center;
  61. margin: 0 auto;
  62. }
  63. </style>
Add Comment
Please, Sign In to add comment