We use cookies to enhance your experience on the site
CodeWorlds

Flexbox

Flexbox is a set of CSS declarations that allow us to more easily position elements on a web page and create flexible layouts. The idea for the Flexbox model appeared some time before 2008, and the first working draft was published on July 23, 2009. Over the following years, the Flexbox model was developed and adapted to the needs of designers and developers, until October 2017 when the last working draft of the Flexbox model was published.

Flexbox introduces new CSS properties that allow controlling the layout of elements in one dimension - along the main axis and across the cross axis. The main Flexbox properties are:

  • display: flex - Specifies that child elements should use the Flexbox model.
  • flex-direction - Specifies the direction in which elements will be arranged, whether they should be arranged in a horizontal line (row), vertical line (column), or reversed in a horizontal line (row-reverse) or vertical line (column-reverse).
  • justify-content - Specifies how elements are distributed along the main axis, i.e., horizontally. We can set elements to be evenly distributed, centered, aligned to the left or right side, etc.
  • align-items - Specifies how elements are distributed along the cross axis, i.e., vertically. We can set elements to be centered, aligned to the top, bottom, etc.
  • flex-grow, flex-shrink, flex-basis - Define the flexibility and how elements adapt when there isn't enough space to display them in a line.
  • align-self - Specifies the alignment of a single element along the cross axis, independently of other elements.

The Flexbox model is a very useful tool for designing responsive page layouts and managing the layout of elements within specific containers. Thanks to the flexibility and diverse possibilities it offers, Flexbox makes it easier to create unique and aesthetic web page layouts. For many years, styling was done through

float
and
position
properties. Older developers remember table-based layouts. However, they had their problems and sometimes styling was frustrating. The
display: flex;
declaration comes to the rescue:

1section {
2  display: flex;
3}

As you can see above, the

display: flex;
declaration will apply to the section element selector. In our case, the section will be the parent of three elements with the box class:

1<section>
2  <div class="box">Element 1</div>
3  <div class="box">Element 2</div>
4  <div class="box">Element 3</div>
5</section>

From the very beginning, we can see that the elements will be placed next to each other: alt text

A flex container is the foundation of a pyramid, and flex items are the stones from which the pyramid is built. Just as stones can be arranged in different ways, flex items can be organized using various properties such as justify-content or align-items.

You'll learn more in the upcoming scrolls of knowledge, and at the end of the module, a special mini-game awaits you (https://flexboxfroggy.com/#en).

Go to CodeWorlds