div Vertical Center CSS div box up and down vertical center

Source: Internet
Author: User

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

  1. <! DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <Meta charset="utf-8" />
  5. <title> Up and down Vertical Center online demo DIVCSS5</title>
  6. <style>
  7. #main {position:absolute;width:400px;height:200px;left:50%;top:50%;
  8. margin-left:-200px;margin-top:-100px;border:1px Solid #00F}
  9. /*CSS Note: For convenience, wrap CSS code */
  10. </style>
  11. <body>
  12. <div id="main">div horizontally centered and vertically centered </div>
  13. </body>
  14. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.