From fb72298beae301c207efc5efd288438cc4e76410 Mon Sep 17 00:00:00 2001 From: Masahiro FUJIMOTO Date: Sun, 7 Nov 2021 23:53:04 +0900 Subject: CSS Box Model 関係の文書を更新 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 2021/11/06 時点の英語版に同期 --- .../mastering_margin_collapsing/index.md | 110 +++++++++------------ 1 file changed, 47 insertions(+), 63 deletions(-) (limited to 'files/ja/web/css/css_box_model/mastering_margin_collapsing') diff --git a/files/ja/web/css/css_box_model/mastering_margin_collapsing/index.md b/files/ja/web/css/css_box_model/mastering_margin_collapsing/index.md index 1e62749980..ed20cd38df 100644 --- a/files/ja/web/css/css_box_model/mastering_margin_collapsing/index.md +++ b/files/ja/web/css/css_box_model/mastering_margin_collapsing/index.md @@ -1,55 +1,53 @@ --- -title: マージンの相殺 +title: マージンの相殺の習得 slug: Web/CSS/CSS_Box_Model/Mastering_margin_collapsing tags: - CSS - - CSS Box Model - CSS ボックスモデル - - Guide - - Reference + - ガイド + - リファレンス translation_of: Web/CSS/CSS_Box_Model/Mastering_margin_collapsing --- -
{{CSSRef}}
+{{CSSRef}} -

ブロックの先頭末尾のマージンは、それぞれのマージンの最も大きい寸法 (または等しい場合はいずれか1つ) の単一のマージンに結合される (折り畳まれる (collapsed)) ことがあり、マージンの相殺という動作として知られています。なお、浮動要素と絶対位置指定要素では折り畳まれません。

+ブロックの[上端](/ja/docs/Web/CSS/margin-top)と[下端](/ja/docs/Web/CSS/margin-bottom)のマージンは、それぞれのマージンの最も大きい寸法 (または等しい場合はいずれか 1 つ) の単一のマージンに結合される (折り畳まれる、collapsed) ことがあり、**マージンの相殺**という動作として知られています。なお、[浮動](/ja/docs/Web/CSS/float)要素と[絶対位置指定](/ja/docs/Web/CSS/position#types_of_positioning)要素では折り畳まれません。 -

マージンの相殺は、基本的に3つの場合に発生します。

+マージンの相殺は、基本的に 3 つの場合に発生します。 -
-
隣接兄弟要素
-
隣接兄弟要素のマージンは相殺されます (後ろの兄弟要素がそれ以前の浮動状態を 解除 (clear) する必要がある場合を除く)。
-
親と子孫を隔てるコンテンツがない場合
-
あるブロックの {{cssxref("margin-top")}} と、1つ以上の子孫ブロックの {{cssxref("margin-top")}} を隔てる境界、パディング、インライン部分、 ブロック整形コンテキストの生成、浮動の解除 (clear) のいずれもがない場合、もしくは、あるブロックの {{cssxref("margin-bottom")}} と1つ以上の子孫ブロックの {{cssxref("margin-bottom")}} を隔て境界、パディング、インラインコンテンツ、 {{cssxref("height")}}, {{cssxref("min-height")}}, {{cssxref("max-height")}} のいずれもがない場合、それぞれマージンが折り畳まれます。折り畳まれた側のマージンは、親要素の外側に出ます。
-
空ブロック
-
あるブロックの {{cssxref("margin-top")}} を {{cssxref("margin-bottom")}} と隔てる境界、パディング、インラインコンテンツ、 {{cssxref("height")}}, {{cssxref("min-height")}} のいずれもがない場合、上端と下端のマージンは相殺されます。
-
+- 隣接兄弟要素 + - : 隣接兄弟要素のマージンは相殺されます (後ろの兄弟要素がそれ以前の浮動状態を[解除](/ja/docs/Web/CSS/clear) する必要がある場合を除く)。 +- 親と子孫を隔てるコンテンツがない場合 + - : あるブロックの {{cssxref("margin-top")}} と、1 つ以上の子孫ブロックの {{cssxref("margin-top")}} を隔てる境界、パディング、インライン部分、 [ブロック整形コンテキスト](/ja/docs/Web/Guide/CSS/Block_formatting_context)の生成、_[浮動の解除](/ja/docs/Web/CSS/clear)_ のいずれもがない場合、もしくは、あるブロックの {{cssxref("margin-bottom")}} と1つ以上の子孫ブロックの {{cssxref("margin-bottom")}} を隔て境界、パディング、インラインコンテンツ、 {{cssxref("height")}}, {{cssxref("min-height")}}, {{cssxref("max-height")}} のいずれもがない場合、それぞれマージンが折り畳まれます。折り畳まれた側のマージンは、親要素の外側に出ます。 +- 空ブロック + - : あるブロックの {{cssxref("margin-top")}} を {{cssxref("margin-bottom")}} と隔てる境界、パディング、インラインコンテンツ、 {{cssxref("height")}}, {{cssxref("min-height")}} のいずれもがない場合、上端と下端のマージンは相殺されます。 -

注意すべきことがいくつかあります。

+注意すべきことがいくつかあります。 - +- これらのケースが組み合わさると、もっと複雑な (3 つ以上の) マージンの相殺が発生します。 +- このルールはマージン幅がゼロの場合にも当てはまるので、親要素のマージンがゼロであるかどうかに関わらず、先頭/末尾の子要素のマージンは最終的に、 (上のルールに従って) その親要素より外側に出ます。 +- 負のマージンが含まれる場合には、相殺されるマージンの大きさは、一番大きな正のマージンと一番小さな (もっともマイナス寄りの) マージンの合計値になります。 +- すべてのマージンが負の場合、折りたたまれたマージンのサイズは、最小の (最もマイナス寄りの) マージンとなります。これは、隣接する要素と入れ子になった要素の両方に適用されます。 -

+

-

HTML

+### HTML -
<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>
+```html
+

この段落の下端マージンは相殺されています …

+

… この段落の上端マージンとの間で 1.2rem のマージンになります。

-<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> +
この親要素には 2 つの段落があります。 +

この段落には上のテキストとの間に .4rem のマージンがあります。

+

下端マージンは親と相殺され、下端マージンは 2rem になります。

+
-<p>I am <code>2rem</code> below the element above.</p>
+

上の要素から下に 2rem の位置です。

+``` -

CSS

+### CSS -
div {
+```css
+div {
   margin: 2rem 0;
   background: lavender;
 }
@@ -57,33 +55,19 @@ translation_of: Web/CSS/CSS_Box_Model/Mastering_margin_collapsing
 p {
   margin: .4rem 0 1.2rem 0;
   background: yellow;
-}
- -

- -

{{EmbedLiveSample('Examples', 'auto', 350)}}

- -

仕様書

- - - - - - - - - - - - - - - - -
仕様書状態備考
{{SpecName("CSS2.1", "box.html#collapsing-margins", "margin collapsing")}}{{Spec2("CSS2.1")}}初回定義
- -

関連情報

- - +} +``` + +### 結果 + +{{EmbedLiveSample('Examples', 'auto', 350)}} + +## 仕様書 + +| 仕様書 | 状態 | 備考 | +| -------------------------------------------------------------------------- | ------------------- | -------- | +| {{SpecName("CSS2.1", "box.html#collapsing-margins", "margin collapsing")}} | {{Spec2("CSS2.1")}} | 初回定義 | + +## 関連情報 + +- {{css_key_concepts}} -- cgit v1.2.3-54-g00ecf