cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <p>See <a href="https://rachelandrew.co.uk/archives/2017/01/24/the-end-of-the-clearfix-hack/">the end of the clearfix hack</a></p>

# "display:flow-root" vs. "clearfix"

A new value of the display property that will enable containers to <strong>contain</strong> floats. *Needs Chrome Canary or Firefox Nightlies*.

In the following examples, the markup is as follow:
  
```
<div class="float"></div>  
<div class="container">
  <div class="float"></div>
  <p>Lorem Ipsum</p>
</div>
```
 
Note that all floats are styled with a margin of 20px. 
 
## `.clearfix`

In this example we use "clearfix" to cause the wrapper (`.container`) to <strong>contain</strong> the floated item.

<div class="float"></div>  
<div class="container container1">
  <div class="float"></div>
  <p>Lorem Ipsum</p>
</div>
  
2 things to notice:

   * The container extends *across its parent box*&mdash;to the left side of its sibling (the first float).
   * The text in the paragraph shows *at the very top of the container*.

## `display:flow-root`

With `flow-root` our boxes behave very differently:

<div class="float"></div>
<div class="container container2">
  <div class="float"></div>
  <p>Lorem Ipsum</p>
</div>

2 things to notice:

   * The container shows *on the right hand side* of the 1st float. 
   * The text in the paragraph shows *further down*.
   
## Why that?

`flow-root` creates a block-formatting context which&mdash;on top of containing floats&mdash;does a couple of other things:

   * it prevents margins from collapsing (the reason why the text does not show at the same place)
   * it "escapes" floats (the reason why the container shows next to the 1st float instead of spreading across its parent)
   
## Collapsing margin is a big deal   
 
Let's see how siblings behave when styled with `.clearfix`
 
<div class="container container1">
  <p>Lorem Ipsum</p>
</div>
<div class="container container1">
  <p>Lorem Ipsum</p>
</div>
<div class="container container1">
  <p>Lorem Ipsum</p>
</div>
<div class="container container1">
  <p>Lorem Ipsum</p>
</div>

versus `display:flow-root`

<div class="container container2">
  <p>Lorem Ipsum</p>
</div>
<div class="container container2">
  <p>Lorem Ipsum</p>
</div>
<div class="container container2">
  <p>Lorem Ipsum</p>
</div>
<div class="container container2">
  <p>Lorem Ipsum</p>
</div>

            
          
!
            
              .container1::after {
  content: "";
  display: block;
  clear: both;
}

/**
 * we use overflow:hidden here instead of display:flow-root so we can check the effect of the block-formatting context across all browsers (even the ones that do not yet support display:flow-root)
 */

.container2 {
  overflow: hidden;
}








.container {
  border-radius: 5px;
  background-color: lime;
}

.float {
  height: 50px;
  width: 100px;
  background-color: teal;
  border-radius: 3px;
  float: left;
  margin: 20px;
}

.container > .float {
  height: 100px;
}

h2 {
  padding: 1em 0 0 0;
}

body {
  font: 18px Helvetica, sans-serif;
  max-width: 600px;
  margin: 40px auto;
}

            
          
!
999px
Loading ..................

Console