CSS margin boundary overlay Depth Analysis Graphic Demo _ Experience Exchange

Source: Internet
Author: User
Boundary superposition is a fairly simple concept. However, when you lay out a Web page in practice, it can cause a lot of confusion. Simply put, when two vertical boundaries meet, they form a boundary. The height of this boundary is equal to the larger of the height of two overlapping boundaries.

When an element appears above another element, the bottom boundary of the first element is superimposed on the top edge of the second element, as shown in figure:


The top boundary of an element is superimposed on the bottom boundary of the preceding element


When an element is contained in another element (assuming that no padding or border separates the boundary), their top and/or bottom boundaries are superimposed, as shown in the figure:


The top boundary of an element is superimposed on the top boundary of the parent element


Although it may seem strange at first, the border can even overlap with itself. Suppose you have an empty element that has boundaries, but no borders or padding. In this case, the top and bottom boundaries come together and they overlap, see figure:


The top boundary of an element is superimposed on the bottom boundary


If this boundary touches the boundary of another element, it will also overlap, see figure:


Superposition of boundaries between an empty element and another empty element


This is why a series of empty paragraph elements occupy a very small amount of space because all of their boundaries are stacked together to form a small boundary.

The boundary superposition may seem a bit strange at first, but it's actually meaningful. Take an example of a typical text page consisting of several paragraphs (see Figure 2-8). The space above the first paragraph equals the top edge of the paragraph. If there is no boundary overlay, the boundary between all subsequent paragraphs will be the sum of the adjacent top and bottom boundaries. This means that the space between paragraphs is twice times the top of the page. If a boundary overlay occurs, the top and bottom boundaries between the paragraphs are stacked together so that the distances are consistent.


Boundary overlay maintains a consistent distance between elements


Only the vertical boundary of a block box in a normal document stream occurs with a boundary overlay. The bounds between the inline, floating, or absolute positioning boxes do not overlap.

   the problem of boundary superposition
Side-Office stacking is a CSS feature that can cause a lot of trouble if misunderstood. Refer to a simple example of a nested paragraph in a DIV element:
Copy Code code as follows:

<div id= "box" >
<p>this paragraph has a 20px margin.</p>
</div>

The Div box has a 10 pixel boundary and the paragraph has a 20 pixel boundary:

Copy Code code as follows:

#box {
margin:10px;
Background-color: #d5d5d5;
}
p{
margin:20px;
Background-color: #6699ff;
}

You would naturally think that the resulting style would be like Figure 1-1, with a 20-pixel distance between the paragraph and the Div, surrounded by a 10-pixel boundary outside the div.


Figure 1-1

However, the resulting style is actually like Figure 1-2.

Figure 1-2
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; Charset=utf-8 "> <title>css margin Boundary Overlay depth Analysis </title> <meta name=" author "content=" Stuart Colville " > <style type= "text/css" > #box {margin:10px; Background-color: #d5d5d5; } p{margin:20px; Background-color: #6699ff; </style> </pead> <body> <div id= "box" > <p>this paragraph has a 20px MARGIN.</P&G T </div> </body> </ptml>
[ctrl+a All selected note: If you need to introduce external JS need to refresh to perform]

Here two conditions took place. First, the 20 pixel upper boundary and the upper boundary of the paragraph are superimposed with the 10 pixel boundary of the div to form a single 20 pixel vertical boundary. Second, these boundaries are not surrounded by div, but are highlighted to the top of the div and the outside of the bottom. This occurs because the element with a block-level child element computes its height.

If an element has no vertical border and padding, its height is the distance between the top of the child element that it contains and the edge of the bottom border. Therefore, the top and bottom margins of the contained child elements are highlighted outside the container element. However, there is a simple solution. By adding a vertical border or fill, the margin is no longer stacked, and the height of the element is the distance between the top of the child element it contains and the edge of the bottom margin.

To make the previous example look like Figure 1-1, simply add a filler or border around the div:
Copy Code code as follows:

#box {
margin:10px;
padding:1px;/* or border:1px solid color;*/
Background-color: #d5d5d5;
}
p{
margin:20px;
Background-color: #6699ff;
}

Most problems with side l boundaries can be repaired by adding a transparent border or 1px of filler.

Supplementary solutions:
Copy Code code as follows:


1. Outer padding
2. Transparent border border:1px solid transparent;
3. Absolute positioning Postion:absolute:
4. Outer div Overflow:hidden;
5. Inner Div plus float:left; Display:inline;
6. The outer div is sometimes used to zoom:1;
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.