Home >Web Front-end >CSS Tutorial >Collapsing Margins in CSS: A Beginners Guide
There's nothing more frustrating than applying a style and having it not work as expected. Collapsing Margin is one phenomenon that causes unexpected styling outcomes. Thus, what is a collapsing margin and how does it affect margins in CSS?
Collapsing margins stem from the CSS property margin, which controls the spacing outside an element. As the name suggests, collapsing margins occur when the margins of adjacent elements combine or 'collapse' into one, rather than adding up. This typically happens between sibling elements or between a parent and child element. For instance, if two sibling elements have margins—one with a 20pxbottom margin and the other with a 30px top margin—you might expect the total margin to be 50px. However, due to collapsing margins, only the larger margin of 30px will be applied, and the smaller margin of 20px will be collapsed.
.element1 { margin-bottom: 20px; } .element2 { margin-top: 30px; }
Moreover, in cases where a parent element lacks padding or border, and its child element possesses a top margin, the margin may "collapse" beyond the parent, thereby impacting the parent's placement.
.parent { margin-top: 0; } .child { margin-top: 20px; }
The 20px top margin from the .child might collapse outside of the .parent, moving the entire parent down by 20px.
This can confuse noob developers (well I never knew about it until recently), as the resulting spacing may not match their expectations.
.parent { padding-top: 1px; /* or border-top: 1px solid transparent; */ }
.parent { overflow: hidden; }
.parent { display: flex; /* Flexbox layout */ flex-direction: column; /* Stack children vertically */ border: 1px solid #000; /* Just for visibility */ } .child1, .child2 { margin: 20px 0; /* Vertical margins that won't collapse */ background-color: lightblue; }
.parent { display: grid; /* Grid layout */ grid-template-rows: auto auto; /* Define two rows */ border: 1px solid #000; /* Just for visibility */ } .child1, .child2 { margin: 20px 0; /* Vertical margins */ background-color: lightgreen; }
.parent { border: 1px solid #000; /* Just for visibility */ } .child1, .child2 { float: left; /* Prevents margin collapsing */ width: 100%; /* Full width */ margin: 20px 0; /* Vertical margins */ background-color: lightcoral; }
.parent { border: 1px solid #000; /* Just for visibility */ } .child1, .child2 { display: inline-block; /* Prevents margin collapsing */ width: 100%; /* Full width */ margin: 20px 0; /* Vertical margins */ background-color: lightyellow; }
Understanding margin collapsing can significantly contribute to consistent and expected spacing in your web layout, particularly when working with block-level elements. It's worth noting that inline elements such as , , , etc. are generally unaffected by margin collapsing due to their distinct behavior in generating vertical margins compared to block-level elements. Margin collapsing primarily poses challenges with block-level elements, such as , The above is the detailed content of Collapsing Margins in CSS: A Beginners Guide. For more information, please follow other related articles on the PHP Chinese website!, and others, as they may have vertical margins that interact with each other.
References