For a long time, the only tools available for creating CSS layouts were features like floats and positioning.

However, some layout designs were difficult to achieve with these tools, like:

Vertically centering a block of content inside of its parent.Making all the children of a container take up an equal amount of the available width or height.Making all columns adopt the same height even if they contain a different amount of content.

As you’ll see in this post, flexbox makes a lot of these layout tasks much easier.

Display: Flex: The Switch

Okay, imagine you have a container <div> with some elements inside:

<div class=”container”>
<div class=”item”>1</div>
<div class=”item”>2</div>
<div class=”item”>3</div>
</div>

To activate the Flexbox layout, all you need to do is add display: flex to your container div.

.container {
display: flex;
}

And with that, you just created two invisible axes:

Main Axis: This is where your elements will primarily align themselves. (Think horizontal rows by default)Cross Axis: This is the secondary direction, which is at 90 degrees to the main axis. (Think vertical columns by default)

Flex Direction: Changing the Flow

There’s another property for changing the flow of these axes — flex-direction and by default, this is set to row, which means that items will flow horizontally from left to right.

But if you want your elements to be stacked vertically, you can set the flex-direction property to column.

.container {
display: flex;
flex-direction: column;
}

But hey, Flexbox is flexible. You can even reverse the order on either axis:

column-reverse: Items flow vertically from bottom to top.row-reverse: Items flow horizontally from right to left.

Justify Content: Controlling the Main Axis

To align those elements on the main axis, we usejustify-content property.

By default, this is set to flex-start and items huddle together at the beginning of the main axis.

.container {
display: flex;
flex-direction: row;
justify-content: flex-start;
}

Here are the potential values that you can utilize for this property:

flex-end: Items are arranged at the end of the main axis.center: Items gather politely in the center.space-between: Items spread out evenly, with the first item touching the start and the last item touching the end.space-around: Similar to space-between, but with equal space around each item (including the edges).space-evenly: Each item gets the exact same amount of space on all sides.

Align Items: Controlling the Cross Axis

To control the cross-axis, we use the align-items property.

The default value of align-items is stretch which makes items stretch to fill the container on the cross-axis.

.container {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: stretch;
}flex-start: Items align to the start of the cross-axis.flex-end: Items align to the end of the cross-axis.center: Items align to the center of the cross-axis.baseline: Items align their text baselines.

Key Point — Remember the Axes: The most important thing to grasp is that justify-content controls the main axis, and align-items controls the cross-axis. Once that clicks, Flexbox becomes a breeze!

The Gap Property

With Flexbox layout, there’s no need to manually add margins to your elements. The gap property controls the spacing between items, applying instant spacing between everything.

.container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
gap: 30px;
}

Flex Wrap: Avoiding the Crush

By default, flex items cram into one line. But you can use flex-wrap: wrap to let them gracefully flow onto new lines.

.container {
display: flex;
flex-wrap: wrap;
}nowrap (default): All items stay on one line.wrap: Items wrap onto multiple lines if needed.wrap-reverse: Items wrap onto multiple lines in reverse order.

Align Content: Control the spacing of the wrapped lines

If you set flex-wrap to wrap, you unlock a new property — align-content which lets you control the spacing of those wrapped lines.

It works just like justify-content, but for multiple lines:

flex-start: Lines pack towards the start.flex-end: Lines pack towards the end.center: Lines center themselves.space-between: Lines spread out with space between.space-around: Lines spread out with space around.stretch (default): Lines stretch to fill the container.

Flexbox Properties for Individual Items

Flexbox doesn’t just stop at the container. We can style individual items too:

You can use the align-self tool for overriding the align-items setting on the container, but just for a specific item.

Let’s say you want one element to be centered while others are at the top.

.item:nth-child(3) {
align-self: center;
}

Other possible values are:

auto (default): Inherits the align-items value from the parent container.flex-start: Aligns to the start of the cross-axis.flex-end: Aligns to the end of the cross-axis.center: Centers the item on the cross-axis.baseline: Aligns the item’s baseline with other baselines.stretch: Stretches the item to fill the cross-axis (if height isn’t set).

Flex Grow: Willingness to Grow

Think of this as an item’s eagerness to grow. It controls how much extra space an item should take up compared to its siblings. A higher number means it will grab more of that extra space.

For example:

.item:nth-child(1) {
flex-grow: 0; // default value
}
.item:nth-child(2) {
flex-grow: 1;
}
.item:nth-child(3) {
flex-grow: 2;
}

In this case, the 1st item won’t grow at all. The 2nd item will take up an equal share of available space. And the 3rd item will take up twice as much available space as the 2nd item.

Flex Shrink: Willingness to Shrink

This is like an item’s willingness to shrink. It controls how much an item will shrink when there’s not enough space. A higher number means it will shrink more readily.

flex-shrink: 1 (default): The item will shrink proportionally if needed.flex-shrink: 0: The item refuses to shrink, even if it causes overflow.

For instance, we can set the first and last items to refuse to shrink by:

.item:nth-child(1) {
flex-shrink: 0;
}
.item:nth-child(6) {
flex-shrink: 0;
}

Flex Basis

This sets the initial size of an item before extra space is distributed. Think of it as the item’s starting point.

.item:nth-child(4) {
flex-basis: 50%;
}

It can be a specific length (like 200px or 30%) or keywords like auto (use the item’s content size) or content (use the content’s minimum size).

Bonus Tip: The Flex Shorthand

Instead of writing flex-grow, flex-shrink, and flex-basis separately, you can use the super convenient flex shorthand property.

With the shorthand, you can achieve the same effect with:

.item:nth-child(4) {
flex: 1 0 0;
}

The order of the values matters in the shorthand. In this case, it sets flex-grow to 1, flex-shrink to 0, and flex-basis to 0.

Order

And finally, we have the order property, which changes the visual order of the items. It takes a number, and lower numbers appear first.

.item:nth-child(1) {
order: 1;
}
.item:nth-child(6) {
order: -1;
}

But if you rely solely on order to change the logical order it can affect accessibility. That’s because screen readers and keyboard navigation follow the source code order.

Here’s a Free Flexbox Cheatsheet for you.

If you found this post helpful, be sure to check out my Free HTML & CSS Crash Course.

Learn CSS Flexbox, the easy way was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.

​ Level Up Coding – Medium

about Infinite Loop Digital

We support businesses by identifying requirements and helping clients integrate AI seamlessly into their operations.

Gartner
Gartner Digital Workplace Summit Generative Al

GenAI sessions:

  • 4 Use Cases for Generative AI and ChatGPT in the Digital Workplace
  • How the Power of Generative AI Will Transform Knowledge Management
  • The Perils and Promises of Microsoft 365 Copilot
  • How to Be the Generative AI Champion Your CIO and Organization Need
  • How to Shift Organizational Culture Today to Embrace Generative AI Tomorrow
  • Mitigate the Risks of Generative AI by Enhancing Your Information Governance
  • Cultivate Essential Skills for Collaborating With Artificial Intelligence
  • Ask the Expert: Microsoft 365 Copilot
  • Generative AI Across Digital Workplace Markets
10 – 11 June 2024

London, U.K.