Flexbox - The Ultimate CSS Flex Cheatsheet (with animated diagrams!) (2024)

This comprehensive CSS flexbox cheatsheet will cover everything you need to know to start using flexbox in your web projects.

CSS flexbox layout allows you to easily format HTML. Flexbox makes it simple to align items vertically and horizontally using rows and columns. Items will "flex" to different sizes to fill the space. It makes responsive design easier.

CSS flexbox is great to use for the general layout of your website or app. It's easy to learn, is supported in all modern browsers, and it doesn't take that long to figure out the basics. By the end of this guide, you'll be ready to start using flexbox in your web projects.

The article includes helpful animated gifs from Scott Domes which will make flexbox even easier to understand and visualize.

All CSS Flexbox properties

Here is a list of all the CSS flexbox properties that can be used to position elements in CSS. Next, you'll see how they work.

CSS that can be applied to the container

display: flexbox | inline-flex;flex-direction: row | row-reverse | column | column-reverse;flex-wrap: nowrap | wrap | wrap-reverse;flex-flow: <‘flex-direction’> || <‘flex-wrap’>justify-content: flex-start | flex-end | center | space-between | space-around;align-items: flex-start | flex-end | center | baseline | stretch;align-content: flex-start | flex-end | center | space-between | space-around | stretch;

CSS that can be applied to items/elements in the container

order: <integer>;flex-grow: <number>; /* default 0 */flex-shrink: <number>; /* default 1 */flex-basis: <length> | auto; /* default auto */flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]align-self: auto | flex-start | flex-end | center | baseline | stretch;

Terminology

Flexbox - The Ultimate CSS Flex Cheatsheet (with animated diagrams!) (1)

Before you learn what the flexbox properties do, it's important to understand the associated terminology. Here are definitions of the key flexbox terms, taken from the official W3C specification for flexbox.

  • main-axis: The main axis of a flex container is the primary axis along which flex items are laid out. The direction is based on the flex-direction property.
  • main-start | main-end: The flex items are placed within the container starting on the main-start side and going toward the main-end side.
  • main size: The width or height of a flex container or flex item, whichever is in the main dimension, is that box’s main size. Its main size property is thus either its width or height property, whichever is in the main dimension.
  • cross axis: The axis perpendicular to the main axis is called the cross axis. Its direction depends on the main axis direction.
  • cross-start | cross-end: Flex lines are filled with items and placed into the container starting on the cross-start side of the flex container and going toward the cross-end side.
  • cross size: The width or height of a flex item, whichever is in the cross dimension, is the item's cross size. The cross size property is whichever of ‘width’ or ‘height’ that is in the cross dimension.

CSS Display Flex

display: flex is tells your browser, "I wanna use flexbox with this container, please."

A div element defaults to display:block. An element with this display setting takes up the full width of the line it is on. Here is an example of four colored divs in a parent div with the default display setting:

Flexbox - The Ultimate CSS Flex Cheatsheet (with animated diagrams!) (2)

To use flexbox on a section of your page, first convert the parent container to a flex container by adding display: flex; to the CSS of the parent container.

This will initialize this container as a flex container and apply some default flex properties.

Flexbox - The Ultimate CSS Flex Cheatsheet (with animated diagrams!) (3)

Flex Direction

flex-direction allows you to control how the items in the container display. Do you want them left to right, right to left, top to bottom, or bottom to top? You can do all of these easily by setting the flex-direction of the container.

The default arrangement after applying display: flex is for the items to be arranged along the main axis from left to right. The animation below shows what happens when flex-direction: column is added to the container element.

Flexbox - The Ultimate CSS Flex Cheatsheet (with animated diagrams!) (4)

You can also set flex-direction to row-reverse and column-reverse.

Flexbox - The Ultimate CSS Flex Cheatsheet (with animated diagrams!) (5)

Justify Content

justify-content is a property to align items in the container along the main axis (see terminology diagram above). This changes depending on how content is displayed. It allows us to fill any empty space on rows and define how we want to ‘justify’ it.

Here are our the most common options used to justify content: flex-start | flex-end | center | space-between | space-around.

Here is what the different options look like:

Flexbox - The Ultimate CSS Flex Cheatsheet (with animated diagrams!) (6)

space-between distributes items so that the first item is flush with the start and the last is flush with the end. space-around is similar but items have a half-size space on either end.

Flex Align Items

align-items allows us to align items along the cross axis (see terminology diagram above). This allows content to be positioned in many different ways using justify content and align items together.

Here are the most common options used to align items: flex-start | flex-end | center | baseline | stretch

For stretch to work how you would expect, the height of the elements must be set to auto instead of a specific height.

This animation shows what the options look like:

Flexbox - The Ultimate CSS Flex Cheatsheet (with animated diagrams!) (7)

Now we'll use both justify-content and align-items. This will show off the difference between the main axes and the cross axes.

Flexbox - The Ultimate CSS Flex Cheatsheet (with animated diagrams!) (8)

Align Self

align-self allows you to adjust the alignment of a single element.

It has all the same properties of align-items.

In the following animation, the parent div has the CSS align-items: center and flex-direction: row. The first two squares cycle through different align-self values.

Flexbox - The Ultimate CSS Flex Cheatsheet (with animated diagrams!) (9)

Flex Wrap

Flexbox by default will try to fit all elements into one row. However, you can change this with the flex-wrap property. There are three values you can use to determine when elements go to another row.

The default value is flex-wrap: nowrap. This will cause everything to stay in one row from left to right.

flex-wrap: wrap will allow items to pop to the next row if there is not enough room on the first row. The items will be displayed from left to right.

flex-wrap: wrap-reverse also allows items to pop to the next row but this time the items are displayed from right to left.

Flex Flow

flex-flow combines the use of flex-wrap and flex-direction into one property. It is used by first setting the direction and then the wrap. Here is an example: flex-flow: column wrap;

Align Content

align-content is used for aligning items with multiple lines. It is for aligning on the cross axis and will have no effect on content that is one line.

Here are the options: align-content: flex-start | flex-end | center | space-between | space-around | stretch;

Vertically Centering with Flexbox

If you want to vertically center all the contents inside a parent element, use align-items. Here is the code to use:

.parent { display: flex; align-items: center;}

Games and Apps

If you want to practice using flexbox, try out these games and apps that will help you master flexbox.

  • Flexbox Defense is a web game where you learn flexbox while trying to stop the incoming enemies from getting past your defenses.
  • Flexbox Froggy is a game where you help Froggy and friends by writing CSS code.
  • Flexyboxes is an app that allows you to see code samples and change parameters to see how Flexbox works visually.
  • Flexbox Patters is a website that shows off a bunch of Flexbox examples.

Conclusion

We've covered all the most common CSS flexbox properties. The next step is practice! Try making a few projects using flexbox so you can get used to how it works.

Flexbox - The Ultimate CSS Flex Cheatsheet (with animated diagrams!) (2024)

FAQs

Is flexbox still used? ›

It turns out it's now used as the main layout system for modern web pages. Flexbox is a one-dimensional layout system that we can use to create a row or a column axis layout.

What is CSS flexbox used for? ›

In a perfect world of browser support, the reason you'd choose to use flexbox is because you want to lay a collection of items out in one direction or another. As you lay out your items you want to control the dimensions of the items in that one dimension, or control the spacing between items.

What is the difference between Flex and flexbox? ›

Flexbox is a purely css layout, which means the entire layout is manage and control at css level, no need to play with the markups. Flex model is capable to adjust its items height/width to best fill in the available space of container. It can expand or shrink to avoid overflow.

What is flexbox in CSS interview questions? ›

CSS Grid Layout is a two-dimensional system along with rows and columns. It is used for large-sized layouts. Flexbox is a Grid layout with a one-dimensional system either within a row or a column. It is used for the components of an application.

What are the disadvantages of using flexbox? ›

One of the major drawbacks of using flexbox is performance issues. The use of flexbox can increase the page loading time if you have a larger project. You can compare the page loading time through Chrome development tools using the flexbox and the same pages constructed without using flexbox.

Is it better to use grid or flexbox? ›

Alignment: Aligning elements on a web page can be done in various ways, but CSS Grid and CSS Flexbox are two of the most popular methods. When comparing the two, the main difference is that CSS Grid is better suited for creating two-dimensional layouts, while CSS Flexbox is better for one-dimensional designs.

What is flexbox explained easily? ›

Flexbox is all about arranging a group of items in a row or column, and giving us a ridiculous amount of control over the distribution and alignment of those items.

What are the advantages and disadvantages of flexbox? ›

Flexbox has a one-dimensional layout which means when you want to stack the elements in one direction only i.e. either in rows or in columns then you should use Flexbox to easily layout the content on the webpages. Flexbox is not designed to support both the rows and columns alignment at the same time.

Is CSS flexbox supported by all browsers? ›

Browser Support

The flexbox properties are supported in all modern browsers.

When should I use flexbox? ›

As per my understanding we use flexbox generally on a lower level for unidirectional alignment along a row or column and grid for 2d layouts when layout and alignment is taken into consideration along both axes on a higher level.

Is flexbox similar to Bootstrap? ›

It's important to understand that Bootstrap 4's grid system is built with Flexbox. What sets Bootstrap apart from using Flexbox alone is the process of writing code. With Bootstrap, you can create a grid using only HTML. With Flexbox, you must use HTML and CSS.

Is flexbox responsive? ›

It allows you to easily align elements in rows or columns, and adjust their size and position based on the available space. With Flexbox, you can create complex, responsive layouts with just a few lines of code, making it a great choice for developers who want to build responsive websites.

What is the main feature of flexbox? ›

The flexible box layout module, usually referred to as flexbox, was designed as a one-dimensional layout model, and as a method that could offer space distribution between items in an interface and powerful alignment capabilities.

What is the difference between flex and width in CSS? ›

The flex-basis property can be applied only to flex-items and width property can be applied to all. When using flex property, all the three properties of flex-items i.e, flex-row, flex-shrink and flex-basis can be combined into one declaration, but using width, doing the same thing would require multiple lines of code.

Should I use flexbox or float? ›

It's also worth noting that Grid and Flexbox are relatively new properties and have better browser support than the float property, so it's a good idea to use them when possible.

Why use flexbox instead of float? ›

These are the following reasons to use flexbox over floats.

Positioning child elements becomes easier with flexbox. Flexbox is responsive and mobile-friendly. Flex container's margins do not collapse with the margins of its content.

Which is better float or flexbox? ›

Instead of using a float to create layouts by floating elements to the left or the right, flexbox allows you to create layouts by aligning items to a single axis. The axis can be horizontal or vertical. It is best used for distributing space for items in the same axis.

What is the alternative to flexbox? ›

Flexbox Grid AlternativesCSS Frameworks and other similar apps like Flexbox Grid
  • 242. Bootstrap. CSS Framework. ...
  • Bulma. CSS Framework. Free • Open Source. ...
  • Tailwind CSS. CSS Framework. ...
  • UIkit. Free • Open Source. ...
  • Semantic UI. Free • Open Source. ...
  • Foundation. CSS Framework. ...
  • Panda CSS. CSS Framework. ...
  • Tailwind UI. CSS Framework.
Oct 20, 2023

Top Articles
Latest Posts
Article information

Author: Trent Wehner

Last Updated:

Views: 5905

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.