0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HTML clearfixの使い方

Last updated at Posted at 2019-10-01

Memo

clearfixの使い方

サンプルを準備

sample
<div class="box1">box1
	<div class="box2">box2</div>
	<div class="box3">box3</div>
</div>

<div class="box4">box4</div>
css
.box1 {
  width: 500px;
  border: 5px solid #000;
}
.box2 {
  height:100px;
  width: 100px;
  background-color:#999;
  float: left;
}
.box3 {
  height:100px;
  width: 100px;
  background-color:#777;
  float: left;
}
.box4 {
  height:50px;
  width: 100px;
  background-color:#555;
  float: left;
}
/*-- clearfix このCSSが重要 --*/
.clearfix::after {
  content: "";
  display: block;
  clear: both;
}

サンプルではfloatした要素の親要素の高さが無い状態

See the Pen RwbzYMP by abek922 (@abek922) on CodePen.

floatした要素の親要素にクラス名clearfixをつけてclearfixのCSSを適応

sample
<div class="box1 clearfix">
	<div class="box2"></div>
	<div class="box3"></div>
</div>

<div class="box4"></div>

clearfixのCSSを読み込むと高さがとれる状態になる

See the Pen Clearfix_2 by abek922 (@abek922) on CodePen.

clearfix

clearfix
.clearfix::after {
  content: "";
  display: block;
  clear: both;
}
0
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?