Flexbox begins with the display property. Applying display: flex to an element turns it into a flex container, and its direct children turn into flex items. By default, flex items align side by side, left to right, all in one row. The flex container fills the available width like a block element, but the flex items may not necessarily fill the width of their flex container. The flex items are all the same height, determined naturally by their contents.

That means it allows items to stretch or shrink inside their containers, preventing wasted space and overflow. It's a real plus for making layouts fit a variety of viewport sizes. Other advantages:

Because flexbox layout is defined in terms of the main axis and cross axis, we use the terms start and end in reference to the axes, rather than left and right or top and bottom.

Let walkthrough the syntax with this game.

Container properties

Property

Example

flex-direction

flex-wrap

justify-content

align-items: container height required

align-content: container height required

Flex item properties