For a long time, the only reliable cross-browser compatible tools available for creating CSS layouts were features like floats and positioning. These work, but in some ways they’re also limiting and frustrating.
The following simple layout designs are either difficult or impossible to achieve with such tools in any kind of convenient, flexible way:
- Vertically centering a block of content inside its parent.
- Making all the children of a container take up an equal amount of the available width/height, regardless of how much width/height is available.
- Making all columns in a multiple-column layout adopt the same height even if they contain a different amount of content.
As you’ll see in subsequent sections, flexbox makes a lot of layout tasks much easier. Let’s dig in!
In this article, you’ll work through a series of exercises to help you understand how flexbox works. To get started, you should make a local copy of the first starter file — flexbox0.html from our GitHub repo. Load it in a modern browser (like Firefox or Chrome) and have a look at the code in your code editor. You can also see it live here
You’ll see that we have a
<header> element with a top level heading inside it and a
<section> element containing three
<article>s. We’re going to use these to create a fairly standard three column layout.
To start with, we need to select which elements are to be laid out as flexible boxes. To do this, we set a special value of
display on the parent element of the elements you want to affect. In this case we want to lay out the
<article> elements, so we set this on the
This causes the <section> element to become a flex container and its children to become flex items. The result of this should be something like so: