Learn how to create fully responsive grid layouts using CSS Grid with this step-by-step guide. Includes beginner-friendly code examples and detailed explanations.
When building modern websites, a responsive layout is essential. One of the best tools for creating flexible and clean layouts is CSS Grid. It allows you to design in rows and columns with minimal code.
In this guide, you’ll learn how to use CSS Grid to build a responsive layout step-by-step, even if you’re a beginner.
CSS Grid is a layout system in CSS that allows you to arrange elements in rows and columns. It’s like building a spreadsheet with rows and columns where each cell can hold a piece of content.
Let’s build a simple, responsive 3-column grid layout that adjusts to different screen sizes.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive CSS Grid Layout</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="grid-container">
<div class="grid-item">Item 1</div>
<div class="grid-item">Item 2</div>
<div class="grid-item">Item 3</div>
<div class="grid-item">Item 4</div>
<div class="grid-item">Item 5</div>
<div class="grid-item">Item 6</div>
</div>
</body>
</html>
grid-container
is the parent div that will hold our grid items.
Each grid-item
is a box that we want to place inside the grid.
Now we’ll use CSS to turn that container into a responsive grid.
/* style.css */
body {
margin: 0;
padding: 20px;
font-family: Arial, sans-serif;
background-color: #f5f5f5;
}
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
max-width: 1200px;
margin: auto;
}
.grid-item {
background-color: #4CAF50;
color: white;
padding: 40px;
font-size: 1.2rem;
text-align: center;
border-radius: 8px;
}
display: grid;
This turns the container into a grid layout.
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
repeat(...)
: Automatically repeats columns.
auto-fit
: Automatically fits as many columns as possible.
minmax(250px, 1fr)
: Each column will be at least 250px, and can grow to fill available space (1fr
means one fraction of remaining space).
This makes the grid responsive — it adjusts based on the screen size.
gap: 20px;
Adds space between grid items.
max-width: 1200px;
and margin: auto;
Centers the grid and limits its width to keep it looking good on large screens.
Thanks to auto-fit
and minmax()
, the grid will:
Show 3 columns on large screens.
Switch to 2 columns on tablets.
Collapse to 1 column on mobile phones.
No need for media queries — it adapts automatically!
If you want more control, you can still use media queries:
@media (max-width: 600px) {
.grid-item {
font-size: 1rem;
padding: 20px;
}
}
This makes items more compact on smaller screens.
You now have a responsive grid that adjusts perfectly across devices. You can use it to build galleries, product lists, dashboards, or portfolios.
CSS Grid is a powerful and easy-to-use tool for creating layouts that work on all screen sizes. With just a few lines of CSS, you can build clean, responsive designs that scale beautifully.
Thank you for visiting! Enjoy exploring our diverse collection of blogs, crafted with passion and insight to inspire and inform. Happy reading!