HTML•CSS Grid
CSS Grid Layout: grid-template-columns and Responsive Grids
CSS Grid creates two-dimensional row-and-column grids that adapt automatically to screen widths.
Syntax
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 15px; }
Parameters & Values
Key components and acceptable values for this structure:
| Parameter | Type | Description |
|---|---|---|
| repeat(auto-fit, minmax()) | Grid Formula | Creates fully responsive columns without media queries. |
Ready-to-Run Code Example
Executable in CompilraRun this complete, working example directly without installing HTML locally:
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; padding: 20px; }
.feature-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 15px;
}
.grid-card {
background: #f1f5f9;
border: 1px solid #cbd5e1;
border-radius: 8px;
padding: 15px;
text-align: center;
}
</style>
</head>
<body>
<h2>Responsive CSS Grid System</h2>
<div class="feature-grid">
<div class="grid-card"><strong>Python</strong><p>Version 3.12</p></div>
<div class="grid-card"><strong>JavaScript</strong><p>Node.js 22</p></div>
<div class="grid-card"><strong>Rust</strong><p>Cargo & Rustc</p></div>
</div>
</body>
</html>Expected Output:
[Rendered Responsive 3-Column Feature Cards Grid Adjusting to Viewport Width]
How It Works: Step-by-Step
- Lines 6-10: display: grid creates auto-fitting columns with minmax(180px, 1fr).
- Lines 20-24: Grid items snap into symmetric responsive columns.
Key Concepts
- CSS Grid manages both columns and rows simultaneously.
- minmax(180px, 1fr) ensures cards never shrink below 180px, wrapping automatically.
Common Mistake: Hardcoding fixed pixel columns like grid-template-columns: 200px 200px 200px
Fixed pixel columns overflow on mobile screens. Use 1fr and minmax() for responsiveness.
❌ Incorrect
grid-template-columns: 300px 300px; // Breaks on mobile screens!
✅ Correct
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
Related HTML Tutorials & Compiler
CSS Flexbox: flex-direction, justify-content, and align-items
Design responsive 1-dimensional layouts with CSS Flexbox.
HTML Tabular Dataset Matrices: Structural Presentation Layouts with Table Semantics
Structure tabular dataset matrices using semantic table elements (<table>, <thead>, <tbody>, <tfoot>, <tr>, <th>, <td>) with column alignments and metrics formatting.
DOM Manipulation: querySelector, textContent, and innerHTML
Select and manipulate HTML elements using document.querySelector and textContent.
HTML Online Compiler
Open in Compilra IDE
Run and test HTML on Compilra
Execute this code snippet with pre-supplied standard input (stdin) in Compilra’s cloud compiler.