Skip to content

[CSS] Docs: Clarify margin collapse rule for parent/child scenarios#40349

Merged
estelle merged 6 commits intomdn:mainfrom
kannanwisen:patch-10
Jul 15, 2025
Merged

[CSS] Docs: Clarify margin collapse rule for parent/child scenarios#40349
estelle merged 6 commits intomdn:mainfrom
kannanwisen:patch-10

Conversation

@kannanwisen
Copy link
Copy Markdown
Contributor

Description

This PR proposes a small wording change to the section on parent/child margin collapsing to be more precise and easier to understand.

Motivation

The current text states that a parent's margin collapses with "one or more of its descendant blocks". While technically true in some complex edge cases, this can be misleading for the most common parent-child scenario. It might imply that a parent's margin could collapse with a grandchild, which is not the case.

Changing the wording to specify the "first descendant block" for margin-top and the "last descendant block" for margin-bottom is more precise and provides a clearer mental model for developers trying to understand this tricky concept.

Additional details

Suggested Change

I suggest changing the wording from:

"...to separate the margin-top of a block from the margin-top of one or more of its descendant blocks..."

To the more precise:

"...to separate the margin-top of a block from the margin-top of its first in-flow descendant block..."

(And a similar change for the margin-bottom rule).

Related issues and pull requests

@kannanwisen kannanwisen requested a review from a team as a code owner July 14, 2025 07:02
@kannanwisen kannanwisen requested review from estelle and removed request for a team July 14, 2025 07:02
@github-actions github-actions bot added Content:CSS Cascading Style Sheets docs size/s [PR only] 6-50 LoC changed labels Jul 14, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jul 14, 2025

Preview URLs

(comment last updated: 2025-07-15 09:20:40)

- : Specifically, this occurs in two main cases:
- The {{cssxref("margin-top")}} of a parent collapses with the {{cssxref("margin-top")}} of its first in-flow descendant unless the parent has a border-top, padding-top, contains any inline content (like text), or has _[clearance](/en-US/docs/Web/CSS/clear) applied.
- The {{cssxref("margin-bottom")}} of a parent collapses with the {{cssxref("margin-bottom")}} of its last in-flow descendant unless the parent has a defined {{cssxref("height")}} or {{cssxref("min-height")}}, a {{cssxref("border-bottom")}}, or {{cssxref("padding-bottom")}}.
- : In both cases, creating a new [block formatting context](/en-US/docs/Web/CSS/CSS_display/Block_formatting_context) on the parent will also prevent its margins from collapsing with its children.
Copy link
Copy Markdown
Member

@Josh-Cena Josh-Cena Jul 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not how our DL syntax works. You can only have a single - : child. You have to write these as normal paragraphs.

kannanwisen and others added 3 commits July 14, 2025 13:36
…index.md

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…index.md

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Comment thread files/en-us/web/css/css_box_model/mastering_margin_collapsing/index.md Outdated
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Copy link
Copy Markdown
Member

@estelle estelle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 🎉

Comment thread files/en-us/web/css/css_box_model/mastering_margin_collapsing/index.md Outdated
@estelle estelle merged commit c9fc9aa into mdn:main Jul 15, 2025
8 checks passed
@kannanwisen
Copy link
Copy Markdown
Contributor Author

You're welcome! 🎉
Happy to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:CSS Cascading Style Sheets docs size/s [PR only] 6-50 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants