Which of the following will NOT prevent margin collapsing between a parent and its first child?
Adding a border to the parent.
Setting padding-top: 1px; on the parent.
Using display: inline-block; on the child.
Incorrect.
Think about which properties create a separation between the parent and child margins.
Setting margin-top: 0; on the child.
Why my answer is false? (The third one)
Your browser information:
User Agent is: Mozilla/5.0 (iPad; CPU OS 18_5_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/138.0.7204.119 Mobile/15E148 Safari/604.1
Challenge Information:
Working with CSS Transforms, Overflow, and Filters - What Is Margin Collapsing, and How Does It Work?
because that one prevents the margin from collapsing
from the transcript
Margins can also collapse between a parent element and its first or last child. If there’s no border, padding, inline content, or clearance to separate the parent’s margin from the child’s, they will collapse.
I’m a bit confused. My understanding is that setting display: inline-block on the child doesn’t introduce any physical separation like a border, padding, or content would. So why would it prevent margin collapsing?
Also, the correct answer is “Setting margin-top: 0; on the child.” But in this case, there’s no margin to collapse — which means there’s nothing to prevent. It simply removes the margin entirely.
Am I missing something in how inline-block changes margin behavior
You can read more about margin collapsing behaviour here
The important bit to note:
If there is no border, padding, inline part, block formatting context created, or clearance to separate the margin-top of a block from the margin-top of one or more of its descendant blocks; or no border, padding, inline content, height, or min-height to separate the margin-bottom of a block from the margin-bottom of one or more of its descendant blocks, then those margins collapse. The collapsed margin ends up outside the parent.
And from reading about “block formatting context” you can find out that inline-blocks (elements with display: inline-block) create a context which then stops the margin collapse.
In this example the empty-blocks top and bottom margins collapse into a single 30 pixels margin, the larger of the two.
If margin collapsing happens between top and bottom of .empty-block as stated in the last paragraph, shouldn’t it be 20 px (collapsed into larger of 20px and 10px) ?
(The problem could come from my english, not native, but alongside I asked to three ai who all agreed with my thought.)
Thank you for helping make FCC better. Bugs can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.