How does CSS clearing really work?

前端 未结 5 1249
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 03:12

I have a

that I want to be on a line by itself. According to W3Schools, this rule:

div.foo {
  clear: both;
}

...s

5条回答
  •  隐瞒了意图╮
    2020-12-30 03:34

    Yes, clearing only moves the element to which it is applied. However, the result feels different depending on whether the element is "in flow" or out of flow. I guess that is confusing you.

    If the element is in flow, clearing moves it down together with everything that follows. Think of it as moving current pen position (the place where next element should be placed) down. This behaviour is easy to undestand.

    If the element is out of flow, as the float in your example, only the element is moved, the pen position stays at the same place, unless other rules make them move. For example, a float is not allowed to start above a previous float.

    There is also a messy issue of margin collapsing. Clearing interrupts them, sometimes in an unintuitive way.

提交回复
热议问题