Careeroza
Responsive Grids
auto-fit and minmax()
Example
Create as many columns as fit, each at least 140px wide, with no media queries. Resize the code runner to watch it adapt:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
.gallery { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }
.gallery img { width: 100%; height: 90px; object-fit: cover; border-radius: 6px; display: block; }
</style>
</head>
<body>
<div class="gallery">
<img src="https://picsum.photos/id/1011/300/200" alt="Boat">
<img src="https://picsum.photos/id/1012/300/200" alt="Road">
<img src="https://picsum.photos/id/1013/300/200" alt="Woman">
<img src="https://picsum.photos/id/1015/300/200" alt="River">
</div>
</body>
</html>Result
Grid vs Flexbox
| Flexbox | Grid | |
|---|---|---|
| Dimensions | one (row or column) | two (rows and columns) |
| Driven by | content size | the layout you define |
| Best for | navbars, toolbars, centering, small components | page layouts, galleries, dashboards |
Tip: They work well together: a Grid page layout with Flexbox inside each component.