Css horizontal center, vertical center, css vertical center

Source: Internet
Author: User

Css horizontal center, vertical center, css vertical center

Key Point 1: Set [position: absolute;] For containers ;]

Key Aspect 2: Container setting [top: 50%; left: 50% ;]

Key 3: You need to know the actual width and heigh of the container (which can be preset or dynamically obtained at runtime)

Key 4: horizontal center: margin-left =-1 * (width/2). For example, if width is 280px, set [margin-left: 140px ;]

Key 5: vertical center: margin-top =-1 * (height/2). For example, if the height is 110px, set [margin-top: 55px ;]

 

Example:

Html

<Html> 

Css

    #alert {        position: absolute;        width: 280px;        top: 50%;        left: 50%;        margin-top: -55px;        margin-left: -140px;        border-radius: 5px;        background: #fff;        z-index: 1000000;        padding: 20px;    }

Running Effect

Actual running size

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.