Graphic demonstration of deep analysis of CSS margin boundary superposition

Source: Internet
Author: User

Border superposition is a simple concept. However, it may cause a lot of confusion when layout webpages in practice. Simply put, when two vertical boundaries meet, they form a boundary. The height of this boundary is equal to the greater of the two overlapping boundary heights.

When an element appears on another element, the bottom boundary of the first element overwrites the top boundary of the second element, as shown in the figure below:


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

When an element is contained in another element (if it is not filled or the border is separated), their top and/or bottom boundaries are also superimposed, as shown in the following figure:


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

Although it seems a bit strange at the beginning, the boundary can even overlap with itself. Suppose there is an empty element that has a boundary, but there is no border or fill. In this case, the top boundary and the bottom boundary come together, and they will overlap, as shown in the figure below:


Overlay the top boundary and bottom boundary of an element.

If this boundary hits the boundary of another element, it will overlay it, as shown in the following figure:


The overlapping boundary of an empty element overwrites the boundary of another empty element.

This is why a series of empty section elements occupy a very small space, because all their boundaries are superimposed to form a small boundary.

Boundary superposition may seem a bit strange at the beginning, but it actually makes sense. Take a typical text page composed of several paragraphs as an example (see Figure 2-8 ). The space above the first paragraph is equal to the top boundary of the paragraph. If no boundary is superimposed, the boundary between all subsequent paragraphs is the sum of the adjacent top boundary and bottom boundary. This means that the space between paragraphs is twice that at the top of the page. If a boundary overlay occurs, the top boundary and bottom boundary of a paragraph are superimposed, so that the distance between each section is the same.


The boundary overlays elements to maintain a consistent distance.

Only the vertical boundary of the block box in the normal document flow can overlap the boundary. The boundary between the line box, floating box, or absolute positioning box is not superimposed.

  Border superposition
Side-by-side superposition is a misunderstanding that will cause a lot of troublesome CSS features. See the simple example of nested section in the div element:Copy codeThe Code is as follows: <div id = "box">
<P> This paragraph has a 20px margin. </p>
</Div>

The div box sets a 10-pixel border and a section sets a 20-pixel border:

Copy codeThe Code is as follows: # box {
Margin: 10px;
Background-color: # d5d5d5;
}
P {
Margin: 20px;
Background-color: # 6699ff;
}

Naturally, you will think that the generated style will have a 20-pixel distance between the paragraph and the div, as shown in Figure 1-1. The div is surrounded by a 10-pixel boundary.


Figure 1-1

However, the generated style is actually like Figure 1-2.
Figure 1-2Xmlns = "http://www.w3.org/1999/xhtml">


This paragraph has a 20px margin.

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.