A responsive grid helps your website look good on all screen sizes by adjusting layout automatically.
Responsive grid with breakpoints in SASS
Start learning this pattern below
Jump into concepts and practice - no test required
@mixin breakpoint($point) { @if $point == small { @media (max-width: 599px) { @content; } } @else if $point == medium { @media (min-width: 600px) and (max-width: 899px) { @content; } } @else if $point == large { @media (min-width: 900px) { @content; } } } .grid { display: grid; grid-template-columns: repeat(1, 1fr); @include breakpoint(medium) { grid-template-columns: repeat(2, 1fr); } @include breakpoint(large) { grid-template-columns: repeat(4, 1fr); } }
The @mixin breakpoint defines screen size rules.
Use @include breakpoint(name) to apply styles inside that screen size.
@mixin breakpoint($point) { @if $point == small { @media (max-width: 599px) { @content; } } }
.container { display: grid; grid-template-columns: repeat(1, 1fr); @include breakpoint(medium) { grid-template-columns: repeat(2, 1fr); } }
.container { display: grid; grid-template-columns: repeat(1, 1fr); @include breakpoint(medium) { grid-template-columns: repeat(2, 1fr); } @include breakpoint(large) { grid-template-columns: repeat(4, 1fr); } }
This example creates a grid with 1 column on small screens, 2 columns on medium screens, and 4 columns on large screens. Each grid item is a blue box with white text. The layout changes automatically when you resize the browser.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Responsive Grid Example</title> <style lang="scss"> $breakpoints: ( small: 599px, medium: 899px ); @mixin breakpoint($point) { @if $point == small { @media (max-width: map-get($breakpoints, small)) { @content; } } @else if $point == medium { @media (min-width: #{map-get($breakpoints, small) + 1px}) and (max-width: map-get($breakpoints, medium)) { @content; } } @else if $point == large { @media (min-width: #{map-get($breakpoints, medium) + 1px}) { @content; } } } .grid { display: grid; gap: 1rem; grid-template-columns: repeat(1, 1fr); padding: 1rem; @include breakpoint(medium) { grid-template-columns: repeat(2, 1fr); } @include breakpoint(large) { grid-template-columns: repeat(4, 1fr); } } .item { background-color: #4a90e2; color: white; padding: 1rem; border-radius: 0.5rem; text-align: center; font-weight: bold; font-size: 1.25rem; } </style> </head> <body> <main> <section class="grid" aria-label="Responsive grid example"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> <div class="item">5</div> <div class="item">6</div> </section> </main> </body> </html>
Use gap in grid to add space between items instead of margins.
Always test your grid by resizing the browser or using device simulation in browser DevTools.
Use semantic HTML and ARIA labels for accessibility, like aria-label on the grid container.
A responsive grid adjusts columns based on screen size using breakpoints.
Use Sass mixins to write clean, reusable media queries for breakpoints.
Test responsiveness by resizing your browser or using DevTools device mode.
Practice
Solution
Step 1: Understand breakpoints in responsive design
Breakpoints are screen widths where layout changes to fit device size better.Step 2: Role of breakpoints in grids
They adjust the number of columns so content fits nicely on different screens.Final Answer:
To change the number of columns based on screen size -> Option DQuick Check:
Breakpoints adjust layout = B [OK]
- Thinking breakpoints only change colors
- Believing breakpoints disable grids
- Assuming breakpoints only affect fonts
Solution
Step 1: Understand mixin with parameter
Mixin should accept a size parameter to be reusable for any breakpoint.Step 2: Correct media query syntax
Use min-width for breakpoints that apply styles at or above the size.Final Answer:
@mixin breakpoint($size) { @media (min-width: $size) { @content; } } -> Option AQuick Check:
Mixin with parameter and min-width = A [OK]
- Forgetting to add parameter for size
- Using max-width instead of min-width incorrectly
- Not including @content inside media query
@mixin breakpoint($size) {
@media (min-width: $size) {
@content;
}
}
.grid {
display: grid;
grid-template-columns: repeat(1, 1fr);
@include breakpoint(600px) {
grid-template-columns: repeat(2, 1fr);
}
@include breakpoint(900px) {
grid-template-columns: repeat(3, 1fr);
}
}Solution
Step 1: Check screen width against breakpoints
Screen is 800px wide, which is >= 600px but < 900px.Step 2: Determine applied grid-template-columns
At 600px breakpoint, columns become 2; 900px breakpoint not reached yet.Final Answer:
2 columns -> Option AQuick Check:
800px between 600 and 900 = 2 columns [OK]
- Choosing 3 columns thinking 800px is above 900px
- Choosing 1 column ignoring breakpoint overrides
- Assuming grid disables at certain widths
@mixin breakpoint($size) {
@media min-width: $size {
@content;
}
}
.grid {
display: grid;
grid-template-columns: repeat(1, 1fr);
@include breakpoint(768px) {
grid-template-columns: repeat(2, 1fr);
}
}Solution
Step 1: Check media query syntax in mixin
Media queries require parentheses around conditions, e.g., (min-width: 768px).Step 2: Identify missing parentheses
Code has @media min-width: $size without parentheses, causing syntax error.Final Answer:
Missing parentheses around media query condition -> Option BQuick Check:
Media query needs parentheses = C [OK]
- Omitting parentheses in @media
- Confusing mixin name with media query
- Thinking grid-template-columns syntax is wrong
Solution
Step 1: Understand breakpoint directions
Use min-width to apply styles at or above the breakpoint size.Step 2: Check column counts for each breakpoint
Default 1 column, 2 columns at ≥600px, 4 columns at ≥1200px matches @mixin breakpoint($size) { @media (min-width: $size) { @content; } } .grid { display: grid; grid-template-columns: repeat(1, 1fr); @include breakpoint(600px) { grid-template-columns: repeat(2, 1fr); } @include breakpoint(1200px) { grid-template-columns: repeat(4, 1fr); } }.Final Answer:
@mixin breakpoint($size) { @media (min-width: $size) { @content; } } .grid { display: grid; grid-template-columns: repeat(1, 1fr); @include breakpoint(600px) { grid-template-columns: repeat(2, 1fr); } @include breakpoint(1200px) { grid-template-columns: repeat(4, 1fr); } } -> Option CQuick Check:
min-width with increasing columns = A [OK]
- Using max-width instead of min-width
- Setting default columns incorrectly
- Reversing column counts at breakpoints
