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 trial

CSS

Liam Maclachlan
Liam Maclachlan
22,805 Points

Media query based grid system: Sharing some fun with SASS development

So, I took a little bit of time and wanted to create a responsive grid sytem that would allow me to add and remove screen sizes with minimal fuss and maintenance. This is what I have come up with: (Codepen)

// Media breakpoints
$media__xsmall      : 1px;
$media__small       : 480px;
$media__medium      : 768px;
$media__large       : 960px;
$media__xlarge      : 1080px;

//Media queries
@mixin media($breakpoint) {
    @if $breakpoint == "xsmall" {
        @media screen and (min-width: $media__xsmall) and (max-width: $media__small - 1 ) {
            @content;
        }
    }
    @else if $breakpoint == "small" {
        @media screen and (min-width: $media__small) and (max-width: $media__medium - 1 ) {
            @content;
        }
    }
    @else if $breakpoint == "medium" {
        @media screen and (min-width: $media__medium) and (max-width: $media__large - 1 ) {
            @content;
        }
    }
    @else if $breakpoint == "large" {
        @media screen and (min-width: $media__large ) and (max-width: $media__xlarge - 1) {
            @content;
        }
    }
    @else if $breakpoint == "xlarge" {
        @media screen and (min-width: $media__xlarge ) {
            @content;
        }
    }
    @else {
        @error "Meida mixin only allows xsmall, small, medium, large or xlarge as arguements";
    }
}

// Grid config
$grid__column-count : 16;
$grid__gutter-width : 10px;
$grid__column-width : 70px;

// Function
@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;
}


//Grid System for media queries
$sizes : (xsmall, small, medium, large);

.grid__container {
    width: 100%;
}

// 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 ) );
    %grid__col--#{$i} {
        width: percentage($target/$context)!important;
    }
    .grid__col--#{$i} {
        @extend %grid__col--#{$i};
    }
}
.alpha {
    float: left;
    margin-left: 0;
}

.omega {
    float: right;
    margin-left: 0;
    margin-right: 0;
}

// Media based grid

$sizes : (xsmall, small, medium, large, xlarge);

@each $size in $sizes {
    @include media($size) {
        @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 ) );
            %#{$size}__col--#{$i} {
                width: percentage($target/$context)!important;
            }
            .#{$size}__col--#{$i} {
                @extend %#{$size}__col--#{$i};
            }
        }
        .#{$size}__alpha {
            float: left;
            margin-left: 0;
        }

        .#{$size}__omega {
            float: right;
            margin-left: 0;
            margin-right: 0;
        }
    }   
}

And this glorious beast outputs this:

.grid__container {
  width: 100%; }

.grid__col--1 {
  width: 6.25% !important; }

.grid__col--2 {
  width: 12.5% !important; }

.grid__col--3 {
  width: 18.75% !important; }

.grid__col--4 {
  width: 25% !important; }

.grid__col--5 {
  width: 31.25% !important; }

.grid__col--6 {
  width: 37.5% !important; }

.grid__col--7 {
  width: 43.75% !important; }

.grid__col--8 {
  width: 50% !important; }

.grid__col--9 {
  width: 56.25% !important; }

.grid__col--10 {
  width: 62.5% !important; }

.grid__col--11 {
  width: 68.75% !important; }

.grid__col--12 {
  width: 75% !important; }

.grid__col--13 {
  width: 81.25% !important; }

.grid__col--14 {
  width: 87.5% !important; }

.grid__col--15 {
  width: 93.75% !important; }

.grid__col--16 {
  width: 100% !important; }

.alpha {
  float: left;
  margin-left: 0; }

.omega {
  float: right;
  margin-left: 0;
  margin-right: 0; }

@media screen and (min-width: 1px) and (max-width: 479px) {
  .xsmall__col--1 {
    width: 6.25% !important; }

  .xsmall__col--2 {
    width: 12.5% !important; }

  .xsmall__col--3 {
    width: 18.75% !important; }

  .xsmall__col--4 {
    width: 25% !important; }

  .xsmall__col--5 {
    width: 31.25% !important; }

  .xsmall__col--6 {
    width: 37.5% !important; }

  .xsmall__col--7 {
    width: 43.75% !important; }

  .xsmall__col--8 {
    width: 50% !important; }

  .xsmall__col--9 {
    width: 56.25% !important; }

  .xsmall__col--10 {
    width: 62.5% !important; }

  .xsmall__col--11 {
    width: 68.75% !important; }

  .xsmall__col--12 {
    width: 75% !important; }

  .xsmall__col--13 {
    width: 81.25% !important; }

  .xsmall__col--14 {
    width: 87.5% !important; }

  .xsmall__col--15 {
    width: 93.75% !important; }

  .xsmall__col--16 {
    width: 100% !important; }

  .xsmall__alpha {
    float: left;
    margin-left: 0; }

  .xsmall__omega {
    float: right;
    margin-left: 0;
    margin-right: 0; } }
@media screen and (min-width: 480px) and (max-width: 767px) {
  .small__col--1 {
    width: 6.25% !important; }

  .small__col--2 {
    width: 12.5% !important; }

  .small__col--3 {
    width: 18.75% !important; }

  .small__col--4 {
    width: 25% !important; }

  .small__col--5 {
    width: 31.25% !important; }

  .small__col--6 {
    width: 37.5% !important; }

  .small__col--7 {
    width: 43.75% !important; }

  .small__col--8 {
    width: 50% !important; }

  .small__col--9 {
    width: 56.25% !important; }

  .small__col--10 {
    width: 62.5% !important; }

  .small__col--11 {
    width: 68.75% !important; }

  .small__col--12 {
    width: 75% !important; }

  .small__col--13 {
    width: 81.25% !important; }

  .small__col--14 {
    width: 87.5% !important; }

  .small__col--15 {
    width: 93.75% !important; }

  .small__col--16 {
    width: 100% !important; }

  .small__alpha {
    float: left;
    margin-left: 0; }

  .small__omega {
    float: right;
    margin-left: 0;
    margin-right: 0; } }
@media screen and (min-width: 768px) and (max-width: 959px) {
  .medium__col--1 {
    width: 6.25% !important; }

  .medium__col--2 {
    width: 12.5% !important; }

  .medium__col--3 {
    width: 18.75% !important; }

  .medium__col--4 {
    width: 25% !important; }

  .medium__col--5 {
    width: 31.25% !important; }

  .medium__col--6 {
    width: 37.5% !important; }

  .medium__col--7 {
    width: 43.75% !important; }

  .medium__col--8 {
    width: 50% !important; }

  .medium__col--9 {
    width: 56.25% !important; }

  .medium__col--10 {
    width: 62.5% !important; }

  .medium__col--11 {
    width: 68.75% !important; }

  .medium__col--12 {
    width: 75% !important; }

  .medium__col--13 {
    width: 81.25% !important; }

  .medium__col--14 {
    width: 87.5% !important; }

  .medium__col--15 {
    width: 93.75% !important; }

  .medium__col--16 {
    width: 100% !important; }

  .medium__alpha {
    float: left;
    margin-left: 0; }

  .medium__omega {
    float: right;
    margin-left: 0;
    margin-right: 0; } }
@media screen and (min-width: 960px) and (max-width: 1079px) {
  .large__col--1 {
    width: 6.25% !important; }

  .large__col--2 {
    width: 12.5% !important; }

  .large__col--3 {
    width: 18.75% !important; }

  .large__col--4 {
    width: 25% !important; }

  .large__col--5 {
    width: 31.25% !important; }

  .large__col--6 {
    width: 37.5% !important; }

  .large__col--7 {
    width: 43.75% !important; }

  .large__col--8 {
    width: 50% !important; }

  .large__col--9 {
    width: 56.25% !important; }

  .large__col--10 {
    width: 62.5% !important; }

  .large__col--11 {
    width: 68.75% !important; }

  .large__col--12 {
    width: 75% !important; }

  .large__col--13 {
    width: 81.25% !important; }

  .large__col--14 {
    width: 87.5% !important; }

  .large__col--15 {
    width: 93.75% !important; }

  .large__col--16 {
    width: 100% !important; }

  .large__alpha {
    float: left;
    margin-left: 0; }

  .large__omega {
    float: right;
    margin-left: 0;
    margin-right: 0; } }
@media screen and (min-width: 1080px) {
  .xlarge__col--1 {
    width: 6.25% !important; }

  .xlarge__col--2 {
    width: 12.5% !important; }

  .xlarge__col--3 {
    width: 18.75% !important; }

  .xlarge__col--4 {
    width: 25% !important; }

  .xlarge__col--5 {
    width: 31.25% !important; }

  .xlarge__col--6 {
    width: 37.5% !important; }

  .xlarge__col--7 {
    width: 43.75% !important; }

  .xlarge__col--8 {
    width: 50% !important; }

  .xlarge__col--9 {
    width: 56.25% !important; }

  .xlarge__col--10 {
    width: 62.5% !important; }

  .xlarge__col--11 {
    width: 68.75% !important; }

  .xlarge__col--12 {
    width: 75% !important; }

  .xlarge__col--13 {
    width: 81.25% !important; }

  .xlarge__col--14 {
    width: 87.5% !important; }

  .xlarge__col--15 {
    width: 93.75% !important; }

  .xlarge__col--16 {
    width: 100% !important; }

  .xlarge__alpha {
    float: left;
    margin-left: 0; }

  .xlarge__omega {
    float: right;
    margin-left: 0;
    margin-right: 0; } }

Let me know what you think and feel free to use this for your own projects :)

The Alpha and Omega additional classes can be deemed unnecessary, but I think they also have their uses :)