CodePen

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}
            
          
!
? ?
? ?
Must be a valid URL.
+ add another resource
via CSS Lint

JS

            
              
            
          
!
Must be a valid URL.
+ add another resource
via JS Hint
Loading ..................