css - margin 负值为什么要这样设计?
阿神
阿神 2017-04-17 11:16:32
0
1
895
  • margin top left 为负数时:
    是以内容盒为基准线,所以负值使整个元素向上,右移动。
  • margin right bottom 为负数时:
    是以border为基准线,所以负值使不会影响元素的位置,但会使元素的基准线发生变化。结果使这个元素相邻的元素的位置发生变化。

margin top left 好理解。
margin right bottom 为什么要这样设计呢?这样有什么好处?

阿神
阿神

闭关修行中......

reply all(1)
左手右手慢动作

Here I will use an article by Yi Fei to explain... Let’s talk about the margin attribute from the simplest to the deeper

You can introduce a reference line to help understand this problem. (There is no reference line in W3C.) The reference line here refers to the reference point where the margin moves. This reference point is stationary relative to the box. of. The value of margin is the displacement of the box relative to the reference line.

In the design of CSS margin, the reference lines of top, right, bottom, and left are not consistent in the same category, but are divided into two types of reference lines. The reference lines of top and left belong to the same category, and the reference lines of right and bottom belong to the same category. Belongs to another category.

Here, top is displaced vertically downward using the upper edge of the content of the containing block or the lower edge of the margin of the vertically connected element as a reference line;
left uses the left side of the content of the containing block or the right side of the margin of the horizontally connected element as the reference line to move horizontally to the right.
right uses the right side of the border of the element itself as the reference line to move horizontally to the right;
bottom uses the lower edge of the border of the element itself as the reference line to move vertically downward.

The diagram is as follows:

From the above we can see that top and left are referenced by other elements, while right and bottom are referenced by this element. The displacement direction above refers to the situation when the margin value is positive. If it is a negative value, the displacement direction is in the opposite direction.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template