CSS Grid creates two-dimensional layouts with rows and columns. Set display: grid; on the parent.
Define columns
.grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 16px; }
fr means a fraction of the free space. repeat(3, 1fr) is the same as 1fr 1fr 1fr.
Responsive without media queries
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); fits as many 200px+ columns as the screen allows.
Spanning
grid-column: span 2; makes an item two columns wide.
Named areas
Use grid-template-areas to draw the layout with names like "header header" "sidebar main".
Examples
Example — Page layout with grid areas
HTML
<style>
.page { display: grid; grid-template-columns: 150px 1fr; grid-template-areas: "header header" "side main" "footer footer"; gap: 8px; }
.page > div { padding: 14px; color: white; }
.h { grid-area: header; background: #1f386b; }
.s { grid-area: side; background: #4a90e2; }
.m { grid-area: main; background: seagreen; min-height: 120px; }
.f { grid-area: footer; background: #555; }
</style>
<div class="page">
<div class="h">Header</div>
<div class="s">Sidebar</div>
<div class="m">Main content</div>
<div class="f">Footer</div>
</div>
Example — Responsive gallery
HTML
<style>
.gallery { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; }
.gallery div { background: #ffd8a8; height: 90px; border-radius: 6px; display: flex; align-items: center; justify-content: center; }
</style>
<div class="gallery">
<div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div>
</div>