Flexbox: A Beginner's Guide

Muhammad umairMuhammad umair
4 min read

Flexbox, or Flexible Box Layout, is a powerful layout tool in CSS (Cascading Style Sheets) that helps developers create responsive and flexible web designs. If you're new to web development or just want to understand what Flexbox is and how to use it, this guide is for you.

Why Flexbox?

Before Flexbox, creating complex layouts was challenging. Developers had to rely on floats, positioning, and other hacks to align elements. Flexbox simplifies this process by providing a more intuitive and powerful way to align and distribute space among items in a container.

The Basics of Flexbox

Flexbox is all about the container and its items. The container is the parent element that holds the items, and the items are the child elements inside the container. Let's break down the key concepts:

1. Flex Container

To use Flexbox, you first need to define a flex container. You do this by setting the display property to flex or inline-flex on the parent element.

.container {
  display: flex;
}

2. Flex Items

The children of the flex container automatically become flex items. These items can be manipulated to align and distribute space in various ways.

Main Axis and Cross Axis

Understanding the main axis and cross axis is essential to using Flexbox effectively.

  • Main Axis: The primary axis along which flex items are placed. By default, this is horizontal (left to right) when flex-direction: row is used, but it can change based on the flex-direction property.

  • Cross Axis: The axis perpendicular to the main axis. By default, this is vertical (top to bottom) when flex-direction: row is used.

Key Properties of Flex Container

Here are some important properties you can use on the flex container:

  • flex-direction: Determines the direction of the flex items.

    • row (default): Items are placed in a row, along the main axis.

    • row-reverse: Items are placed in a row, but reversed.

    • column: Items are placed in a column, along the main axis.

    • column-reverse: Items are placed in a column, but reversed.

.container {
  display: flex;
  flex-direction: row;
}
  • justify-content: Aligns items along the main axis.

    • flex-start: Items are aligned at the start of the container.

    • flex-end: Items are aligned at the end of the container.

    • center: Items are centered along the main axis.

    • space-between: Items are evenly spaced with the first item at the start and the last item at the end.

    • space-around: Items are evenly spaced with equal space around them.

.container {
  display: flex;
  justify-content: center;
}
  • align-items: Aligns items along the cross axis.

    • flex-start: Items are aligned at the start of the cross axis.

    • flex-end: Items are aligned at the end of the cross axis.

    • center: Items are centered along the cross axis.

    • stretch (default): Items are stretched to fill the container.

    • baseline: Items are aligned along their baseline.

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

Key Properties of Flex Items

Flex items also have properties that allow for more control:

  • order: Changes the order of the items.
.item {
  order: 2;
}
  • flex-grow: Specifies how much a flex item should grow relative to the rest of the items.
.item {
  flex-grow: 1;
}
  • flex-shrink: Specifies how much a flex item should shrink relative to the rest of the items.
.item {
  flex-shrink: 1;
}
  • flex-basis: Defines the default size of an element before the remaining space is distributed.
.item {
  flex-basis: 200px;
}
  • align-self: Allows the default alignment to be overridden for individual items.
.item {
  align-self: flex-end;
}

Putting It All Together

Here’s a simple example to illustrate how these properties work together:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    *{
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }
    .container {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      height: 100vh;
      background-color: #f0f0f0;
    }
    .item {
      background-color: #4CAF50;
      color: white;
      padding: 20px;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="item">Item 1</div>
    <div class="item">Item 2</div>
    <div class="item">Item 3</div>
  </div>
</body>
</html>

In this example, we have a container with three items. The items are spaced evenly along the main axis (horizontal), and they are centered along the cross axis (vertical). The container takes up the full height of the viewport, and each item has padding and margin for spacing.

Conclusion

Flexbox is a versatile and powerful tool for creating responsive and flexible layouts. By understanding and using the key properties of the flex container and items, you can easily control the alignment, spacing, and distribution of elements in your web designs. Experiment with these properties to see how they work together, and you'll soon find that Flexbox can make your layout tasks much simpler and more efficient.

0
Subscribe to my newsletter

Read articles from Muhammad umair directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Muhammad umair
Muhammad umair