Responsive Three-Column Layout
This example demonstrates how to create a responsive three-column webpage layout using CSS Grid. The main content appears between two sidebars, providing a flexible structure for websites with multiple content areas.
Main Content
This is the primary content area of the webpage. It occupies twice as much available column space as either sidebar.
CSS Grid allows all three columns to appear next to each other while maintaining consistent spacing between them.
The layout automatically adjusts as the browser window changes size, allowing the content to remain readable on different devices.
Responsive Design: Resize the browser window to see how the three-column layout changes to two columns on medium screens and a single column on smaller screens.
How the Layout Works
The display: grid declaration creates a CSS Grid container that arranges its child elements into columns.
The grid-template-columns: 1fr 2fr 1fr declaration divides the available space into four fractional units. Each sidebar receives one unit, while the main content receives two.
The gap: 20px declaration creates consistent spacing between the columns and rows.
The min-width: 0 declaration allows each grid item to shrink within its assigned column when necessary.
Responsive Design
CSS media queries adjust the layout according to the viewport width.
At widths of 900 pixels or less, the layout changes to two columns. The left sidebar and main content remain next to each other, while the right sidebar moves below them and spans both columns.
At widths of 600 pixels or less, the layout changes to a single column. The main content appears first, followed by the left sidebar and right sidebar.
This approach prioritizes the main content on smaller screens while preserving access to the supporting information.
Customizing the Layout
You can change the column proportions by modifying the grid-template-columns declaration.
For example, the following CSS creates three equal-width columns:
grid-template-columns: 1fr 1fr 1fr;
Alternatively, 1fr 3fr 1fr creates a wider main content area with narrower sidebars.
You can also adjust the background colors, borders, padding, and spacing to match your website's design.
Modify the media query breakpoints when the content requires more or less space at different screen sizes.