CSSマージンの折りたたみ
2つのマージンが1つのマージンに折りたたまれることがあります。
マージン崩壊
要素の上下の余白は、2つの余白の最大値に等しい単一の余白に折りたたまれることがあります。
これは、左右の余白では発生しません。上マージンと下マージンのみ!
次の例を見てください。
例
マージン崩壊のデモンストレーション:
h1 {
margin: 0 0 50px 0;
}
h2 {
margin: 20px 0 0 0;
}
上記の例では、<h1>要素の下部マージンは50pxで、<h2>要素の上部マージンは20pxに設定されています。
常識的には、<h1>と<h2>の間の垂直方向のマージンは合計70px(50px + 20px)になると思われます。しかし、マージンの崩壊により、実際のマージンは50pxになります。
すべてのCSSマージンプロパティ
Property | Description |
---|---|
margin | A shorthand property for setting the margin properties in one declaration |
margin-bottom | Sets the bottom margin of an element |
margin-left | Sets the left margin of an element |
margin-right | Sets the right margin of an element |
margin-top | Sets the top margin of an element |