Skip to content

RWD Grid View

Responsive Web Design - Modern Layout Techniques

Section titled “Responsive Web Design - Modern Layout Techniques”

While older websites often relied on float-based grid systems (typically 12 columns using percentages), modern CSS offers far more powerful and flexible tools for creating responsive layouts: Flexbox and CSS Grid Layout.

These modern techniques simplify the process of arranging elements, controlling alignment, and adapting layouts across different screen sizes.

Before diving into layout methods, it’s crucial to ensure consistent box model calculations. Setting box-sizing: border-box; on elements makes their specified width and height include padding and borders, simplifying layout math.

It’s a standard best practice to apply this globally:

html {
box-sizing: border-box;
}
*, *::before, *::after {
box-sizing: inherit; /* Ensures all elements inherit from html */
}

Learn more in our CSS Box Model chapter.

CSS Grid is a two-dimensional layout system, designed for dividing a page into major regions or defining the relationship between parts of a control in terms of size, position, and layer. It allows you to create complex, responsive layouts with cleaner HTML and CSS.

Let’s create a container that behaves differently on small and large screens.

<!-- HTML Structure -->
<div class="grid-container">
<div class="grid-item header">Header</div>
<div class="grid-item sidebar">Sidebar</div>
<div class="grid-item main">Main Content</div>
<div class="grid-item footer">Footer</div>
</div>
/* CSS */
.grid-container {
display: grid;
gap: 10px; /* Space between grid items */
padding: 10px;
/* Mobile layout (default): Single column */
grid-template-columns: 1fr; /* Single flexible column */
grid-template-areas:
"header"
"sidebar"
"main"
"footer";
}
/* Assign items to named areas */
.header { grid-area: header; background-color: lightblue; }
.sidebar { grid-area: sidebar; background-color: lightpink; }
.main { grid-area: main; background-color: lightgreen; }
.footer { grid-area: footer; background-color: lightgray; }
/* Desktop layout: Apply styles for wider screens */
@media (min-width: 768px) {
.grid-container {
/* Two columns: sidebar 1fr, main content 3fr */
grid-template-columns: 1fr 3fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
}

In this example, display: grid turns the container into a grid. grid-template-columns defines column structure (using the flexible fr unit). grid-template-areas provides a visual way to arrange named grid items. Media queries (@media) allow changing the layout based on screen width.

CSS Grid offers much more control over placement, alignment, and spacing than older float-based methods.

Flexbox is a one-dimensional layout model optimized for arranging items in rows or columns. It excels at distributing space and aligning items within a container, making it ideal for components like navigation bars, form controls, or card layouts.

<!-- HTML Structure -->
<nav class="flex-nav">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Products</a>
<a href="#">Contact</a>
</nav>
/* CSS */
.flex-nav {
display: flex; /* Turn the nav into a flex container */
flex-direction: row; /* Arrange items in a row (default) */
justify-content: space-around; /* Distribute space around items */
background-color: #333;
padding: 10px;
}
.flex-nav a {
color: white;
text-decoration: none;
padding: 8px 15px;
}
.flex-nav a:hover {
background-color: #555;
}

display: flex makes the children (<a> tags) flex items. justify-content controls alignment along the main axis (the row, in this case). Flexbox provides powerful alignment (align-items) and ordering (order) capabilities.

Grid and Flexbox are not mutually exclusive; they often work together. A common pattern is to use Grid for the overall page layout (header, sidebar, main, footer) and Flexbox for the components within those areas (navigation links, card elements).

  • Use Grid when you need two-dimensional layout control (rows and columns simultaneously) or want to define explicit layout structures.
  • Use Flexbox when you need one-dimensional layout control (arranging items in a single row or column) and want powerful alignment and space distribution along that axis.

Previously, developers used CSS float properties and percentage widths to create grid-like layouts. This involved calculating column widths (e.g., 100% / 12 columns = 8.33% per column) and using techniques like ‘clearfix’ hacks to manage the floats.

While you might encounter float-based layouts in older codebases, Flexbox and Grid are the recommended modern approaches due to their superior capabilities, simpler syntax, and better handling of alignment and responsiveness.