Flexbox is a flexible box layout. It gives us the power to create responsive layouts that can automatically arrange depending on the screen size.
It is based on a model that follows two directions, the main axis, that goes from the left to right, and the cross axis that goes from the top to the bottom.
Since flexbox is a whole module and not a single property, it involves a lot of things including its whole set of properties. Some of them are meant to be set on the container (parent element, known as “flex container”) whereas the others are meant to be set on the children (said “flex items”).
If “regular” layout is based on both block and inline flow directions, the flex layout is based on “flex-flow directions”. Please have a look at this figure from the specification, explaining the main idea behind the flex layout.
Flexbox Basic Axis Information
Having trouble looking at the image? Click here to take a closer look
Properties for the Parent (flex container)
Example:
Here we see a flex container with 3 items inside of it.
Flex container
Flex item
Cross Axis ↕
Main Axis ↔
In order to define a flex container, we just need to add the property of "display: flex;" to the target's CSS:
<!-- html -->
<div class="my-container">
ㅤㅤ<div>1</div>
ㅤㅤ<div>2</div>
ㅤㅤ<div>3</div>
</div>
/* CSS */
.my-container {
display: flex; /* or inline-flex */
}
And just like that, we have a flex container, which will inherit flex context for all of its direct children.
Flex Direction
By default, whenever we define a flex container its direction will be set to "row", which has its main axis fit the items from left to right, and its cross axis from the top to the bottom. However, we can change that with the "flex-direction" property:
Flex Direction
Cross Axis ↕
Main Axis ↔
(You can click the buttons to change the property)
/* CSS */
.my-container {
flex-direction: row;
}
Although you may have already noticed, it's important to mention that by changing the "flex-direction" property to column or column-reverse, the main axis and the cross axis will switch places, therefore the main axis will go from the top to the bottom, and the cross axis from left to right (unless it's reversed, of course).
Flex Wrap
By default, flex items will try to fit onto one line. You can change that and allow the items to wrap as needed with the "flex-wrap" property.
In this container, each flex item has the same size as in the previous example, however, since all of them are in the same line (or div) inside a flex container, they shrink to fit into the container's size, this happens because the "flex-wrap" default property is "nowrap". As you can see, once we change the flex-wrap property either to "wrap" or "wrap-reverse", the items turn back to their original size and move following the container's direction.
Flex Wrap
/* CSS */
.my-container {
flex-wrap: nowrap;
}
The flex-wrap properties are:
- nowrap (default): all flex items will be on one line
- wrap: flex items will wrap onto multiple lines, from top to bottom.
- wrap-reverse: flex items will wrap onto multiple lines from bottom to top.
Flex Flow
This is just a shorthand for the previous properties "flex-direction" and "flex-flow", it merges them so you can define both properties at once, instead of doing it separately. Since flex-direction's default value is "row" and flex-wrap's is "nowrap", flex-flow follows the same convention, having its default value set as "row nowrap".
/* So instead of doing this */
/* CSS */
.my-container {
flex-direction: column; /* or row/row-reverse/column-reverse */
flex-wrap: wrap; /* or wrap-reverse/nowrap */
}
/* You could do this */
/* CSS */
.my-container {
flex-flow: column wrap; /* or whatever combination you want */
}
And that's about it, just a way to do more with less, pretty simple, right?
Justify Content
This defines the alignment along the main axis. It helps distribute extra free space leftover when either all the flex items on a line are inflexible, or are flexible but have reached their maximum size. It also exerts some control over the alignment of items when they overflow the line.
Justify Content
/* CSS */
.my-container {
justify-content: flex-start;
}
The justify-content properties are:
- flex-start (default): items are packed toward the start of the flex-direction.
- flex-end: items are packed toward the end of the flex-direction.
- center: items are centered along the line
- space-between: items are evenly distributed in the line; first item is on the start line, last item on the end line
- space-around: items are evenly distributed in the line with equal space around them. Note that visually the spaces aren't equal, since all the items have equal space on both sides. The first item will have one unit of space against the container edge, but two units of space between the next item because that next item has its own spacing that applies.
- space-evenly: items are distributed so that the spacing between any two items (and the space to the edges) is equal.
Justify Content Extra Notes and Properties
Some other properties for justify-content are:
- start: items are packed toward the start of the writing-mode direction.
- end: items are packed toward the end of the writing-mode direction.
- left: items are packed toward left edge of the container, unless that doesn't make sense with the flex-direction, then it behaves like start.
- right: items are packed toward right edge of the container, unless that doesn’t make sense with the flex-direction, then it behaves like end.
Note that that browser support for these values is nuanced. For example, space-between never got support from some versions of Edge, and start/end/left/right aren't in Chrome yet. MDN has detailed charts. The safest values are flex-start, flex-end, and center.
There are also two additional keywords you can pair with these values: safe and unsafe. Using safe ensures that however you do this type of positioning, you can't push an element such that it renders off-screen (e.g. off the top) in such a way the content can't be scrolled too (called “data loss”).
Align Items
This defines the default behavior for how flex items are laid out along the cross axis on the current line. Think of it as the justify-content version for the cross-axis (perpendicular to the main-axis).
In this case, I've added a red underline so you can spot the baseline easier and find the differences between the "baseline" and "center" properties.
Align Items
/* CSS */
.my-container {
align-items: stretch;
}
The align-items properties are:
- stretch (default): stretch to fill the container (still respect min-width/max-width)
- flex-start / start / self-start: items are placed at the start of the cross axis. The difference between these is subtle, and is about respecting the flex-direction rules or the writing-mode rules.
- flex-end / end / self-end: items are placed at the end of the cross axis. The difference again is subtle and is about respecting flex-direction rules vs. writing-mode rules.
- center: items are centered in the cross-axis
- baseline: items are aligned such as their baselines align
The safe and unsafe modifier keywords can be used in conjunction with all the rest of these keywords (although note browser support), and deal with helping you prevent aligning elements such that the content becomes inaccessible.
Align Content
The align-content property aligns a flex container's lines within when there is extra space in the cross-axis, similar to how justify-content aligns individual items within the main-axis.
Note: This property only takes effect on multi-line flexible containers, where flex-wrap is set to either wrap or wrap-reverse). A single-line flexible container (i.e. where flex-wrap is set to its default value, no-wrap) will not reflect align-content.
Align Content
/* CSS */
.my-container {
flex-wrap: wrap;
align-content: flex-start;
}
The align-content properties are:
- normal (default): items are packed in their default position as if no value was set.
- flex-start / start: items packed to the start of the container. The (more supported) flex-start honors the flex-direction while start honors the writing-mode direction.
- flex-end / end: items packed to the end of the container. The (more support) flex-end honors the flex-direction while end honors the writing-mode direction.
- center: items centered in the container
- space-between: items evenly distributed; the first line is at the start of the container while the last one is at the end
- space-around: items evenly distributed with equal space around each line
- space-evenly: items are evenly distributed with equal space around them
- stretch: lines stretch to take up the remaining space
The safe and unsafe modifier keywords can be used in conjunction with all the rest of these keywords (although note browser support), and deal with helping you prevent aligning elements such that the content becomes inaccessible.
Gap, Row Gap and Column Gap
The gap property explicitly controls the space between flex items. It applies that spacing only between items not on the outer edges.
There are many ways in which you can define the gap properties:
/* gap */
/* CSS */
.my-container {
gap: 10px; /* When written like this, you are defining both the row and column gap */
gap: 10px 20px; /* Or you can define them separately, the first value for the row and the second for the column */
}
/* row-gap, column-gap */
/* CSS */
.my-container {
row-gap: 10px; /* You can also define the properties like this */
column-gap: 20px; /* in case you prefer being more specific */
}
And here we see a container with some flex items, you can try increasing/decreasing the gaps to see how they look.
Gap, Row Gap and Column Gap
/* CSS */
.my-container {
row-gap: 2px;
column-gap: 2px;
/* Or */
gap: 2px 2px;
}
The behavior could be thought of as a minimum gutter, as if the gutter is bigger somehow (because of something like justify-content: space-between;) then the gap will only take effect if that space would end up smaller. It is not exclusively for flexbox, gap works in grid and multi-column layout as well.
And that pretty much sums up the basics of what you should know about flex containers. There are, however, some other properties that you should know about, and these are the ones meant for the children or flex items, let's get into them.
Properties for the Children (flex items)
The following properties belong to the flex items, which means that they will apply to the children, and not to the parent/flex container.
Order
By default, flex items are laid out in the source order. However, the order property controls the order in which they appear in the flex container.
For this example, I have created a container with 4 flex items inside of it. There are 3 common items and another one that has been colored green to make it easier to spot, I've called it "the dude". Each flex item has its "order" value set as 0, which is the default value, therefore, the items will be placed as defined inside the container.
<!-- html -->
<div class="my-container">
ㅤㅤ<div>1</div>
ㅤㅤ<div id="the-dude">The dude</div>
ㅤㅤ<div>3</div>
ㅤㅤ<div>3</div>
</div>
/* CSS */
#the-dude {
order: 0; /* The default value is always 0 for the order property */
}
But what happens if we change the dude's order property?
Order
/* CSS */
#the-dude {
order: 0;
}
In this case, since the rest of the items have their order value set as 0 by default, the dude gets placed last in the container, because its value is bigger than the rest. When we select the "0" value, it gets back to its place since items with the same order value revert to their source order.
Flex Grow
This defines the ability for a flex item to grow if necessary. It accepts a unitless value that serves as a proportion. It dictates what amount of the available space inside the flex container the item should take up.
If all items have flex-grow set to 1, the remaining space in the container will be distributed equally to all children. If one of the children has a value of 2, that child would take up twice as much of the space either one of the others (or it will try, at least).
Flex Grow
/* CSS */
#the-dude {
width: auto;
flex-grow: 1;
}
Note: Negative numbers are invalid.
Flex Shrink
This defines the ability for a flex item to shrink if necessary.
In this case you can see that each item has a width of 200px (set with flex basis), and when we modify the container's width, the item with the most flex-shrink (The green item, the dude) is the one that shrinks the most. You can modify the container's width with the buttons over the code container.
Negative numbers are also invalid with this one.
Flex Shrink
/* CSS */
#The-dude {
flex-basis: 200px;
flex-shrink: 2;
}
.normal-flex-item {
flex-basis: 200px;
flex-shrink: 1;
}
But, what is flex basis?
Flex Basis
This defines the default size of an element before the remaining space is distributed. It can be a length (e.g. 20%, 5rem, etc.) or a keyword. The auto keyword means “look at my width or height property” (which was temporarily done by the main-size keyword until deprecated). The content keyword means “size it based on the item’s content” – this keyword isn’t well supported yet, so it’s hard to test and harder to know what its brethren max-content, min-content, and fit-content do.
/* CSS */
.flex-item {
flex-basis: | auto; /* default auto */
}
If set to 0, the extra space around content isn’t factored in. If set to auto, the extra space is distributed based on its flex-grow value. See this graphic.
Flex
This is the shorthand for flex-grow, flex-shrink and flex-basis combined. The second and third parameters (flex-shrink and flex-basis) are optional. The default is 0 1 auto, but if you set it with a single number value, like flex: 5;, that changes the flex-basis to 0%, so it’s like setting flex-grow: 5; flex-shrink: 1; flex-basis: 0%;.
/* CSS */
.flex-item {
flex: 0 1 auto; /* default values for flex-grow, flex-shrink and flex-basis */
}
It is recommended that you use this shorthand property rather than set the individual properties. The shorthand sets the other values intelligently.
Align Self
This allows the default alignment (or the one specified by align-items) to be overridden for individual flex items.
Please see the align-items explanation to understand the available values.
Align Self
/* CSS */
.my-container {
align-items: center;
}
#The-dude {
align-self: center;
}
Note that float, clear and vertical-align have no effect on a flex item.
And those are the basics of flexbox. Even though I didn't cover all of the flexbox's magic tricks in this guide, there's a lot you can already do with what you've seen here, it's all practice from this point. It may take you some time to get a good grasp of all the concepts, but I hope that this can help you at least a little in your journey, good luck!.