The flexible box layout (or flexbox) makes it easier to build a responsive and flexible layout for your page, something that has historically been either difficult or hacky in CSS. The layout is great for when you are not sure of the size of the items inside, hence the layout is flexible enough to handle dynamic sizes.
It gives the overall container the ability to adjust the size of the containing items to display them in the most efficient and readable way possible. The flexible box layout is a combination of the flexbox and grid layout.
Flex box layout
To create a flexible box layout, you need to use the flexbox property. Give a container a display: flex property.
Now we can apply any of the follow CSS properties to the parent element to determine how to display its children elements.
flex-direction
flex-wrap
flex-flow
justify-content
align-items
align-content
Let's go over each property in detail and learn about each one's effect.
Flex Direction
Flex direction determines in what orientation and what direction the items inside will be placed. There are two options for the orientation, row or column, and two options for the direction, normal or reverse:
row
row-reverse
column
column-reverse
Row
Using the same HTML and styles as above, here is how the items will render when we apply the different values for flex-direction:
With row-reverse, the items, while still displayed in a row, are now being displayed in the reverse order as the HTML would suggest. This is useful when you want to display the items in a column, but you want to display them in the reverse order.
By default, the container will try to fit all the children items on the same line. You can change this behavior with flex-wrap. Below are the following values for it:
wrap
no-wrap
wrap-reverse
Wrap
CSS
.container {
display: flex;
flex-wrap: wrap;
}
Flex Wrap set to Wrap
Since we set it to wrap, the last item, D, was rendered on the next line, wrapping when it couldn't fit at the end.
No-Wrap
CSS
.container {
display: flex;
flex-wrap: no-wrap;
}
Flex Wrap set to No Wrap
To fit the items on one line and not wrap, the items were squeezed together.
The justify-content value of space-around ensures that the space around the items are equal. Notice that the first and last item have less space on the edges than between the items.
The align-items property defines how the items are positioned within the row or column. It's similar to justify-content but perpendicular to the line it's being aligned on. To illustrate the following values, we'll use this HTML and CSS:
The align-items value of stretch will just make every element as tall (if a row) or wide (if a column) as the tallest/widest element. In our case, we manually set the heights of every box, but if we remove all the heights, we get this:
The align-items value of baseline aligns all the baselines of the items. Because the baselines are all the same because the content in them is just a single letter, they look aligned like flex-start in our example.
The align-content property defines the spacing between lines if they wrap. To properly demonstrate this property, we'll need more than 4 boxes, so let's do 12 boxes:
Because A has an order of 1, it got rendered last, since 1 is greater than 0. Items are rendered in numerical order, and you are allowed negative numbers if you'd like. This is a powerful way to precisely control the order of items inside your row or column.
Flex Grow
The flex-grow property defines how items in a row or column grow relative to each other. By default, the value is 0, which basically means that the items themselves determine their own width. By setting every item to a flex-grow of 1, every item will take up the same amount of space since they are equal, relative to one another.
However, watch what happens when we give 1 item a flex-grow of 2, while leaving the rest at a flex-grow of 1:
As you would expect, the middle box, with it's flex-grow of 2, maintained twice as large of a width as the other boxes. If you resized your browser, it would remain twice as large.
flex-grow is an awesome way to control the sizes of the items relative to one another to ensure that each item is given the space they need.
Flex Shrink
The opposite of flex-grow is flex-shrink. As you would imagine, this property controls how an item will shrink relative to other items. The default value is 1 and the bigger the value, the faster it will shrink.
Let's have one box have a flex-shrink of 0 and another with 2:
Using flex-shrink to change how fast an item shrinks.
The box with the letter B in it shrunk slower than the rest because it had a smaller value for flex-shrink, whereas the box with the letter D in it shrunk faster because it had a larger value for flex-shrink.
Flex Basis
The flex-basis CSS property is the flex way of defining an element's width or height. Remember that you can have a row or a column. With flex-basis it will apply the length provided as a width if in a row, or a height if in a column. There is otherwise no difference between flex-basis and width or height.
Let's show the use of flex-basis in this simple example:
Using flex-basis to give an item in a row a width.
Align Self
The align-self property allows individual flex items to override the alignment specified by align-items. For example, you could have an instance where every item is aligned with flex-end but one item is aligned center:
Using align-self to give an item it's own align-items value.
We let the box with the letter C in it align itself center whereas the rest of the items were aligned flex-end. This technique allows individual flex items to behave and render differently from the rest of the items.