Guide to Flexbox – CSS Tricks

Flexbox is a powerful layout tool that enables you to design flexible and responsive layouts for your web pages. It allows you to easily align and distribute elements within a container, regardless of their size, and without the need for complex positioning or float properties. In this guide, we’ll cover the basics of Flexbox, including the following topics:

  1. Flex container and items
  2. Flexbox properties
  3. Flex-direction
  4. Justify-content
  5. Align-items
  6. Flex-wrap
  7. Align-content
  8. Flexbox examples
  9. Flex container and items

A Flex container is an HTML element that contains one or more Flex items. To turn an HTML element into a Flex container, you need to set its display property to flex or inline-flex. By default, Flex items will be arranged in a row, from left to right.

Flex items are the child elements of a Flex container. They can be any HTML element, including divs, images, and text. Each Flex item is positioned within the Flex container, and can be sized and aligned using Flexbox properties.

  1. Flexbox properties

There are many properties available in Flexbox that allow you to customize the layout of your Flex container and items. Here are some of the most important ones:

  1. Flex-direction

This property controls the direction of the Flex container. By default, the direction is set to row, which arranges items from left to right. You can also set it to column, which arranges items from top to bottom.

  1. Justify-content

This property controls the horizontal alignment of the Flex items within the Flex container. It has several possible values, including:

  • flex-start: Aligns items to the left of the container
  • flex-end: Aligns items to the right of the container
  • center: Aligns items in the center of the container
  • space-between: Distributes items evenly along the main axis, with space between them
  • space-around: Distributes items evenly along the main axis, with space around them
  • space-evenly: Distributes items evenly along the main axis, with equal space between them
  1. Align-items

This property controls the vertical alignment of the Flex items within the Flex container. It has several possible values, including:

  • flex-start: Aligns items to the top of the container
  • flex-end: Aligns items to the bottom of the container
  • center: Aligns items in the center of the container
  • stretch: Stretches items to fill the container vertically
  • baseline: Aligns items along their baselines
  1. Flex-wrap

This property controls whether the Flex items should wrap onto multiple lines or not. By default, items will not wrap. You can set it to wrap if you want items to wrap onto additional lines if there isn’t enough space for them.

  1. Align-content

This property controls the alignment of Flex items when there is extra space in the Flex container. It has several possible values, including:

  • flex-start: Aligns items to the top of the container
  • flex-end: Aligns items to the bottom of the container
  • center: Aligns items in the center of the container
  • space-between: Distributes items evenly along the cross axis, with space between them
  • space-around: Distributes items evenly along the cross axis, with space around them
  • stretch: Stretches items to fill the container vertically
  1. Flexbox examples

Here are some examples of how Flexbox can be used to create flexible layouts:

  • Centering content: You can use Flexbox to easily center content both horizontally and vertically within a container.
  • Responsive navigation: Flexbox can be used to create a flexible and responsive navigation menu that adapts to different screen sizes and devices.
  • Grid-like layout: Flexbox can also be used to create grid-like layouts with equal spacing between items, without the need for complex CSS grid or table layouts.
  • Equal height columns: With Flexbox, you can create columns with equal heights, regardless of their content, which can be useful for creating consistent layouts.

In summary, Flexbox is a powerful tool that can be used to create flexible and responsive layouts for your web pages. By mastering its properties and capabilities, you can easily align and distribute elements within a container, and create a variety of layouts that adapt to different devices and screen sizes.