Custom grids let you design page layouts exactly how you want. They give you control over spacing, columns, and alignment.
Why custom grids offer control in SASS
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
SASS
@mixin custom-grid($columns, $gap) { display: grid; grid-template-columns: repeat($columns, 1fr); gap: $gap; }
This mixin creates a grid with a set number of columns and gap size.
You can reuse it with different values to make many grid layouts.
Examples
SASS
@include custom-grid(3, 1rem);
SASS
@include custom-grid(4, 2rem);
SASS
@include custom-grid(2, 0.5rem);
Sample Program
This example uses a custom grid mixin to create a 3-column layout with equal spacing. Each item is styled for clear visibility and accessibility roles are added.
SASS
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Custom Grid Example</title> <style> /* Sass mixins cannot be used directly in <style> tags; this is for demonstration only. */ /* In practice, compile Sass to CSS before using in HTML. */ </style> </head> <body> <section class="grid-container" role="grid" aria-label="Sample custom grid"> <div class="grid-item" role="gridcell">Item 1</div> <div class="grid-item" role="gridcell">Item 2</div> <div class="grid-item" role="gridcell">Item 3</div> </section> </body> </html>
Important Notes
Custom grids let you change columns and gaps easily without changing HTML.
Using mixins keeps your code clean and reusable.
Always add roles and labels for accessibility when using grids.
Summary
Custom grids give you full control over layout columns and spacing.
They help create flexible, reusable designs that adapt well to different screens.
Using Sass mixins makes it easy to apply and change grid settings.
Practice
1. Why do custom grids in Sass give you more control over your webpage layout?
easy
Solution
Step 1: Understand what custom grids do
Custom grids let you set specific column widths and gaps, unlike fixed grids.Step 2: Recognize the benefit of control
This control helps you create layouts that fit your design needs exactly.Final Answer:
Because you can define exact column sizes and spacing easily -> Option AQuick Check:
Custom grids = precise layout control [OK]
Hint: Custom grids let you set sizes and gaps yourself [OK]
Common Mistakes:
- Thinking custom grids fix browser bugs automatically
- Believing custom grids remove all CSS needs
- Assuming custom grids force uniform element sizes
2. Which Sass syntax correctly defines a mixin for a custom grid with 12 columns?
easy
Solution
Step 1: Identify correct mixin syntax
Mixins use @mixin with parameters and CSS inside curly braces.Step 2: Check grid-template-columns usage
Using repeat($columns, 1fr) sets equal columns, correct for grids.Final Answer:
@mixin grid($columns) { display: grid; grid-template-columns: repeat($columns, 1fr); } -> Option DQuick Check:
@mixin + repeat() = correct grid mixin [OK]
Hint: Mixins start with @mixin and include CSS rules inside braces [OK]
Common Mistakes:
- Using @function instead of @mixin for CSS blocks
- Trying to include mixin with parameters incorrectly
- Using 'columns' property which is not for grid layout
3. Given this Sass mixin and usage:
What will be the visual layout of elements inside
@mixin custom-grid($cols) {
display: grid;
grid-template-columns: repeat($cols, 1fr);
gap: 1rem;
}
.container {
@include custom-grid(3);
}What will be the visual layout of elements inside
.container?medium
Solution
Step 1: Analyze the mixin properties
display: grid sets grid layout; grid-template-columns: repeat(3, 1fr) creates 3 equal columns.Step 2: Understand gap property
gap: 1rem adds space of 1rem between columns and rows.Final Answer:
Three equal columns with 1rem gap between them -> Option CQuick Check:
repeat(3, 1fr) + gap:1rem = 3 equal spaced columns [OK]
Hint: repeat(n, 1fr) means n equal columns [OK]
Common Mistakes:
- Confusing gap size units
- Thinking grid creates flexbox layout
- Assuming columns have different widths
4. Identify the error in this Sass code for a custom grid mixin:
@mixin grid($cols) {
display: grid;
grid-template-columns: repeat($cols 1fr);
gap: 1rem;
}medium
Solution
Step 1: Check repeat() syntax
repeat() requires a comma between the count and size: repeat($cols, 1fr).Step 2: Verify other properties
display: grid and gap: 1rem are correct and valid.Final Answer:
Missing comma between $cols and 1fr in repeat() -> Option AQuick Check:
repeat() needs comma between arguments [OK]
Hint: repeat() arguments must be comma-separated [OK]
Common Mistakes:
- Omitting comma inside repeat()
- Confusing grid with flex display
- Changing gap units unnecessarily
5. You want a responsive custom grid in Sass that changes from 4 columns on large screens to 2 columns on small screens. Which approach gives you the best control?
hard
Solution
Step 1: Understand responsive design needs
Layouts must adapt to screen size, so columns should change with media queries.Step 2: Use mixin with parameters and media queries
Mixins let you reuse code and media queries inside them adjust columns dynamically.Final Answer:
Use a mixin with a parameter for columns and add media queries inside it to adjust columns based on screen width -> Option BQuick Check:
Mixin + media queries = flexible responsive grid [OK]
Hint: Combine mixins with media queries for responsive grids [OK]
Common Mistakes:
- Using fixed columns without responsiveness
- Ignoring media queries for screen sizes
- Using floats which are outdated for grids
