Before we even get started talking about Sass and the code for this
project, let me remind you this is no more than a quick experiment which
by no means should be used in a live context. So what follows is only a
proof of concept of what we could do with a couple of Sass variables
and the
There are probably still quite a couple of flaws and edge cases that are not handled in this example. If you have any recommendations for improving this, be sure to share. Otherwise keep in mind it’s just an experiment.
Then someone on Twitter asked me how I would build a grid system if I had to. Firstly, I wouldn’t. There are too many CSS grid systems and frameworks already, and building another one would be reinventing the wheel. In fact, I once wrote an article called We don’t need your CSS grid, even if my opinion is now slightly more peaceful than when I first wrote the article. Long story short: Some smart people already built grid-systems that are more powerful than any grid system I could ever come up with (like Breakpoint and Susy).
Anyway, I didn’t want to do a grid system like the others so I thought “hey, why not have some fun with
… which is actually a variable number of arguments (AKA an
But before looping, let’s add a couple of declarations to build the layout:
To target all children from the container calling the mixin, we use the
In our declaration block we float all immediate child elements and add a right margin. The wrapper has
Now, the loop!
Ouch, this looks complicated as hell! Let’s deal with this one line at a time. For the whole explanation, let’s assume we are calling
First, the selector. Don’t pay attention to the
Now the
And last but not least, our right sidebar (
Now if we add the three results to make sure everything’s right:
Which makes sense since we have 2 gutters of 10px. That’s all for the calculations folks. It wasn’t that difficult, was it?
Last important thing: We remove the right margin from the last child outside of the loop with another advanced CSS selector:
In case you’re wondering, applying margin to all children, then removing margin from last child, is better than just applying margin on every child except the last. I tried both.
Note: when using Sass variables in the
Because we don’t know the number of children, we can’t include the “ratio” for all children in the mixin inclusion. So I came up with a solution only requiring the pattern of a single row (e.g.
Also you may have noticed we are not using any sub-wrappers for each row since we don’t make any change to the DOM. So we need to make sure the last child of the container and the last child of each row each have no margin.
Hence the
Below this screen width, elements behave as they would without the grid system. That is, block-level elements stretch to fit the width of their parent and get positioned one under in source order. A simple, yet efficient, approach.
We could make our mixin extend placeholders instead of directly dumping CSS rules. First, let’s create our placeholders.
The first three placeholders speak for themselves. For the 4th placeholder, to avoid computing the width directly inside the mixin, we create as many placeholders as we need for our grid (e.g. 12 for 12-columns) with a
Regarding the
And here is what the mixin looks like now — doing no mare than extending placeholders:
calc()
function from CSS.There are probably still quite a couple of flaws and edge cases that are not handled in this example. If you have any recommendations for improving this, be sure to share. Otherwise keep in mind it’s just an experiment.
What Are We Trying to Do?
In the last couple of days I have been working with the calc() function in CSS.calc()
can be a blessing when it comes to cross-unit calculations, and gets
even better when you have a couple of Sass variables to make everything
easily customizable.Then someone on Twitter asked me how I would build a grid system if I had to. Firstly, I wouldn’t. There are too many CSS grid systems and frameworks already, and building another one would be reinventing the wheel. In fact, I once wrote an article called We don’t need your CSS grid, even if my opinion is now slightly more peaceful than when I first wrote the article. Long story short: Some smart people already built grid-systems that are more powerful than any grid system I could ever come up with (like Breakpoint and Susy).
Anyway, I didn’t want to do a grid system like the others so I thought “hey, why not have some fun with
calc
”?
I wanted to keep things as simple as they could be — three variables,
one breakpoint, one mixin. That’s all. The three customizable variables
are:- The number of columns in the grid (default
$grid-columns: 12
) - The width of a gutter between columns (default
$grid-gutter: 10px
) - The screen width, under which we move to a single column (default
$grid-breakpoint: 48em
)
:nth-of-type
.Mixin Core
I always try to keep my function and mixin signatures as lean as possible. For this one, I ended up needing no more than a single argument:
1
2
3
| @mixin grid($cols...) { // Sass magic } |
… which is actually a variable number of arguments (AKA an
argList
), as indicated by the ellipses after the $cols
variable. The main idea is to loop through those arguments and handle columns based on this thanks to the :nth-of-type
CSS selector. For instance, calling grid(6, 6)
on a 12-column based grid will create 2 columns separated by a 10px
gutter.But before looping, let’s add a couple of declarations to build the layout:
1
2
3
4
5
6
7
8
9
10
| @mixin grid($cols...) { overflow : hidden ; > * { float : left ; margin-right : $gutter; } // Loop } |
To target all children from the container calling the mixin, we use the
*
selector with the child combinator >
. I bet some of you are gasping already. Well… yes. It’s 2014, which means CSS performance is not a problem anymore. Also, since we’re using calc()
, we won’t be supporting anything below Internet Explorer 9, so we’re using > *
, alright!In our declaration block we float all immediate child elements and add a right margin. The wrapper has
overflow: hidden
to clear inner floats. If you’re more of a micro-clearfix person, be sure to change this to suit your needs. In case it’s a list, don’t forget to add list-style: none
and padding-left: 0
, if your CSS reset is not already doing so.Now, the loop!
1
2
3
4
5
6
| @for $i from 1 through length($cols) { > :nth-of-type(#{$i}n) { $multiplier: $i / $grid-columns; width : calc( 100% * #{$multiplier} - #{$grid-gutter} * ( 1 - #{$multiplier})); } } |
Ouch, this looks complicated as hell! Let’s deal with this one line at a time. For the whole explanation, let’s assume we are calling
grid(3, 7, 2)
,
which would be a pretty standard layout with a central container
circled with 2 sidebars. And don’t forget we have a 12-column layout
with 10px gutters, as we’ve defined in our variables earlier.First, the selector. Don’t pay attention to the
n
yet,
we’ll see why it’s there in the next section. For now all you have to
understand is we select children one by one to apply a width to them. By
the way, the empty space before :nth-of-type
is an implicit *
.Now the
calc()
function. The calculation looks pretty
intense, doesn’t it? Actually it’s not that hard to understand if you
picture it. Let’s deal with our first column (3
). If we go through our equation step by step, here is what we get:- 100% * 3 / 12 – 10px * (1 – 3 / 12)
- 100% * 0.25 – 10px * 0.75
- 25% – 7.5px
7
):- 100% * 7 / 12 – 10px * (1 – 7 / 12)
- 100% * 0.5833333333333334 – 10px * 0.41666666666666663
- 58.33333333333334% – 4.1666666666666663px
And last but not least, our right sidebar (
2
):- 100% * 2 / 12 – 10px * (1 – 2 / 12)
- 100% * 0.16666666666666666 – 10px * 0.8333333333333334
- 16.666666666666666% – 8.333333333333334px
Now if we add the three results to make sure everything’s right:
- (25% – 7.5%) + (58.33333333333334% – 4.1666666666666663px) + (16.666666666666666% – 8.333333333333334px)
- (25% + 58.33333333333334% + 16.666666666666666%) – (4.1666666666666663px + 8.333333333333334px + 7.5px)
- 100% – 20px
Which makes sense since we have 2 gutters of 10px. That’s all for the calculations folks. It wasn’t that difficult, was it?
Last important thing: We remove the right margin from the last child outside of the loop with another advanced CSS selector:
1
2
3
| > :nth-of-type(#{length($cols)}n) { margin-right : 0 ; } |
In case you’re wondering, applying margin to all children, then removing margin from last child, is better than just applying margin on every child except the last. I tried both.
Note: when using Sass variables in the
calc()
function, don’t forget to interpolate them if you want it to work. Remember calc()
is not compiled by Sass but by the browser itself so it needs to have all values properly written in the function.Unknown Number of Items
I suppose it is needless to say that the grid system handles nested grids quite well. One thing I wanted was the ability to have nested grids with an unknown number of children. There are numerous reasons for this, whether it be Ajax loading, lazyload, or whatever.Because we don’t know the number of children, we can’t include the “ratio” for all children in the mixin inclusion. So I came up with a solution only requiring the pattern of a single row (e.g.
grid(3, 3, 3, 3)
). Then if there are more than 4 children, they should still behave like it’s a 4-column grid (new row and so on).Also you may have noticed we are not using any sub-wrappers for each row since we don’t make any change to the DOM. So we need to make sure the last child of the container and the last child of each row each have no margin.
Hence the
:nth-of-type()
selectors we’ve seen previously. This means for instance that children 4
, 8
, 12
, and so on won’t have a right margin.Dealing with Small Screens
Now that we have everything working like a charm, we should make sure the grid degrades gracefully on small screens. I’m keeping it simple: Below the given breakpoint everything stacks as a single column.
1
2
3
4
5
6
7
8
9
| @mixin grid($cols...) { // Mixin core @media ( max-width : $breakpoint) { float : none ; width : 100% ; margin-right : 0 ; } } |
Below this screen width, elements behave as they would without the grid system. That is, block-level elements stretch to fit the width of their parent and get positioned one under in source order. A simple, yet efficient, approach.
Improving CSS Output with Placeholders
So far, it does the job very well. Everything works great and we are pretty happy, aren’t we? However if we happen to have multiple grids on the same page, there are a lot of duplicate CSS rules that could be merged to make output lighter.We could make our mixin extend placeholders instead of directly dumping CSS rules. First, let’s create our placeholders.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| %grid-parent { overflow : hidden ; } %grid-child { float : left ; margin-right : $grid-gutter; } %grid-last-child { margin-right : 0 ; } @for $i from 1 through $grid-columns { %grid-col-#{$i} { $multiplier: $i / $grid-columns; width : calc( 100% * #{$multiplier} - #{$grid-gutter} * ( 1 - #{$multiplier})); } } @media ( max-width : $grid-breakpoint) { %grid-fallback { float : none ; width : 100% ; margin-right : 0 ; } } |
The first three placeholders speak for themselves. For the 4th placeholder, to avoid computing the width directly inside the mixin, we create as many placeholders as we need for our grid (e.g. 12 for 12-columns) with a
@for
loop.Regarding the
%grid-fallback
placeholder, we have to
instantiate it inside a media query in order to be able to extend it
from within an equivalent media query elsewhere in the stylesheet.
Indeed, Sass has some restrictions regarding cross-media @extend (i.e.
it doesn’t work).And here is what the mixin looks like now — doing no mare than extending placeholders:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| @mixin grid($cols...) { @extend %grid-parent; > * { @extend %grid-child; @for $i from 1 through length($cols) { &:nth-of-type(#{$i}n) { @extend %grid-col-#{nth($cols, $i)}; } } &:nth-of-type(#{length($cols)}n) { @extend %grid-last-child; } } @media ( max-width : $grid-breakpoint) { @extend %grid-fallback; } } |
No comments:
Post a Comment