LoginSignup
3
1

More than 5 years have passed since last update.

親要素と子要素それぞれにfloatをかけてみた

Last updated at Posted at 2018-11-02

目的

親要素、子要素へのfloatのかかり方を身体で覚える

使ったファイル

  • sample.html
  • sample.css
sample.html
<html>
  <head>
    <meta charset="utf-8">
    <title>test</title>
    <link rel="stylesheet" href="sample.css">
  </head>
  <body>
  <div class="red"><div class="green"></div>
  </div>
    <div class="blue">他人</div>
  </body>
</html>
sample.css
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}

.red {
  background-color: red;
  height: 200px;
  width: 200px;
}

.green {
  background-color: green;
  height: 100px;
  width: 100px;
}

.blue {
  background-color: blue;
  height: 150px;
  width: 150px;
}
sample.htmlをブラウザで表示すると、こんな感じ

初期状態.png

子(green)にfloatをかけてみる
sample.css
.green {
  background-color: green;
  height: 100px;
  width: 100px;
  float: right;
}
すると、

子にfloat.png

親の範囲内でfloatした
次に子(green)のfloatを削除して、親(red)にfloatかけてみた
sample.css
.red {
  background-color: red;
  height: 200px;
  width: 200px;
  float: right;
}
すると、

親にfloat.png

親要素のみにfloatをかけたら、子要素も連れてfloatした

結論

親要素にfloatすれば子要素も一緒にfloatする
子要素にfloatすれば親要素の範囲内でfloatする

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