CSS Absolute absolute positioning centered Example

Source: Internet
Author: User

We often use ' margin:0 auto ' to achieve horizontal center, and always think ' margin:auto ' can not achieve vertical center ... In fact, achieving vertical centering requires only declaring element heights and the following CSS:

The code is as follows Copy Code


. Absolute-center {
Margin:auto;
Position:absolute;
top:0; left:0; bottom:0; right:0;
}

Method Two

Position:absolute;
top:50%;
left:50%; You can only position the div in a red circle as the starting point, plus margin:-100px 0px 0px-100, you will be able to move it to the red dot as the center of the position.

Sample code:

div layer absolutely centered "vertical and horizontal"

The code is as follows Copy Code

#content {
Position:absolute;
width:650px;
height:298px;
left:50%;
top:50%;
margin-left:-325px; /* Set to half of width * *
margin-top:-149px; /* Set to half of the height * *
}

Analysis: Position:absolute; This is a must.

margin-left:-325px; /* Set to half of width * *
margin-top:-149px; /* Set to half of the height * *

This enables the center to be positioned

The effect is as follows


Related Article

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.