Skip to main content

Responsive Flexbox

Media queries let a flexbox layout change shape across screen sizes and devices.

Take a design with three columns on large screens and one column on small screens. One approach switches flex-direction from row to column once a chosen breakpoint is reached.

The other approach keeps the direction and alters the percentage given to the flex property on each flex item. For that to work, the flex container also needs flex-wrap: wrap;.

Flex direction change

At a screen width of 600px or less, flex-direction goes from row to column.

Prompt
Create a page with a three-column flexbox layout using flex-direction: row. Add a media query at max-width 600px that changes flex-direction to column, making it a one-column layout. Use three colored boxes with text.
Output: complete HTML page

Flex percentage change

Once the screen is no wider than 600px, the flex property jumps from 33.3% to 100%.

Prompt
Create a page with a flex container using flex-wrap: wrap. Each flex item has flex: 33.3% to make three columns. Add a media query at max-width 600px that changes each item to flex: 100% for a single-column layout.
Output: complete HTML page

Responsive Image Gallery using Flexbox

Combine flexbox with media queries and an image gallery can change its number of columns to suit the screen size.

Example

Prompt
Create a responsive image gallery using flexbox and media queries. Show four images per row on large screens, two per row on medium screens (max-width 800px), and one per row on small screens (max-width 600px). Use placeholder images with descriptive text below each.
Output: complete HTML page

Responsive Website using Flexbox

A whole responsive website layout is also possible with flexbox, with both the navigation bar and the content area staying flexible.

Example

Prompt
Create a responsive website layout using flexbox. Include a top navigation bar with links, a main content area with a sidebar, and a footer. Use media queries to stack the navigation links vertically and place the sidebar below the content on screens smaller than 600px.
Output: complete HTML page