8. repeat() Function
The
repeat() function reduces repetitive CSS.Instead of writing:
.container {
grid-template-columns: 1fr 1fr 1fr;
}You can write:
.container {
grid-template-columns: repeat(3, 1fr);
}Both create three equal-width columns.
9. minmax() Function
The
minmax() function defines a minimum and maximum size for a grid track..container {
display: grid;
grid-template-columns: repeat(3, minmax(150px, 1fr));
}Each column has a minimum size of 150px and can grow to share available space.
10. auto-fit and auto-fill
These keywords help create flexible grid layouts.
.container {
display: grid;
grid-template-columns:
repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
}•
auto-fit fits columns into available space and can expand existing columns.•
auto-fill creates as many tracks as fit, preserving empty tracks when space allows.These are especially useful for responsive card layouts.
11. Real-World Example: Product Grid
HTML:
<div class="products">
<div class="card">Laptop</div>
<div class="card">Mobile</div>
<div class="card">Headphones</div>
<div class="card">Keyboard</div>
</div>
CSS:
.products {
display: grid;
grid-template-columns:
repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
}
.card {
padding: 30px;
background-color: lightblue;
border-radius: 10px;
text-align: center;
}This creates a responsive product grid that adjusts the number of columns based on available space.
12. Flexbox vs CSS Grid
