Creating a horizontally centered and vertically centered div through CSS is a hassle, and you have to know the size of another div in advance:
| The code is as follows |
Copy Code |
. classname{ width:300px; height:200px; Position:absolute; left:50%; top:50%; margin:-100px 0 0-150px; } |
Horizontally centered and vertically centered through jquery has been mentioned before, the CSS method only applies to have a fixed size div, so to jquery to play a role
| The code is as follows |
Copy Code |
JQuery.fn.center = function () { This.css ("position", "absolute"); This.css ("Top", ($ (window). Height ()-this.height ())/2+$ (window). scrolltop () + "px"); This.css ("Left", ($ (window). Width ()-this.width ())/2+$ (window). ScrollLeft () + "px"); return this; } Use the above function as: $ (Element). Center (); |
There are other ways to implement
| The code is as follows |
Copy Code |
| <!--HTML--> <div id= "outer" > <div id= "Middle" > <div id= "inner" > Any text <br> <b>any Height </b><br> Any content, for example generated from DB <br> Everything is vertically centered <br> </div> </div> </div> <!--CSS--> html{height:100%;} body {height:100%;} #outer {height:600px overflow:visible;width:100%;p osition:relative}/* or without overflow * * #outer [id] {display:table; position:static;} #middle {position:absolute; top:50%}/* for explorer only*/ #middle [id] {display:table-cell; vertical-align:middle; width:100%;p osition:static;} #inner {position:relative top: -50%}/* for Explorer only */ /* Optional: #inner [id] {position:static} */ |
Examples compatible with Firefox IE6 above Chrome Opera
| The code is as follows |
Copy Code |
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/> <title> Absolute Vertical Centered example--http://www.111cn.net/</title> <script type= "Text/javascript" src= "Http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" ></ Script> <style type= "Text/css" > #wrap {height:200px;width:200px;background: #cccccc;p Osition:absolute;} </style> <script type= "Text/javascript" > $ (function () { function Windowalign () { var bodyheight = $ (document). Height (),//width of the entire page if this is window Ie6and IE7 gets a negative number Bodywidth = $ (window). Width (),//height of the entire page if this is document IE7 will not be centered horizontally Wrapwidth = $ ("#wrap"). Width () (),//////breadth of the content to be centered vertically Wrapheight = $ ("#wrap"). Height (); The height of the content to be centered vertically Postop = (bodyheight-wrapheight)/2; Get the top position Posleft = (bodywidth-wrapwidth)/2; Get the position on the left hand side $ ("#wrap"). CSS ({"Left":p osleft+ "px", "Top":p ostop+ "px"}); Set position } Windowalign (); $ (window). Resize (function () {//dynamic correction position when adjusting window Windowalign (); }); }) </script> <body> <div id= "Wrap" ></div> </body> |