DEV Community

Cover image for How to Build Flexible Web Page Designs with CSS3 Flexbox
Raj Aryan
Raj Aryan

Posted on

How to Build Flexible Web Page Designs with CSS3 Flexbox

CSS has been capable of elegant, flexible layouts for years. Yet somehow, a shocking number of websites still limp along using tables, floats, and other relics from the archaeological layers of the web. CSS3 Flexbox exists precisely to end this suffering.

Flexbox is one of the most practical, production-ready layout systems we have. It is simple, expressive, responsive by default, and designed for modern interfaces where content size changes constantly. If you care about clean layouts, maintainable CSS, and your own sanity, Flexbox deserves a permanent place in your toolkit.

This article walks through what Flexbox is, why it matters, and how to use it effectively to build flexible, real-world web page designs.


Why Flexbox Still Matters

CSS3 has been around long enough that browser support is no longer a serious excuse. Flexbox is supported by the vast majority of browsers, and even older ones degrade gracefully if your HTML is written properly.

Flexbox solves a problem that traditional CSS struggled with for years:

  • Aligning items horizontally and vertically
  • Distributing available space intelligently
  • Reordering content without touching the HTML
  • Adapting layouts as content grows or shrinks

Instead of micromanaging positions with floats or absolute values, Flexbox lets you define rules and let the browser do the math.

That is the entire value proposition.


What Is CSS3 Flexbox?

Flexbox is a layout model that allows elements inside a container to automatically adjust their size and position to best fill the available space.

You define:

  • A flex container
  • flex items inside it

Then you control how those items:

  • Flow horizontally or vertically
  • Wrap to new lines
  • Grow or shrink relative to each other
  • Align along both axes

The browser handles the rest.


Creating a Flex Container

Any element can become a flex container. The most common choices are semantic HTML elements such as:

  • <main>
  • <section>
  • <article>
  • <ul> or <ol>
  • <div> when no better option exists

To activate Flexbox:

.container {
  display: flex;
}
Enter fullscreen mode Exit fullscreen mode

That single line already changes how child elements behave.

If you want an inline container:

.container {
  display: inline-flex;
}
Enter fullscreen mode Exit fullscreen mode

Controlling Layout Direction and Wrapping

Flexbox layouts are built around two axes:

  • Main axis
  • Cross axis

You control the main axis with flex-direction:

.container {
  flex-direction: row;          /* default */
  /* row-reverse | column | column-reverse */
}
Enter fullscreen mode Exit fullscreen mode

To allow items to wrap onto multiple lines:

.container {
  flex-wrap: wrap;
}
Enter fullscreen mode Exit fullscreen mode

Most developers use the shorthand:

.container {
  flex-flow: row wrap;
}
Enter fullscreen mode Exit fullscreen mode

This single rule defines direction and wrapping together.


Aligning Items Inside the Container

Flexbox gives you precise alignment control without hacks.

Horizontal Alignment (Main Axis)

Use justify-content:

.container {
  justify-content: space-between;
}
Enter fullscreen mode Exit fullscreen mode

Common values:

  • flex-start
  • flex-end
  • center
  • space-between
  • space-around

Vertical Alignment (Cross Axis)

Use align-items:

.container {
  align-items: center;
}
Enter fullscreen mode Exit fullscreen mode

Common values:

  • flex-start
  • flex-end
  • center
  • baseline
  • stretch (default)

When items wrap onto multiple lines, use align-content instead.


Styling Flex Items

Any direct child of a flex container becomes a flex item.

Flex items can be simple elements or complex structures containing images, text, and controls.

Example:

<main class="container">
  <figure>
    <img src="img1.png">
    <figcaption>Item 1</figcaption>
  </figure>
  <figure>
    <img src="img2.png">
    <figcaption>Item 2</figcaption>
  </figure>
</main>
Enter fullscreen mode Exit fullscreen mode

Here, each <figure> is a flex item, regardless of its internal complexity.


Controlling Item Size and Behavior

Flexbox provides three core properties:

.item {
  flex-grow: 1;
  flex-shrink: 1;
  flex-basis: 200px;
}
Enter fullscreen mode Exit fullscreen mode

These define:

  • How much the item grows when space is available
  • How much it shrinks when space is limited
  • Its starting size before space is distributed

Most developers use the shorthand:

.item {
  flex: 1 1 200px;
}
Enter fullscreen mode Exit fullscreen mode

You can also control item order visually without changing HTML:

.item {
  order: 2;
}
Enter fullscreen mode Exit fullscreen mode

Lower numbers appear first.


A Simple Flexbox Example

<ul class="container">
  <li class="item">1</li>
  <li class="item">2</li>
  <li class="item">3</li>
</ul>
Enter fullscreen mode Exit fullscreen mode
.container {
  display: flex;
  background: #004f7f;
  height: 500px;
}

.item {
  width: 200px;
  height: 200px;
  margin: auto;
  list-style: none;
  background: white;
}
Enter fullscreen mode Exit fullscreen mode

With just a few lines of CSS, the layout becomes centered, flexible, and readable.


Building a Responsive Page Layout with Flexbox

Flexbox shines when combined with media queries.

A single layout can adapt from:

  • One column on small screens
  • Two columns on tablets
  • Three columns on desktops

By adjusting flex, order, and flex-flow at breakpoints, you get a responsive layout without rewriting your HTML.

This is exactly how modern navigation bars, dashboards, and content grids are built.


Why Flexbox Is Still Underused

The reasons are mostly historical:

  • Early browser support concerns
  • Habitual reliance on floats and tables
  • Lack of understanding of the Flexbox mental model

None of those hold up anymore.

Flexbox is stable, powerful, and widely supported. Avoiding it today is not caution. It is technical debt.


Summary

CSS3 Flexbox gives you:

  • Cleaner layouts
  • Less brittle CSS
  • Built-in responsiveness
  • Predictable alignment without hacks

It works for small components and full-page layouts alike. Once you understand the container-and-items model, Flexbox becomes one of the most intuitive tools in CSS.

If you want flexible web pages that adapt gracefully to real content and real devices, Flexbox is not optional. It is baseline.


Reference
Browser support data sourced from CanIUse.com

Top comments (0)