Distal to combat development entry: CSS margins (margin) and a method for preventing overlapping

Two or more vertical stages cassette blocks adjacent boundaries are coincident. The result is a boundary width of the boundary in the width of the maximum values ​​of the neighboring. If the border appears negative, subtract the absolute value of the maximum negative border in the largest positive boundaries. If there is no positive boundary, zero subtracted from the maximum absolute value of the negative border. Note: adjacent box may not be the element of parent-child relationships or sibling generation.

But the overlapping boundaries there are exceptions:

1, horizontal margins never coincide.
2, in a specification document, two or more block-level box model overlap adjacent vertical margin. The final margin value is calculated as follows:
A, all positive, whichever is the greatest;
B, not all positive, then the absolute values are taken, and then subtracting the maximum value;
C, no value, then all absolute value, and then subtracting the maximum 0.
Note: adjacent box model may have no inheritance and dynamic neighbor or a DOM element.
3, adjacent box model, if one is floating (a float), do not overlap margin vertical, and between the floating box model and its children as well.
4, the margin is provided between the overflow property element and its subelements not overlap (except overflow value is visible).
5, provided with absolute positioning (position: absolute) of the box model, the vertical margin is not overlapped, and between them and the sub-elements is the same.
6, set display: inline-block element, the vertical margin do not overlap, or even sub-elements and between them is the same.
7, if the upper and lower margin adjacent a box model, then it may overlap the margin covers (collapse through) it. In this case, the position of the element (position) of its margin depending on whether the neighboring elements overlap.
a, if the margin of the element and its parent element overlap margin-top, the boundary defined border-top box model and its elements of the same parent.
B, further, the parent of any element not involved in the overlap margin, only the parent element or margin-bottom are involved in the calculation. If nonzero elements of border-top, border-top then the boundary position of the element, like the original.
Application of a clear operation of the elements of the margin-top margin-bottom and will not block its parent element level of overlap.
Note that, the position of those elements have been overlapped by no impact on other positions have overlapping elements; only when the positioning elements of these sub-elements, border-top boundary position is required.
8, the vertical margin root element will not be overlapped.

Margins (margin) overlapping exemplary
margins overlap refers to two vertically adjacent block-level element, when the upper and lower margins meet, will produce from overlapping margins, and outside the overlap distance, equal to the larger By.

One learns there will be confusion, lack of motivation. Here's what I recommend front-end learning exchange group: 731 771 211, which is the front end of the study, if you want to create web pages cool, want to learn programming. Himself compiled a front-end 2019 the most comprehensive study materials, from the most basic HTML + CSS + JS [cool special effects, games, plug-in package design patterns] to move to the end of the project HTML5 real learning materials are finishing, gave every front-end junior partner, had wanted to learn web front-end, or change jobs, or college students, as well as work want to upgrade their skills, are welcome to join the junior partner to learn to learn.

Click: join

Icon:

Another overlap when one element is contained in another element, will produce overlap between the parent element and the child elements, the overlap from the outside, which is equal to the largest of:
Distal to combat development entry: CSS margins (margin) and a method for preventing overlapping

Similarly, if an empty element without content, will also have its own top and bottom margins overlap.
Distal to combat development entry: CSS margins (margin) and a method for preventing overlapping

From the overlap of meaning outside

The overlapping margins generated only between the distance, this seems a bit strange rules up and down outside the normal stream of the document, in fact, has its practical significance. Contemplated, when we vertically arranged set of rules block-level element (P paragraph), since there is then the block elements between overlapping margins, it will not produce twice the distance between paragraphs.

Prevent overlapping margins Solution:

Although the margins of overlap there is a certain sense, but sometimes we do not want to overlap between the elements in the design, you can have a few suggestions for reference as follows:

  1. Padding instead of the outer element
  2. Transparent border element inner border: 1px solid transparent;
  3. Inner absolutely positioned elements postion: absolute:
  4. Outer element overflow: hidden;
  5. Add inner elements float: left; or display: inline-block;
  6. The inner element padding: 1px;

Guess you like

Origin blog.51cto.com/14284898/2403634