To ensure that elements do not overlap with floats, a clear property is used.
Let's delve into the world of floats and clearances. Floats have the ability to extend beyond their parent containers:
<div style='border:1px solid green;'>
<div style='float:left;background:red;height:100px;width:40px;'></div>
The red box overflows!
</div>
By adding a clearing div, this overflow issue can be resolved. A clear essentially prevents any content from flowing above it:
<div style='border:1px solid green;'>
<div style='float:left;background:red;height:100px;width:40px;'></div>
<div style='clear:both;'></div>
<!-- Content below will not overlap with the floats -->
</div>
However, margin collapsing introduces some complications, as subsequent elements may collapse through preceding ones, extending all the way up to the top margin. Let's explore self-collapsing hacks related to margin collapsing.
Exploring Self-Collapsing Hacks
In general, margin collapsing occurs when any bottom margin touches any adjacent top margin.
This includes an element's own margins as well. This phenomenon is known as self-collapsing, and the process repeats itself. Consider this example showcasing both scenarios:
<div style='margin-top:30px; margin-bottom:30px;'></div>
<div style='margin-top:30px; border:1px solid black;'>
The spacing above me is only 30px, not 90!
</div>
The first div undergoes self-collapsing, resulting in a final space of 30px. The subsequent div then collapses into it, maintaining the total gap at just 30px.
Now, let's take advantage of this concept by creating a self-collapsing clearing div:
<div style='border:1px solid green;'>
<div style='float:left;background:red;height:100px;width:40px;'></div>
<div style='clear:left;margin-top:90px;margin-bottom:90px;'></div>
Despite the clearance, no visible 90px gap remains!
</div>
Although the gap disappears, a 90px margin still extends upwards over the floats.
If there were no text following it and the parent had a bottom margin, according to our rules on margin collapsing, this margin should collapse upwards. Sibling elements could potentially collapse 'through' it, reaching the very top. To prevent unwanted overlaps, we need to block this behavior.
The specification addresses this scenario crisply. Here's a breakdown of its language:
If the top and bottom margins of an element with clearance are adjoining
This refers to a self-collapsing element that has cleared a float.
its margins collapse with the adjoining margins of following siblings
While it's acceptable for other margins to collapse into it, there's one catch...
That resulting margin does not collapse with the bottom margin of the parent block
...as the very bottom margin must not ascend, lest it leads to undesirable overlapping situations.
An illustrative example demonstrates the application of this rule:
<div style='border:1px solid green;'>
<div style='margin-bottom:50px;'>
<div style='float:left;background:red;height:100px;width:40px;'></div>
<div style='clear:left;margin-top:50px;margin-bottom:50px;'></div>
<!-- The parent's bottom margin aligns with our collapsed margin,
yet upward collapsing is prevented, leaving a distinct gap -->
</div>
</div>
Introducing text within this clearing div abolishes self-collapsing, facilitating safe bottom margin collapsing with the parent container instead.