Skip to content

Commit a7a435b

Browse files
committed
Stop margin collapsing through new formatting contexts
Floats, absolutely positioned boxes, table cells and boxes with overflow other than visible keep their children's margins inside. A parent with a fixed height doesn't pass its last child's bottom margin on either.
1 parent 2d515d1 commit a7a435b

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎Source/HtmlRenderer/Core/Dom/CssBox.cs‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,13 +1820,32 @@ private static bool BlocksParentChildTopJoin(CssBox parent, CssBox child)
18201820
// CSS 2.1 §8.3.1 margin collapsing is a block-formatting-context concept - an inline
18211821
// parent (e.g. a block-level child anonymously boxed inside a <span>) is not a block
18221822
// container to collapse "through" in the first place.
1823-
|| parent.Display == CssConstants.Inline || parent.Display == CssConstants.InlineBlock;
1823+
|| parent.Display == CssConstants.Inline || parent.Display == CssConstants.InlineBlock
1824+
|| EstablishesBlockFormattingContext(parent);
18241825
}
18251826

1826-
/// <summary>A box's own bottom border/padding blocks the bottom-margin join with its last child.</summary>
1827+
/// <summary>
1828+
/// A box's own bottom border/padding, a height other than auto, or a new block formatting context
1829+
/// blocks the bottom-margin join with its last child.
1830+
/// </summary>
18271831
private static bool BlocksParentChildBottomJoin(CssBox box)
18281832
{
1829-
return box.ActualBorderBottomWidth > 0.1 || box.ActualPaddingBottom > 0.1;
1833+
return box.ActualBorderBottomWidth > 0.1 || box.ActualPaddingBottom > 0.1
1834+
|| (!string.IsNullOrEmpty(box.Height) && box.Height != CssConstants.Auto)
1835+
|| EstablishesBlockFormattingContext(box);
1836+
}
1837+
1838+
/// <summary>
1839+
/// Floats, absolutely positioned boxes, table cells and captions, and boxes with overflow other than
1840+
/// visible start a new block formatting context, so their margins never collapse with their
1841+
/// children's (CSS 2.1 §8.3.1, §9.4.1).
1842+
/// </summary>
1843+
private static bool EstablishesBlockFormattingContext(CssBox box)
1844+
{
1845+
return box.IsFloated
1846+
|| box.Position == CssConstants.Absolute || box.Position == CssConstants.Fixed
1847+
|| box.Display == CssConstants.TableCell || box.Display == CssConstants.TableCaption
1848+
|| (!string.IsNullOrEmpty(box.Overflow) && box.Overflow != CssConstants.Visible);
18301849
}
18311850

18321851
/// <summary>

‎Source/Test/HtmlRenderer.IntegrationTest/BoxModel/CollapsedMarginBeforeTests.cs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,32 @@ public void AFloatsOwnMarginDoesNotMerge_ButThePredecessorsStillOccupiesSpace()
114114
// predecessor's trailing margin is real space it must sit after.
115115
Assert.AreEqual(15, f.Location.Y - a.ActualBottom, Delta);
116116
}
117+
118+
[TestMethod]
119+
[DataRow("overflow:hidden")]
120+
[DataRow("float:left;width:100px")]
121+
[DataRow("position:absolute;width:100px")]
122+
public void NewBlockFormattingContext_KeepsItsFirstChildsTopMarginInside(string parentStyle)
123+
{
124+
var (root, _) = LayoutHarness.Layout(LayoutHarness.Wrap(
125+
$"<div id='p' style='{parentStyle}'><div id='c' style='margin-top:20px;height:10px'></div></div>"));
126+
127+
var p = LayoutHarness.FindById(root, "p")!;
128+
var c = LayoutHarness.FindById(root, "c")!;
129+
130+
Assert.AreEqual(20, c.Location.Y - p.Location.Y, Delta);
131+
}
132+
133+
[TestMethod]
134+
public void FixedHeightParent_DoesNotPassItsLastChildsBottomMarginOn()
135+
{
136+
var (root, _) = LayoutHarness.Layout(LayoutHarness.Wrap(
137+
"<div id='p' style='height:50px'><div style='height:10px;margin-bottom:30px'></div></div>"
138+
+ "<div id='n' style='height:10px'></div>"));
139+
140+
var p = LayoutHarness.FindById(root, "p")!;
141+
var n = LayoutHarness.FindById(root, "n")!;
142+
143+
Assert.AreEqual(p.ActualBottom, n.Location.Y, Delta);
144+
}
117145
}

0 commit comments

Comments
 (0)