React / 4 MIN READ
Responsive Grid
Sturdy and Responsive
From the original Fervor library. Examples may use older package versions.
Let’s go over how to create a responsive, single-column layout using CSS Grid first.
We’ll use CSS Grid and Media Queries to make this layout responsive. The layout will start as a single column and then, based on the width of the viewport, it can be adjusted.
Firstly, let’s create a simple HTML structure:
<div className="grid-container">
<div className="grid-item">Item 1</div>
<div className="grid-item">Item 2</div>
<div className="grid-item">Item 3</div>
<!-- Add more items as needed -->
</div>
Alright, now let’s modify our HTML structure to include an image, a title, and some text within each grid item. For this, we’re going to use a div for the card, and within it, we will include an img element for the image, an h2 for the title, and a p for the text.
Here’s the updated HTML structure:
<div className="grid-container">
<div className="grid-item">
<div className="card">
<img src="image1.jpg" alt="Image 1"/>
<h2>Title 1</h2>
<p>Some text about item 1...</p>
</div>
</div>
<div className="grid-item">
<div className="card">
<img src="image2.jpg" alt="Image 2">
<h2>Title 2</h2>
<p>Some text about item 2...</p>
</div>
</div>
<div className="grid-item">
<div className="card">
<img src="image3.jpg" alt="Image 3"/>
<h2>Title 3</h2>
<p>Some text about item 3...</p>
</div>
</div>
<!-- Add more items as needed -->
</div>
Now, let’s adjust our CSS to style the cards. We’ll add some styles for .card, .card img, .card h2, and .card p:
.grid-container {
display: grid;
grid-template-columns: 1fr;
gap: 20px; /* Define space between grid items */
padding: 20px; /* Some padding around the grid */
}
.grid-item {
/* No changes here */
}
.card {
background-color: #ddd; /* Just to visualize the card */
padding: 20px;
text-align: center;
border-radius: 8px; /* Optional: Round the corners of the card */
}
.card img {
width: 100%; /* Make the image expand to the width of the card */
height: auto; /* Let the image height adjust automatically */
}
.card h2 {
/* Add any desired styles for the title */
}
.card p {
/* Add any desired styles for the text */
}
Remember, you can replace "image1.jpg", "image2.jpg", "image3.jpg", etc., with the paths to your actual images, and the alt text, title, and paragraph text with your actual content.
And as before, include the media queries for responsiveness:
@media (min-width: 600px) {
.grid-container {
grid-template-columns: repeat(2, 1fr);
}
}
@media (min-width: 900px) {
.grid-container {
grid-template-columns: repeat(3, 1fr);
}
}
You now have a responsive single-column grid layout that contains cards, each with an image, a title, and some text.
Now, let’s style this using CSS:
.grid-container {
display: grid;
grid-template-columns: 1fr;
gap: 20px; /* Define space between grid items */
padding: 20px; /* Some padding around the grid */
}
.grid-item {
background-color: #ddd; /* Just to visualize the items */
padding: 20px;
text-align: center;
}
With the above CSS, we’ve created a single column layout. All items in the .grid-container will stack vertically to form a single column. The 1fr value for grid-template-columns means that all available space in the container will be distributed evenly among the columns (in this case, just one column).
Now, let’s make it responsive. For instance, we might want the layout to change to a two-column grid when the viewport is at least 600px wide:
@media (min-width: 600px) {
.grid-container {
grid-template-columns: repeat(2, 1fr);
}
}
And perhaps we want a three-column grid when the viewport is at least 900px wide:
@media (min-width: 900px) {
.grid-container {
grid-template-columns: repeat(3, 1fr);
}
}
The repeat(2, 1fr) value means that two columns will be created, each taking up an equal fraction of the available space.
With this, we have a single-column grid that becomes a two-column grid at 600px wide and a three-column grid at 900px wide.
Remember to include the viewport meta tag in the <head> of your HTML to ensure proper rendering and touch zooming:
<meta name="viewport" content="width=device-width, initial-scale=1">
This setup should give you a reactive, responsive single-column grid that adapts to the viewport’s width.