← Back to CSS Course | Chapter 12: Responsive Design | Lesson 4 of 8

CSS RWD Grid View

A responsive grid view is like a set of adjustable shelves -- the same shelving unit can hold four narrow columns on a wide wall or collapse down to one column on a narrow one, without you having to build a different shelf for each wall.
Syntax
css
* {
  box-sizing: border-box;
}
.column {
  width: percentage;
  float: left;
}

What a Responsive Grid View Is

A responsive grid view arranges content into a set number of fluid columns that reflow -- stacking, wrapping, or resizing -- as the viewport changes, rather than a rigid pixel grid that stays the same width regardless of screen size.

Example: What a Responsive Grid View Is

css
<style>
.row {
  display: flex;
  flex-wrap: wrap;
}
.row div {
  background: steelblue;
  color: white;
  padding: 10px;
  margin: 4px;
}
</style>
<div class="row">
  <div>Column</div>
  <div>Column</div>
  <div>Column</div>
</div>

Percentage Based Columns

The classic approach divides a row into percentage-width columns (e.g. three columns at 33.33% each) using float or inline-block, so the columns automatically shrink together as the container shrinks, though they still need a breakpoint to stack on very small screens.

Note:
  • Accepted values:
  • width: 25% — four columns
  • width: 33.33% — three columns
  • width: 50% — two columns
  • width: 100% — one column
  • box-sizing: border-box — keeps padding inside the width

Example: Percentage Based Columns

css
<style>
.row {
  overflow: hidden;
}
.col {
  float: left;
  width: 33.33%;
  box-sizing: border-box;
  padding: 10px;
  color: white;
}
.col:nth-child(1) { background: coral; }
.col:nth-child(2) { background: seagreen; }
.col:nth-child(3) { background: steelblue; }
</style>
<div class="row">
  <div class="col">33.33%</div>
  <div class="col">33.33%</div>
  <div class="col">33.33%</div>
</div>

Flexbox Based Grid View

A flex container with flex-wrap: wrap and items given a flex-basis percentage or fixed minimum width produces a self-adjusting grid where columns wrap onto new rows automatically once they no longer fit, without any explicit breakpoint math.

Note:
  • Accepted values:
  • display: flex — makes a row
  • flex-wrap: wrap — allows wrapping
  • flex: 1 1 <basis> — flexible columns
  • gap: <length> — gutters

Example: Flexbox Based Grid View

css
<style>
.row {
  display: flex;
  flex-wrap: wrap;
}
.col {
  flex-basis: 200px;
  background: steelblue;
  color: white;
  padding: 10px;
  margin: 4px;
}
</style>
<div class="row">
  <div class="col">Wraps automatically</div>
  <div class="col" style="background: seagreen;">Wraps automatically</div>
</div>

CSS Grid Based Grid View

grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)) builds a grid view that fits as many 200px-minimum columns as the row allows and stretches them evenly to fill remaining space -- often needing zero media queries at all.

Note:
  • Accepted values:
  • display: grid — makes a grid
  • grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)) — responsive columns
  • gap: <length> — gutters
  • grid-column: span <n> — column spans

Example: CSS Grid Based Grid View

css
<style>
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 8px;
}
.grid div {
  background: steelblue;
  color: white;
  padding: 12px;
}
</style>
<div class="grid">
  <div>Card</div>
  <div style="background: seagreen;">Card</div>
</div>

Choosing Between the Approaches

Percentage floats are largely legacy at this point; flexbox suits single-row or single-column flows like card grids and toolbars, while CSS Grid suits genuinely two-dimensional layouts (rows AND columns both need explicit control) like a full page shell or dashboard.

Example: Choosing Between the Approaches

css
<style>
.cards {
  display: flex;
  flex-wrap: wrap;
}
.cards div {
  background: steelblue;
  color: white;
  padding: 10px;
  margin: 4px;
}
.dashboard {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 10px;
}
.dashboard div {
  background: seagreen;
  color: white;
  padding: 10px;
}
</style>
<div class="cards">
  <div>Card row: Flexbox</div>
</div>
<div class="dashboard">
  <div>Two-dimensional shell: Grid</div>
</div>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using widths that add up to more than 100% once padding and border are included, so columns wrap; use box-sizing: border-box.
  2. Forgetting to clear floats in a float based grid, so the parent collapses.
  3. Using fixed pixel column widths instead of percentages or fr units.
Browser Support

Supported in all modern browsers: Chrome, Firefox, Safari, Edge and Opera.

🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.