Skip to content

Instantly share code, notes, and snippets.

@wiegertschouten
Created October 1, 2020 18:38
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save wiegertschouten/443f7e061d1fc656b229ac4b5b0faece to your computer and use it in GitHub Desktop.
Save wiegertschouten/443f7e061d1fc656b229ac4b5b0faece to your computer and use it in GitHub Desktop.
A very simple grid system built with SASS and CSS grid
$columns: 12;
$gap: 30px;
$breakpoints: (
xs: 480px,
sm: 768px,
md: 960px,
lg: 1170px,
xl: 1280px
);
@mixin create-selectors($breakpoint: null) {
$infix: if($breakpoint == null, '', '-#{$breakpoint}');
@for $i from 1 through $columns {
.col#{$infix}-#{$i} {
grid-column-end: span $i;
}
.col-offset#{$infix}-#{$i} {
grid-column-start: $i + 1;
}
.row#{$infix}-#{$i} {
grid-row-end: span $i;
}
.row-offset#{$infix}-#{$i} {
grid-row-start: $i + 1;
}
}
}
.grid {
display: grid;
grid-template-columns: repeat($columns, 1fr);
grid-gap: $gap;
gap: $gap;
}
@include create-selectors;
@each $breakpoint, $width in $breakpoints {
@media (min-width: $width) {
@include create-selectors($breakpoint);
}
}
@nguyenvu
Copy link

Thank you for sharing, do you have any idea how to implement row with this grid?

@wiegertschouten
Copy link
Author

@nguyenvu you can create a row like this:

<div class="grid">
    <div class="col-12 col-md-8">I'm a column 2/3 wide</div>
    <div class="col-12 col-md-4">I'm a column 1/3 wide</div>
</div>

In this way, you create a row of which the children stack on top of one another on mobile devices, and become columns on desktop computers.

Does that help?

@nguyenvu
Copy link

Thank you! I keep playing with this and trying to figure out to use .row-l-* but yes, it helps a lot :D

@arsors
Copy link

arsors commented May 31, 2022

Very simple and good idea. But unfortunately at a browser width below 337px (Smallest iPhone: 320px) the window becomes scrollable, because the content does not shrink. It is related to the gap. Unfortunately I still have too little knowledge to fix this grid problem. @wiegertschouten do you have an idea or someone else?

This is my current markup:

<div class="grid">
    <div class="col-12">I'm a column</div>
</div>

@wiegertschouten
Copy link
Author

@arsors You are right, it's related to the gap. That is a bit of a shortcoming of this idea. You can solve it by adding the following after the initial code:

@media (max-width: 400px) {
    .grid {
        grid-gap: 20px;
        gap: 20px;
    }
}

This makes the gap somewhat smaller on devices below 400px.

@arsors
Copy link

arsors commented Jun 2, 2022

@wiegertschouten Thank you for the answer. It seems to be a general problem with grids and gaps. I have already solved it like your idea. Alternatively you could use: gap: min($gap, 5%);, but this results in a strange spacing.

Edit: After a bit of testing i found out that gap: clamp(15px, 2vw, $gap); is working pretty ok (not perfect).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment