Flexbox is the short name for the Flexible Box Layout Module, designed to make it easy for us to lay things out in one dimension — either as a row or as a column. To use flexbox, you apply display: flex
to the parent element of the elements you want to lay out; all its direct children then become flex items. We can see this in a simple example.
The HTML markup below gives us a containing element, with a class of wrapper
, inside which are three <div> elements. By default these would display as block elements, below one another, in our English language document.
However, if we add display: flex
to the parent, the three items now arrange themselves into columns. This is due to them becoming flex items and using some initial values that flexbox gives them. They are displayed as a row, because the initial value of flex-direction
is row
. They all appear to stretch to the height of the tallest item, because the initial value of the align-items
property is stretch
. This means that the items stretch to the height of the flex container, which in this case is defined by the tallest item. The items all line up at the start of the container, leaving any extra space at the end of the row.
.wrapper {
display: flex;
}
<div class="wrapper">
- <div class="box1">One</div>
- <div class="box2">Two</div>
- <div class="box3">Three</div>
</div>
In addition to the above properties that can be applied to the flex container, there are properties that can be applied to the flex items. These properties, among other things, can change the way that the items flex, enabling them to expand and contract to fit into the available space.
As a simple example of this, we can add the flex
property to all of our child items, with a value of 1
. This will cause all of the items to grow and fill the container, rather than leaving space at the end. If there is more space then the items will become wider; if there is less space they will become narrower. In addition, if you add another element to the markup the items will all become smaller to make space for it — they will adjust size to take up the same amount of space, whatever that is.
.wrapper {
}
.wrapper > div {
}
<div class="wrapper">
- <div class="box1">One</div>
- <div class="box2">Two</div>
- <div class="box3">Three</div>
</div>
Note: This has been a very short introduction to what is possible in Flexbox, to find out more, see our Flexbox article.