Files
30-seconds-of-code/snippets/clearfix.md
atomiks 767b720f10 Init
2018-02-26 00:14:39 +11:00

1.1 KiB

Clearfix

Ensures that an element self-clears its children.

HTML

<div class="clearfix">
  <div class="floated">float a</div>
  <div class="floated">float b</div>
  <div class="floated">float c</div>
</div>

CSS

.clearfix::after {
  content: '';
  display: table;
  clear: both;
}

.floated {
  float: left;
}

Demo

float a
float b
float c
<style> .snippet-demo__clearfix::after { content: ''; display: table; clear: both; } .snippet-demo__floated { float: left; } </style>

Explanation

  1. .clearfix::after defines a pseudo element.
  2. content: '' allows the pseudo element to affect layout.
  3. clear: both indicates that the left, right or both sides of the element cannot be adjacent to earlier floated elements within the same block formatting context.

Browser support

No caveats.