Home  >  Article  >  Web Front-end  >  Detailed introduction to CSS margin knowledge points

Detailed introduction to CSS margin knowledge points

高洛峰
高洛峰Original
2017-03-07 15:11:511581browse

1. The percentage value of margin. The percentage maigin of ordinary elements is calculated relative to the width (width) of the container element.

Here we set a container with a width and height of 800 * 600 outside the image. Set img{ margin: 10%; }

The result is as follows

 Detailed introduction to CSS margin knowledge points
The result margin value is 800 * 10% = 80px; So here are the width calculations of the container relative to the width of the container. The width calculation of the container . Say it three times

2. The percentage maigin value of absolute positioning

Detailed introduction to CSS margin knowledge pointsDetailed introduction to CSS margin knowledge points

relative toThe width value of the first positioned ancestor element is calculated. That is, the width of parent = 1000px. So margin = 100px;

3. You can use margin to achieve 2:1 adaptation

For example, Two containers

Detailed introduction to CSS margin knowledge points Detailed introduction to CSS margin knowledge points

The height of the box here is not specified. Due to setting margin 50%. Its height is half of the parent container, so the aspect ratio is 1:2;

Detailed introduction to CSS margin knowledge points

4. Why do the margins overlap?

A) Two overlapping properties of margin

will only occur on the horizontal elements of the block. (Excluding float and absolute elements)

Does not consider wirte-mode (that is, writing format) only occurs in the vertical direction (margin-top margin-bottom)

B) occurs Case

1> Adjacent sibling elements

2> The first and last child element of the parent

3> Empty block.

Example 1 Adjacent sibling elements

Detailed introduction to CSS margin knowledge points Detailed introduction to CSS margin knowledge points

Here are two sibling elements.

Detailed introduction to CSS margin knowledge points There is only one em between the two p here, not two em. Because the first margin-bottom and the second margin-top overlap.

Example the second parent element overlaps the last child element

Detailed introduction to CSS margin knowledge points Detailed introduction to CSS margin knowledge points

According to conventional theory, there will be 80px left between son and the father of the parent element margin-top value. But it doesn't. The background element of son has not changed and no 80px is left;

Here is just 80px set for the parent element;

Detailed introduction to CSS margin knowledge points

Conditions for parent and child margin overlap

Detailed introduction to CSS margin knowledge pointsDetailed introduction to CSS margin knowledge points

So how to get rid of margin-top overlap?

As long as it doesn't meet those conditions.

Add overflow: hidden; border-top padding-top (add a space between them) to the parent element;

Example 3 The margins of empty block elements overlap.

Detailed introduction to CSS margin knowledge points Detailed introduction to CSS margin knowledge points

Note There is an empty element with no content inside. Conditions for margin overlap of empty elements

Detailed introduction to CSS margin knowledge points

4 Calculation rules for margin overlap.

A) Take the larger positive value

B) Add the positive and negative values

C) Take the most negative value.

5. The meaning of margin overlap

A) If there is no margin overlap at the end of a continuous paragraph or list, it will appear 1:2. Uncoordinated

B) Nesting or placing p anywhere in the web will not affect the original layout

C) Any number of empty p elements left behind should not affect the original reading Typesetting

Practical application

When making lists, control the distance in each list

Detailed introduction to CSS margin knowledge points

.list{

margin-top : 15px;

margin-bottom: 15px;

}

More robust, even if the last one is removed, it will not affect the layout

For more detailed articles on CSS margin knowledge points, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn