简体   繁体   English

为什么水平边距不会像垂直边距一样折叠?

[英]Why horizontal margin doesn't collapse as vertical margin?

I understand why margin collapsing happens;我明白为什么会发生边距崩溃 I have seen a few examples on various sites as to how it happens and why it happens.我在各种网站上看到了一些关于它是如何发生的以及为什么会发生的例子。

To be specific, the below is the link to the post I read:具体来说,以下是我阅读的帖子的链接:
https://css-tricks.com/almanac/properties/m/margin/ https://css-tricks.com/almanac/properties/m/margin/

It's clearly explained that the collapsing happens vertically and it was implemented like that for a purpose.很清楚地解释了折叠是垂直发生的,并且它是出于某种目的而实施的。 But, it's mentioned that但是,有人提到

This does not happen on horizontal margins (left and right), only vertical (top and bottom).这不会发生在水平边距(左侧和右侧)上,只会发生在垂直边距(顶部和底部)上。

No explanation has been provided as to why it doesn't happen on horizontal margins.没有解释为什么它不会发生在水平边距上。

Why doesn't horizontal margin collapsing occur?为什么不发生水平边距折叠?

Horizontal margins never get the chance to collapse as the rules that govern margin collapsing mean that they can never satisfy the conditions.水平边距永远没有机会崩溃,因为管理边距崩溃的规则意味着它们永远无法满足条件。

In CSS, the adjoining margins of two or more boxes (which might or might not be siblings) can combine to form a single margin.在 CSS 中,两个或多个盒子(可能是也可能不是兄弟)的相邻边距可以组合形成一个边距。 Margins that combine this way are said to collapse, and the resulting combined margin is called a collapsed margin.以这种方式组合的边距称为折叠边距,由此产生的组合边距称为折叠边距。

Collapsing margins ( http://www.w3.org/TR/CSS21/box.html#collapsing-margins )折叠边距 ( http://www.w3.org/TR/CSS21/box.html#collapsing-margins )

Adjoining boxes can only be block-level boxes:相邻框只能是块级框:

Two margins are adjoining if and only if:两个边距相邻当且仅当:

  • both belong to in-flow block-level boxes that participate in the same block formatting context两者都属于参与相同块格式化上下文的流入块级框

Collapsing margins ( http://www.w3.org/TR/CSS21/box.html#collapsing-margins )折叠边距 ( http://www.w3.org/TR/CSS21/box.html#collapsing-margins )

And margins only collapse if they are not floated or positioned absolutely:只有当它们没有绝对浮动或定位时,边距才会折叠:

  • Margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).浮动框和任何其他框之间的边距不会折叠(甚至在浮动框与其流入的子框之间也不折叠)。
  • Margins of elements that establish new block formatting contexts (such as floats and elements with 'overflow' other than 'visible') do not collapse with their in-flow children.建立新块格式上下文的元素的边距(例如浮动和具有“溢出”而不是“可见”的元素)不会与其流入的子项一起折叠。
  • Margins of absolutely positioned boxes do not collapse (not even with their in-flow children).绝对定位的盒子的边距不会塌陷(即使它们的流入子容器也不塌陷)。

Collapsing margins ( http://www.w3.org/TR/CSS21/box.html#collapsing-margins )折叠边距 ( http://www.w3.org/TR/CSS21/box.html#collapsing-margins )

This means that block-level boxes can never be positioned on the same line horizontally (as block-level boxes will automatically start on a new line by default) and satisfy the margin collapsing conditions at the same time.这意味着块级框永远不能水平放置在同一行上(因为块级框默认会自动从新行开始)并同时满足边距折叠条件。

In theory, inline boxes could satisfy the conditions but as they are not block-level the rules do not apply to them at all.理论上,内联框可以满足条件,但由于它们不是块级的,因此规则根本不适用于它们。

In an inline formatting context, boxes are laid out horizontally, one after the other, beginning at the top of a containing block.在行内格式化上下文中,框从包含块的顶部开始一个接一个地水平排列。 Horizontal margins, borders, and padding are respected between these boxes.这些框之间遵循水平边距、边框和填充。

Inline formatting contexts ( http://www.w3.org/TR/CSS21/visuren.html#block-formatting )内联格式上下文 ( http://www.w3.org/TR/CSS21/visuren.html#block-formatting )

That said, the simple reason why they don't collapse is that W3C said so:也就是说,它们不崩溃的简单原因是 W3C 是这样说的:

Horizontal margins never collapse.水平边距永远不会塌陷。

Collapsing margins ( http://www.w3.org/TR/CSS21/box.html#collapsing-margins )折叠边距 ( http://www.w3.org/TR/CSS21/box.html#collapsing-margins )

when you add any dom element, they are been added from left to right, one next to the other, which meen that the main layout is horizontally, this is why I believe collapsing happens only on vertical.当你添加任何 dom 元素时,它们是从左到右添加的,一个挨着另一个,这意味着主布局是水平的,这就是为什么我相信折叠只发生在垂直上。 this ensure a "new line" each time the element almost touch each other in the vertical sides, you can change the main element layout for example in angularjs from:这确保每次元素在垂直侧几乎相互接触时都会出现“新行”,您可以更改主要元素布局,例如在 angularjs 中:

<div layout="row"></div>

to

<div layout="column"></div>

which will change the layout to vertically and so each element that you add will be treated from top to bottom and so is the margin, this will make the elements go one on top of each other from bottom to top if you will set margin:-20px这会将布局更改为垂直,因此您添加的每个元素都将从上到下进行处理,边距也是如此,如果您设置margin:-20px ,这将使元素从下到上依次排列margin:-20px

It's the Visual formatting model 9.1.2 Containing blocks .它是Visual 格式化模型 9.1.2 Containing blocks Block boxes, when positioned statically, tend to occupy the horizontal space available inside their containing block.块框,当静态定位时,往往会占据其包含块内可用的水平空间。 If you set the width of the box, it will be added a right margin to compensate.如果你设置了框的宽度,它会被添加一个右边距来补偿。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM