A Complete, Interactive Guide to CSS Flex Box

A Complete, Interactive Guide to CSS Flex Box

The ultimate interactive guide to CSS Flex box

·

2 min read

In the past, making HTML elements appear next to each other was a tricky proposition. It usually relied on floats, often resulting in some strange behaviours. Today, we have flex box, which is widely accepted as the standard way to do this. Flex box lets us align content in columns or rows, while automatically adjusting its size based on how we set it up. Below, you can view the complete guide to CSS flex box, along with a generator to let you generate the layout style you want for your container elements.

CSS Flex Box Generator

Properties for Containers

For your reference, here are the individual properties and the effects they have on a flex box configuration. For all of these examples, except flex-wrap itself, we use flex-wrap: wrap;.

justify-content

This justifies content along the flex-direction axis. %[codepen.io/smpnjn/pen/PoORNaL]

align-items

This aligns the content along the axis perpendicular to the flex-direction axis for a single row of items. %[codepen.io/smpnjn/pen/BamrKVe]

flex-direction

This sets then main axis of the flex, whether that is vertical (column) or horizontal (row). %[codepen.io/smpnjn/pen/bGYvpKX]

align-content

This sets the position for all rows in a much larger box. %[codepen.io/smpnjn/pen/rNYdery]

flex-wrap

This sets the position for all rows in a much larger box. %[codepen.io/smpnjn/pen/eYeMZjg]

Properties for Items

Although you can affect entire collections of items with flex box, you can also call out individual items and apply styles to them in particular. Below, the examples show you how to do that.

order

When applied to an item, it gives it an order relevant to other items around it. An order of 9999 will go to the end of the flow direction. Think of this like z-index, but for flex box. %[codepen.io/smpnjn/pen/ZEaxWjB]

flex-grow

Dictates the alignment of a particular item along the perpendicular axis to flow-direction. %[codepen.io/smpnjn/pen/MWOVyBj]

align-self

Dictates the alignment of a particular item along the perpendicular axis to flow-direction. %[codepen.io/smpnjn/pen/zYPWqLz]

Conclusion

I hope you've enjoyed this guide on flex box, and I hope it's been useful in fully explaining how flex box works. If you want to see something similar for CSS grid, see the link here.

Did you find this article valuable?

Support Daily Dev Digest by becoming a sponsor. Any amount is appreciated!