float 3 divs equally - COOL wrapping options

by Andy Bulka

HTML

<p>From <a href="http://stackoverflow.com/questions/16553635/css-how-to-float-3-divs-side-by-side-for-width100-without-messing-up">stackoverflow</a></p>

By default, borders are out of the box-size, So 3 elements of width 33% are over 100% (99% + 6 times the border size!).<br clear="all" />
    <div class="left">...A1</div>
    <div class="left">...A2</div>
    <div class="left">...A3</div>

<br clear="all" /><hr>
Use { box-sizing: border-box; } so the border is counted within the box-size. Thus, 3 divs of widht="33%" are exactly equal to 99%. It fit well in the body { width: 100%; }:<br clear="all" />
    <div class="left box-sizing">...B1</div>
    <div class="left box-sizing">...B2</div>
    <div class="last box-sizing">...B3</div></div>

CSS

.left {
  float:left; 
  width:33.3%;
  border: 3px solid #333;
  
  background: #C0C0C0;
}
.last{
  float:left; 
  width:33.3%;
  border: 3px solid #333;
  
  background: #C0C0C0;
}
.box-sizing { box-sizing: border-box; margin: 0; }

/* Smart Phone */
@media (min-width: 1px) and (max-width: 638px) {
.left {
  float:left; 
  width:50%;
  border: 3px solid #333;
  
  background: #30C0C0;
}
.last{
  clear:both !important;
  width:100%;
  border: 3px solid #333;
  
  background: #30C0C0;
}
}

/* iPad */
@media (min-width: 768px) and (max-width: 960px) {
.left {
  float:left; 
  width:33.3%;
  border: 3px solid #333;
  
  background: #F0C0C0;
}
.last{
  float:left; 
  width:33.3%;
  border: 3px solid #333;
  
  background: #C0C0C0;
}
}