--- title: 여백 상쇄 정복 slug: Web/CSS/CSS_Box_Model/Mastering_margin_collapsing tags: - CSS - CSS Box Model - Guide - Reference translation_of: Web/CSS/CSS_Box_Model/Mastering_margin_collapsing ---
여러 블록의 위쪽 및 아래쪽 바깥 여백(마진)은 경우에 따라 제일 큰 여백의 크기를 가진 단일 여백으로 결합(상쇄)되곤 합니다. 이런 동작을 여백 상쇄라고 부릅니다. 단, 플로팅 요소와 절대 위치를 지정한 요소의 여백은 절대 상쇄되지 않습니다.
여백 상쇄는 다음과 같은 세 가지 기본 상황에 발생합니다.
margin-top
과 분리할 권한이 없는 경우, 또는, 부모 블록에 테두리, 안쪽 여백, 인라인 콘텐츠가 없으며 부모의 {{cssxref("margin-bottom")}}과 자손의 margin-bottom
을 분리할 {{cssxref("height")}}, {{cssxref("min-height")}}, {{cssxref("max-height")}}가 존재하지 않는 경우 부모와 자손의 여백이 상쇄됩니다. 상쇄된 여백은 부모 블록 바깥에 위치합니다.다음은 참고할만한 사항입니다.
<p>The bottom margin of this paragraph is collapsed …</p> <p>… with the top margin of this paragraph, yielding a margin of <code>1.2rem</code> in between.</p> <div>This parent element contains two paragraphs! <p>This paragraph has a <code>.4rem</code> margin between it and the text above.</p> <p>My bottom margin collapses with my parent, yielding a bottom margin of <code>2rem</code>.</p> </div> <p>I am <code>2rem</code> below the element above.</p>
div { margin: 2rem 0; background: lavender; } p { margin: .4rem 0 1.2rem 0; background: yellow; }
{{EmbedLiveSample('예제', 'auto', 350)}}
명세 | 상태 | 설명 |
---|---|---|
{{SpecName("CSS2.1", "box.html#collapsing-margins", "margin collapsing")}} | {{Spec2("CSS2.1")}} | 초기 정의 |