<!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 (No Media Queries)</title>
<style>
/* Reset and base styles for full responsiveness */
* {
box-sizing: border-box;
}
html, body {
margin: 0;
padding: 0;
height: 100%;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}
body {
padding: 2rem;
background-color: #f5f5f5;
line-height: 1.6;
}
/* The responsive grid container */
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
grid-gap: 2rem;
max-width: 1200px; /* Optional: caps extreme wide screens */
margin: 0 auto;
padding-bottom: 2rem;
}
/* Grid items (cards) */
.grid-item {
background: white;
border-radius: 12px;
padding: 2rem;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.grid-item:hover {
transform: translateY(-4px);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}
.grid-item h3 {
margin: 0 0 1rem;
color: #333;
font-size: 1.25rem;
}
.grid-item p {
margin: 0 0 1rem;
color: #666;
}
/* Demo content variety */
.grid-item:nth-child(odd) { border-left: 5px solid #4CAF50; }
.grid-item:nth-child(even) { border-left: 5px solid #2196F3; }
</style>
</head>
<body>
<div class="grid-container">
<div class="grid-item">
<h3>Card 1</h3>
<p>This grid automatically adapts: 1 column on mobile (<300px), 2 on tablet (300-600px), 3+ on desktop. No media queries needed!</p>
</div>
<div class="grid-item">
<h3>Card 2</h3>
<p>Uses <code>repeat(auto-fit, minmax(300px, 1fr))</code> for fluid columns that wrap based on container width.</p>
</div>
<div class="grid-item">
<h3>Card 3</h3>
<p>Items expand to fill available space while respecting the minimum width.</p>
</div>
<div class="grid-item">
<h3>Card 4</h3>
<p>Perfect for galleries, dashboards, or product grids.</p>
</div>
<div class="grid-item">
<h3>Card 5</h3>
<p>Hover effects and shadows for polish.</p>
</div>
<div class="grid-item">
<h3>Card 6</h3>
<p>Scales seamlessly across all devices.</p>
</div>
<div class="grid-item">
<h3>Card 7</h3>
<p>Extra card to demonstrate more columns on wide screens.</p>
</div>
<div class="grid-item">
<h3>Card 8</h3>
<p>Grid gap ensures consistent spacing.</p>
</div>
</div>
</body>
</html>
How It Works (No Media Queries Required)
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)):
auto-fit: Creates as many columns as fit the container, collapsing empty tracks.
minmax(300px, 1fr): Each column is at least 300px wide but expands flexibly (1fr) to fill space.
- Responsive Behavior:
| Screen Width | Columns |
|---|
| < 300px | 1 |
| 300-600px | 2 |
| 600-900px | 3 |
| 900px+ | 4+ |
- Full viewport adaptation: Viewport meta tag + full-width container ensures mobile-first scaling.
- Customizable: Tweak
minmax(250px, 1fr) for smaller items or add auto-fill for fixed tracks.
Copy-paste into an HTML file and resize your browser to test! 🚀