1
0

More than 1 year has passed since last update.

HTML&CSS marginの相殺について

Last updated at Posted at 2021-10-23

★FlexBoxではmarginの相殺が起きない★
(ドットインストール 「詳解CSS フレックスボックス編 #11 縦方向に要素を配置してみよう」)

HTML

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSSの練習</title>
  <link rel="stylesheet" href="css/styles.css">
</head>
<body>
  <header>Header</header>
  <div class="container">
    <main>Main</main>
    <nav>Nav</nav>
    <aside>Aside</aside>
  </div>
</body>
</html>

CSS

html {
  height: 100%;
}

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  height: 100%
}

header {
  background: pink;
  margin-bottom: 100px;
}

.container {
  display: flex;
  flex: 1;
  margin-top: 50px;
}

main {
  background: skyblue;
  flex: 1;
}

nav {
  background: tomato;
  width: 100px;
  order: -1;
}

aside {
  background: orange;
  width: 100px;
}

footer {
  background: silver;
}

上のように

header {
background: pink;
margin-bottom: 100px;
}

.container {
display: flex;
flex: 1;
margin-top: 50px;
}

だとmarginが打ち消されてmargin-bottom:100pxだけが適用されそうだが、
FlexBoxなので両方適用されて150pxとなる。

1
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
1
0