A responsive grid helps your website look good on all screen sizes by adjusting layout automatically.
Responsive grid with breakpoints in SASS
@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.