Bird
Raised Fist0
SASSmarkup~10 mins

Grid system mixin from scratch in SASS - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a simple grid container with flex display.

SASS
@mixin grid-container() {
  display: [1];
  flex-wrap: wrap;
}
Drag options to blanks, or click blank then click option'
Ablock
Bflex
Cgrid
Dinline
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'block' instead of 'flex' will not create a flexible grid.
Using 'inline' will not allow wrapping of grid items.
2fill in blank
medium

Complete the mixin to set the width of a grid column based on a fraction of total columns.

SASS
@mixin grid-column($span, $total-columns) {
  width: ([1] / $total-columns) * 100%;
}
Drag options to blanks, or click blank then click option'
A$span
B$total-columns
C$span * $total-columns
D100
Attempts:
3 left
💡 Hint
Common Mistakes
Using $total-columns in numerator instead of $span.
Multiplying instead of dividing will give incorrect width.
3fill in blank
hard

Fix the error in the mixin to add gutter space between grid columns using margin.

SASS
@mixin grid-column($span, $total-columns, $gutter) {
  width: calc( ( $span / $total-columns ) * 100% - [1] );
  margin-right: $gutter;
}
Drag options to blanks, or click blank then click option'
Acalc(#{$gutter})
B$gutter * 2
Ccalc($gutter)
D$gutter
Attempts:
3 left
💡 Hint
Common Mistakes
Using $gutter directly inside calc() without interpolation causes errors.
Not using calc() at all will cause invalid CSS.
4fill in blank
hard

Fill both blanks to create a responsive grid column that adjusts width on smaller screens.

SASS
@mixin responsive-column($span, $total-columns, $gutter) {
  width: calc( ( $span / $total-columns ) * 100% - [1] );
  margin-right: $gutter;

  @media (max-width: [2]) {
    width: 100%;
    margin-right: 0;
  }
}
Drag options to blanks, or click blank then click option'
A#{$gutter}
B10rem
C5rem
D100%
Attempts:
3 left
💡 Hint
Common Mistakes
Not interpolating $gutter inside calc() causes invalid CSS.
Using 100% as max-width in media query is invalid.
5fill in blank
hard

Fill all three blanks to create a complete grid system mixin with container and column styles.

SASS
@mixin grid-system($total-columns, $gutter) {
  display: [1];
  flex-wrap: wrap;
  margin-left: calc(-1 * [2]);

  > * {
    padding-left: [3];
  }
}
Drag options to blanks, or click blank then click option'
Aflex
B$gutter
Ccalc($gutter / 2)
Dblock
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'block' instead of 'flex' breaks the flex layout.
Not using half gutter values causes uneven spacing.

Practice

(1/5)
1. What is the main purpose of a grid system mixin in Sass?
easy
A. To create flexible column layouts with reusable code
B. To add colors to text elements
C. To write JavaScript functions inside Sass
D. To load external fonts automatically

Solution

  1. Step 1: Understand the role of mixins in Sass

    Mixins let you reuse code blocks with parameters to customize styles.
  2. Step 2: Identify what a grid system mixin does

    A grid system mixin helps create column layouts that adapt easily by changing parameters.
  3. Final Answer:

    To create flexible column layouts with reusable code -> Option A
  4. Quick Check:

    Grid mixin = flexible columns [OK]
Hint: Grid mixins simplify layout by reusing column code [OK]
Common Mistakes:
  • Confusing mixins with variables
  • Thinking mixins add colors only
  • Assuming mixins run JavaScript
2. Which of the following is the correct way to define a simple grid mixin with columns and gap parameters in Sass?
easy
A. @mixin grid($columns, $gap) { display: flex; columns: $columns; gap: $gap; }
B. @mixin grid($columns, $gap) { display: block; grid-columns: $columns; grid-gap: $gap; }
C. @mixin grid($columns, $gap) { display: grid; grid-template-columns: repeat($columns, 1fr); gap: $gap; }
D. @mixin grid($columns, $gap) { display: grid; columns: $columns; gap: $gap; }

Solution

  1. Step 1: Check the display property for grid layout

    Grid layouts require display: grid;, not flex or block.
  2. Step 2: Verify grid-template-columns and gap syntax

    Using grid-template-columns: repeat($columns, 1fr); sets equal columns, and gap: $gap; sets spacing.
  3. Final Answer:

    @mixin grid($columns, $gap) { display: grid; grid-template-columns: repeat($columns, 1fr); gap: $gap; } -> Option C
  4. Quick Check:

    Grid needs display:grid and repeat() [OK]
Hint: Use display:grid and repeat() for columns [OK]
Common Mistakes:
  • Using display:flex instead of grid
  • Using incorrect property names like columns
  • Missing repeat() function for columns
3. Given this Sass mixin and usage:
@mixin grid($cols, $gap) {
  display: grid;
  grid-template-columns: repeat($cols, 1fr);
  gap: $gap;
}

.container {
  @include grid(3, 2rem);
}

What CSS will be generated for .container?
medium
A. .container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2rem; }
B. .container { display: flex; columns: 3; gap: 2rem; }
C. .container { display: grid; grid-template-columns: 3fr; gap: 2rem; }
D. .container { display: block; grid-template-columns: repeat(3, 1fr); gap: 2rem; }

Solution

  1. Step 1: Understand mixin parameters and usage

    The mixin sets grid display, columns with repeat, and gap using passed values 3 and 2rem.
  2. Step 2: Translate Sass mixin to CSS output

    Including the mixin with (3, 2rem) generates CSS with display: grid;, grid-template-columns: repeat(3, 1fr);, and gap: 2rem;.
  3. Final Answer:

    .container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2rem; } -> Option A
  4. Quick Check:

    Mixin params = CSS properties [OK]
Hint: Repeat columns with 1fr for equal width [OK]
Common Mistakes:
  • Confusing flex with grid display
  • Writing grid-template-columns: 3fr instead of repeat()
  • Using display:block by mistake
4. Identify the error in this Sass grid mixin:
@mixin grid($cols, $gap) {
  display: grid;
  grid-template-columns: repeat($cols 1fr);
  gap: $gap;
}
medium
A. gap property is invalid in grid layouts
B. Using display:flex instead of display:grid
C. Mixin parameters should be $columns and $gaps
D. Missing comma between $cols and 1fr in repeat() function

Solution

  1. Step 1: Check syntax of repeat() function

    The repeat() function requires a comma between the number and the size, like repeat($cols, 1fr).
  2. Step 2: Verify other properties

    Display is correctly set to grid, and gap is valid for grid layouts.
  3. Final Answer:

    Missing comma between $cols and 1fr in repeat() function -> Option D
  4. Quick Check:

    repeat() needs comma [OK]
Hint: Always put a comma inside repeat() between count and size [OK]
Common Mistakes:
  • Omitting comma in repeat()
  • Confusing display:flex with grid
  • Thinking gap is invalid in grid
5. You want a responsive grid mixin that changes columns based on screen width. Which Sass code correctly adds a media query inside the mixin to set 2 columns on screens smaller than 600px and 4 columns otherwise?
hard
A. @mixin responsive-grid($gap) { display: grid; grid-template-columns: repeat(2, 1fr); gap: $gap; @media (min-width: 600px) { grid-template-columns: repeat(4, 1fr); } }
B. @mixin responsive-grid($gap) { display: grid; grid-template-columns: repeat(4, 1fr); gap: $gap; @media (max-width: 600px) { grid-template-columns: repeat(2, 1fr); } }
C. @mixin responsive-grid($gap) { display: flex; gap: $gap; @media (max-width: 600px) { flex-direction: column; } }
D. @mixin responsive-grid($gap) { display: grid; grid-template-columns: repeat(4, 1fr); gap: $gap; @media (min-width: 600px) { grid-template-columns: repeat(2, 1fr); } }

Solution

  1. Step 1: Set default columns for large screens

    The mixin sets 4 columns by default using repeat(4, 1fr).
  2. Step 2: Add media query for smaller screens

    Inside @media (max-width: 600px), columns change to 2 with repeat(2, 1fr).
  3. Step 3: Confirm display and gap properties

    Display is grid and gap uses the parameter, which is correct.
  4. Final Answer:

    @mixin responsive-grid($gap) { display: grid; grid-template-columns: repeat(4, 1fr); gap: $gap; @media (max-width: 600px) { grid-template-columns: repeat(2, 1fr); } } -> Option B
  5. Quick Check:

    Default 4 cols, max-width 600px = 2 cols [OK]
Hint: Use max-width media query for smaller screens [OK]
Common Mistakes:
  • Using min-width instead of max-width for small screens
  • Setting flex display instead of grid
  • Reversing column counts in media query