The div vertically centers the CSS div box up and down vertically, allowing the div box to be centered horizontally and vertically in the display browser of any resolution in any browser.
Div Vertical Center is often used in a single box, such as a page with only one login layout, use div css to make this login layout horizontal and CSS vertical center.
Here is a simple and best-compatible way to center horizontally and vertically.
1, the specific example code is as follows
- <! DOCTYPE HTML>
- <html>
- <head>
- <Meta charset="utf-8" />
- <title> Up and down Vertical Center online demo DIVCSS5</title>
- <style>
- #main {position:absolute;width:400px;height:200px;left:50%;top:50%;
- margin-left:-200px;margin-top:-100px;border:1px Solid #00F}
- /*CSS Note: For convenience, wrap CSS code */
- </style>
- <body>
- <div id="main">div horizontally centered and vertically centered </div>
- </body>
- </html>
Here a "#main" Object style is set, width 400px, height 200px, using absolute positioning position style while using absolute positioning left and top, and set Margin-top and Margin-left at the same time, in order to observe the effect, So a red border is added to this div box.
2. Example
Div+css implements a Div object to center vertically and horizontally at the same time
3, Horizontal vertical centering principle is described here using absolute positioning Position:absolute, using left and top to set the object distance between and 50%, but if set 50%, actually the box is not to achieve the center effect, So set margin-left:-200px;margin-top:-100px again, here's the trick: Margin-left's value is half the width, Margin-top's value is half the height of the object, and is set to negative This enables horizontal and vertical centering.
div Vertical Center CSS div box up and down vertical center