CSS Horizontal Center relative positioning and negative margin method

Source: Internet
Author: User
The code is as follows Copy Code

<title>div Horizontal Center </title>
<style>
Div#wrap {
position:relative;
width:760px;
left:50%;
Margin-left: -380px;
border:1px solid #333;
Background-color: #ccc;
}
</style>


<div id= "Wrap" >
<p>
It's an element in the P tag.
</p>
</div>


Analysis

position:relative; is relative positioning, which is relative to the current level of the parent
left:50%; Center meaning (move center element to right 50% relative to its original position)
Margin-left: -380px; The negative margin method (see that the element is already placed in the middle of the page but not centered.) Centered is the central point of the entire element that overlaps the center line of the page. So I'm going to change the element margin-left to half of its width, of course, in order to move to the left, the offset is a negative number.

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.