Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialLiam Maclachlan
22,805 PointsSharing a great tool for generating media query based grid systems that I built in SASS. Feedback appreciated.
Check out the Codepen here where you can modify the setting to print out your desired media based CSS grid system.
Feel free to use this if you find it useful. Credit appreciated. I'm pretty damn happy with it :)
Code below for link-phobics :p
///////////////
// Variables //
///////////////
//map
$sizes__map : (
small: 1px,
medium: 768px,
large: 960px,
largest: 1080px,
xlargest: 1240px
);
//desired ouput
$grid__includes : true; // true/false
$grid__classes : true; // true/false
//grid config
$grid__column-count : 12;
$grid__gutter-width : 10px;
$grid__column-width : 70px;
///////////////
// Functions //
///////////////
// Finds the next value in the give map, from the current position
@function nextKey($current-value, $mapped-list:$sizes__map) {
// get current key from list
$the-list: map-keys($mapped-list);
//find index of current value and add 1
$the-index: ( index( $the-list, $current-value) ) + 1;
// get value from list with new index
$new-value : nth($the-list, $the-index);
@return $new-value;
}
// Generates context that can be converted to a percent, later on
@function gridContext($g-col-count, $g-col-w, $g-gutter-w) {
$g-context: ( $g-col-count * $g-col-w ) + ( $g-col-count * ( $g-gutter-w - 1 ) );
@return $g-context;
}
///////////////////////
// The Mammoth Mixin //
///////////////////////
@mixin media($breakpoint) {
// turn the
$mapped__keys : map-keys($sizes__map);
$index : index($mapped__keys, $breakpoint);
@if $index {
@if $index < length($sizes__map) {
@media screen and (min-width : map-get($sizes__map, $breakpoint ) ) and (max-width: map-get($sizes__map, nextKey($breakpoint) ) - 1 ) {
body {
height: $index;
}
@content;
}
}
@elseif $index == length($sizes__map) {
@media screen and (min-width : map-get($sizes__map, $breakpoint) ) {
body {
width: $index;
}
@content;
}
}
}
@else {
@error "Sorry but #{$breakpoint} is not a breakpoint in your setup";
}
}
///////////////////
// The Grid Loop //
///////////////////
// Non-media based grid
@for $i from 1 through $grid__column-count {
$context: gridContext($grid__column-count, $grid__column-width, $grid__gutter-width);
$target: ( $i * $grid__column-width ) + ( $i * ( $grid__gutter-width - 1 ) );
$set-the-width : percentage($target/$context);
@if $grid__includes == true {
%grid__col--#{$i} {
width: $set-the-width;
}
}
@if $grid__classes == true {
.grid__col--#{$i} {
width: percentage($target/$context);
}
}
}
// new loop for media grid system
@each $key, $value in $sizes__map {
@include media($key) {
@for $i from 1 through $grid__column-count {
$context: gridContext($grid__column-count, $grid__column-width, $grid__gutter-width);
$target: ( $i * $grid__column-width ) + ( $i * ( $grid__gutter-width - 1 ) );
$set-the-width : percentage($target/$context);
@if $grid__includes == true {
%#{$key}__col--#{$i} {
width: $set-the-width;
}
}
@if $grid__classes == true {
.#{$key}__col--#{$i} {
width: percentage($target/$context);
}
}
}
.#{$key}__alpha {
float: left;
margin-left: 0;
}
.#{$key}__omega {
float: right;
margin-left: 0;
margin-right: 0;
}
}
}
Liam Maclachlan
22,805 PointsHey man. Thanks :) I am quite proud of it. I developed the basic grid system guts on one of the SASS courses (I can't remember which but they are all here) and I think it took me about 6-8 hours of coding to work out how to merge the media query loop with an SCSS map, that drives the whole thing.
It was a real pain getting the next variable in the list. That's when I worked out the nextKey() function to help out :)
This was a lot of fun and I'm really hoping it will save me (and others) time in the future when setting up new projects :)
Liam Maclachlan
22,805 PointsIt's worth noting that this one here was my first attempt at it, by I knew it could be better =p
Himanshu Chopra
3,055 PointsIt's really great bro. I know it must have took you enough time to build this. I have very basic knowledge of SCSS and your this Sass based grid system looks very useful.
Thanks so much for sharing.
Liam Maclachlan
22,805 PointsNP man. SASS is amazing. I seriously recommend the other SASS course, when you get the time :)
Himanshu Chopra
3,055 PointsThanx Buddy. Hi Man can you help me with something. I have posted a question here. here is the link for that.
(https://trello.com/c/qJzj3q9A/118-incorrect-notification-message-when-admin-client-is-edited)
Could you please help me with that if you have some free time.
Liam Maclachlan
22,805 PointsJust tried to click it but it says it is a private board :/
Himanshu Chopra
3,055 PointsHimanshu Chopra
3,055 PointsThis is simply awesome bro. The media query based grid system you built in sass is really cool. the code it tough for me to understand but looks useful.
Just wondering, How much time did it take you to build this?