Talk on creating your own flexible grid system delivered at the Boston Front-End Developers Meetup on January 23, 2012.
http://www.meetup.com/Boston-Frontend-Developers/events/91356942/
2. Hi, I’m Steve.
I design/code applications for Fresh Tilled Soil in Watertown.
3. We Use Grids Because They’re Awesome
They create order from chaos.
4. But they can be difficult to do well on the web. This is why we
have so many frameworks, but they have issues.
<div class="row">
<h2 class="span4">It slices!</h2>
<h2 class="span4">It dices!</h2>
<h2 class="span4">It juliennes!</h2>
</div>
/* ================================================================== */
.row { margin-left: -20px; }
.row:before, .row:after { display: table; line-height: 0; content: ""; }
.row:after { clear: both; }
.span4 { width: 300px; }
7. CEO: Close, but where’s my picture? I work very hard! In fact, get
all the execs on here.
But make them smaller than me.
8. CTO: I hear responsive design is pretty big right now. Why isn’t this
responsive, aren’t we paying you enough?
9. The stress has made them lunatics.
We can prevent this from happening to you.
10. Let’s Get SASS-y
SASS is a CSS pre-processor. It allows us to do things like:
1. Nest selectors
2. Use variables
3. Extend classes with mixins
4. Avoid repetition
11. We’ll use SASS to apply styles to pre-existing semantic classes
with some mixins.
These styles will be calculated with SASS functions so our
elements adjust automatically when we change variables.
14. • Total width of 1000px.
• 6 columns @ 150px wide each
• 20px gutters
Percentages are calculated with target / context = result
to get flexible widths: 15% columns with 2% gutters.
15. We’ll store this information in some SASS variables.
$max-width: 1000px; // set page max-width
$column-width: 15%; // set column width
$gutter-width: 2%; // set gutter width
$maximum-columns: 6; // set max number of columns
16. Then we’ll define some helpful mixins. They have use beyond
our grid system and should be defined globally.
@mixin clearfix {
zoom: 1;
&:before, &:after { content: ""; display: table; }
&:after { clear: both; }
}
@mixin border-box {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
17. Now, the fun stuff:
This function uses variables to make flexible column widths.
@function columns($columns, $container-columns: $maximum-columns) {
$width:
$columns * $column-width + ($columns - 1) * $gutter-width;
$container-width:
$container-columns * $column-width + ($container-columns - 1) * $gutter-width;
@return percentage($width / $container-width);
}
* Borrowed and modified from the excellent Bourbon.io
18. And this function helps us get the correct width for gutters.
@function gutter($container-columns: $maximum-columns, $gutter: $gutter-width) {
$container-width:
$container-columns * $column-width + ($container-columns - 1) * $gutter-width;
@return percentage($gutter / $container-width);
}
* Borrowed and modified from the excellent Bourbon.io
19. We’ll use these functions on elements like so:
div.parent {
width: columns(3); // takes 1 arg: column span
margin-right: gutter; // no args required
div.child {
width: columns(1, 3); // takes 2 args: span, span of parent
margin-right: gutter(3); // takes 1 arg: span of parent
}
}
21. For a full grid system we’ll need to define a few more behaviors.
This mixin helps to control nesting elements.
@mixin nesting {
padding: 0; // no padding so nested elements fit
& > div { // affect only immediate children
float: left;
margin-right: gutter;
@include border-box; // math is hard. let’s use border-box
}
}
22. And this one tells elements to behave like rows.
@mixin row {
width: 100%; // make sure to fill its container
max-width: $max-width; // but no more than our max width
margin: 0 auto;
@include clearfix; // clear our floats
@include nesting; // add nesting styles to rows
}
23. For a more advanced layout we’ll need to offset elements. This
function handles the calculations for us.
@function offset-columns($columns) {
$margin: $columns * $column-width + $columns * $gutter-width;
@return $margin;
}
24. And we can apply it using our offset mixin.
@mixin offset($from-direction: left, $columns) {
@if $from-direction == left {
float: left;
margin-left: offset-columns($columns);
}
@if $from-direction == right {
float: right;
margin-right: offset-columns($columns);
}
}
25. There’s one more pesky problem to deal with:
the last element in any row...
@mixin last {
margin-right: 0;
float: right;
}
26. Note:
I tried :last-child, :first-child and :nth-child(). They would be
good solutions, but support is bad or intent is wrong.