Does anyone know how to make a 20px
or 1em
gutter for the grid system below?
I got all the div
s to all go in a row but I want to know how to add a gutter in between each div. I'm doing this to learn how grids are made. jsFiddle Here.
body {
font:20px/1.2 Verdana, Arial; padding:0px; margin:0px;
}
*, *:after, *:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing:border-box;
}
.row { width:100%; }
.row > [class*='col-'] { /* ... */ }
.row > [class*='col-']:last-of-type { /* ... */ }
[class*="col-"] {
float:left;
height:200px;
background-color: #dedede;
border: 1px solid #000;
padding-right:20px;
}
[class*=col-]:last-of-type {
padding-right:0px;
}
.col-1-12 {
width: calc(100% / 12);
}
.col-2-12 {
width: calc(100% / 12 * 2);
}
.col-3-12 {
width: calc(100% / 12 * 3);
}
.col-4-12 {
width: calc(100% / 12 * 4);
}
HTML:
<div class="row">
<div class="col-4-12">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
<div class="col-2-12">
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
<div class="col-3-12">
Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
<div class="col-3-12">
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
</div>
Well, here is the calculation of the columns' width, base on the column number considering the gutter of
20px
between each column.For instance,
col-2-12
:Explanation:
Also, instead of using
margin
for the first and the last column, you can usepadding
for the container.row
and set themargin
for that columns to0
.In addition, as the columns are floated, you should clear the float at the bottom of the
.row
element.WORKING DEMO.
Sassy CSS
Using CSS preprocessors such as Sass, makes the calculation of grid systems fun!
Here is the Sassy way of a fluid grid system:
ONLINE DEMO.