octocatstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource
via CSS Lint

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource
via JS Hint

Code Indentation

     

Save Automatically?

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

Want a Run Button?

If active, the preview will update automatically when you change code.

HTML

            
              <h1>Container-relative floats</h1>

<p>The value of <code>margin-right: -100%</code> makes sure that every subsequent element starts right at the left edge of the container and isn't aware of its preceding sibling. This way the order of the elements within each row in the HTML code doesn't matter (try to move .item2 to the top or delete it). Furthermore you just need to assign <code>clear: left</code> to start a new row. Kind of absolute positioning but with rows which are aware of each other.</p>

<div class="wrapper">
  <!-- first row -->
	<div class="item1">Element<br><br><br></div>
	<div class="item2">Element<br><br></div>
  <div class="item3">Element<br><br><br><br></div>
  
	<!-- second row -->
  <div class="item4">Element<br><br><br><br></div>
  <div class="item5">Element<br><br></div>
  <div class="item6">Element<br><br><br></div>
</div>

<p class="src">Inspiration: <a href="http://www.palantir.net/blog/responsive-design-s-dirty-little-secret">http://www.palantir.net/blog/responsive-design-s-dirty-little-secret</p>
            
          
!

CSS

            
              .item1 {
  float: left;
  width: 20%;
  /* margin-left: 0; */
  margin-right: -100%; /* Every element needs that */
  background: red;
}
.item2 {
  float: left;
  width: 30%;
  margin-left: 20%; /* The sum of the widths of the preceding elements */
  margin-right: -100%; /* Every element needs that */
  background: blue;
}
.item3 {
  float: left;
  width: 50%;
  margin-left: 50%; /* The sum of the widths of the preceding elements: 20% + 30% */
  margin-right: -100%; /* Every element needs that */
  background: green;
}
.item4 {
  clear: left; /* Starts a new line */
  float: left;
  width: 10%;
  margin-left: 0;
  margin-right: -100%; /* Every element needs that */
  background: yellow;
}
.item5 {
  float: left;
  width: 80%;
  margin-left: 10%; /* The sum of the widths of the preceding elements */
  margin-right: -100%; /* Every element needs that */
  background: magenta;
}
.item6 {
  float: left;
  width: 10%;
  margin-left: 90%; /* The sum of the widths of the preceding elements: 10% + 80% */
  margin-right: -100%; /* Every element needs that */
  background: orange;
}

/* Just for styling purposes */
.src {clear: left; padding-top: 1em}
            
          
!

JS

            
              
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console