0
0

More than 3 years have passed since last update.

margin が相殺される場合とされない場合

Last updated at Posted at 2019-11-05

相殺される場合

<!DOCTYPE html>
<html lang="ja" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
    <div class="box">
    </div>
    <div class="box2">
    </div>
  </body>
</html>
.box {
  width: 200px;
  height: 200px;            
  margin: 20px;
  border: 1px solid red;
}
.box2 {
  width: 200px;
  height: 200px;
  margin: 20px;
  border: 1px solid blue;
}

スクリーンショット 2019-11-05 15.37.57.png

この場合boxの下marginとbox2の上marginが重なってしまっているため間隔は40pxとはならず20pxになる。
これが mrginの相殺 である。

相殺されない場合

親要素と子要素のmarginを分けられるようなborder、paddingなどをが存在する場合には相殺されない。

<div class="parent">
  <div class="child">
  </div>
</div>
.parent {
  background-color: red;
  width: 200px;
  height: 100px;
  border: 1px solid black;
}
.child {
  background-color: blue;
  width: 50%;
  height: 50%;
  margin-top: 20px;
}

親要素にborderがあるので、子要素のmargin-topが効く。

スクリーンショット 2019-11-05 15.51.08.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0