Top Margin Stolen From Child Element And Added to Parent (Margin Collapsing)
One of the biggest frustrations that I have encountered with my time dealing with CSS is the Collapsing Margin Bug. It technically isn’t a bug, because for some reason, this was actually intentional, but here it goes:
The Issue:
For this example, imagine that you have a child element (layer, paragraph text, heading, etc…) contained within a parent layer. I often times want to adjust the how much room is above my child element, so I add the specification of “margin-top: 5px;” to the style of the child element to add the necessary space above the element. If you think that sounds logical, you’d be wrong (at least in the convoluted world of CSS)! The 5 pixel margin is taken from top of the child element and added to the top of the parent layer. Makes complete sense…right.
The impacts of this can wreak havoc with a layout. Generally, I expect that nested elements shouldn’t affect the appearance of the page around it. Because this will steal the margin from a child and apply it to the parent, any elements that then rely on the height of the parent layer, will then also break the layout, leading to white gaps, misaligned layers, etc…
The Solution:
The easiest solution is to just use padding, which I guess isn’t subject to this issue. Placing a “padding-top: 5px;” in the style of child layer should give the desired result. This can of course lead to issues if there is a background image or color in the child element that you don\’t want above the text, but so far this is the best solution, amongst a list of solutions that are far from perfect (The perfect solution would be to fix the issue in the CSS specification).
-
Adventure
-
The Built Environment
-
Locations
-
Web Dev