Creating a Two-Column Layout

A two-column layout is one of the most common web design patterns, often used to display a narrow sidebar menu next to a wide main content area. We can easily create this using CSS Flexbox.

Historically, developers used complex CSS properties like float to achieve this. Today, using display: flex is the modern, robust, and easiest method.

The HTML Structure

First, wrap your two columns in a parent div container:

Using Flex Proportions

After we apply display: flex to the container, we use the flex property on the children to tell them how much of the available horizontal space they should inherit.

If we want the right column to be three times as wide as the left column, we can give the left column flex: 1 and the right column flex: 3:

Making it Responsive

Sidebars usually don't fit well on mobile devices. Using a media query, we can change the flex-direction to column when the screen size is small. This stacks the sidebar neatly on top of (or below) the main content:

Here, we use a breakpoint of 576px, which is a common breakpoint for mobile devices. However, you can change this value to whatever you need it to be. A common breakpoint for tablets is 768px and a common breakpoint for desktops is 1024px.

Full Working Example

Below is a fully styled two-column layout. Resize your browser window below 576 pixels to see the sidebar pop to the top, collapsing the layout into a mobile-friendly single column:

View Output Full Screen Preview

The above example uses a breakpoint of 576px so that you see the full effect when viewing in the (narrow) editor/preview window (after clicking the button to view the example). As mentioned, you can change this value to whatever you need it to be.