Getting Started with Flexbox

Learn CSS Flexbox concepts, core properties, and practical applications for flexible layouts.

🎯 Introduction

Flexbox is a layout mode introduced with CSS3. It addresses common challenges in traditional layouts, especially in responsive design, such as aligning elements, distributing space, and adjusting sizes dynamically.

With Flexbox, you can build flexible structures such as navigation bars, card layouts, flexible grids, and vertically centered content. It is particularly useful for mobile and responsive designs, improving development efficiency and maintainability.

This article starts with the basic concepts, introduces the core properties, and uses examples to help you get comfortable with this powerful layout tool.


🧠 What is CSS Flexbox?

Flexbox, short for Flexible Box Layout, is a CSS layout mode that lets a container distribute space among its children and align them flexibly to accommodate different screen sizes and content dimensions.

Traditional block, inline, and float layouts can be cumbersome for some tasks. Flexbox focuses on one-dimensional layout—a row or a column—to make layout control more straightforward.

In practice:

BenefitDescription
Flexible space distributionItems can resize to fill the container
Easy centeringNo complex margin or positioning techniques required
Wrapping and reorderingItems can wrap automatically or change their visual order

📚 Flexbox Concepts and Terminology

Before exploring Flexbox, get familiar with these terms:

TermDefinition
Flex containerA parent with display: flex; or display: inline-flex;
Flex itemA direct child of a flex container
Main axisThe primary layout direction; by default, horizontal and left to right in a left-to-right writing mode
Cross axisThe axis perpendicular to the main axis; by default, top to bottom in a horizontal writing mode
Main startThe start of the main axis, such as the left edge by default
Cross startThe start of the cross axis, such as the top edge by default

Flexbox arranges and aligns items along the main axis and cross axis.


🧩 Core Flex Container Properties

A flex container controls the overall arrangement of its items. Its core properties include:

PropertyValues and meaning
displayflex creates a block-level flex container; inline-flex creates an inline-level flex container
flex-directionSets the main axis; defaults to row; values include row, row-reverse, column, column-reverse
flex-wrapControls wrapping; defaults to nowrap; other values include wrap, wrap-reverse
justify-contentAligns items along the main axis; common values: flex-start, flex-end, center, space-between, space-around, space-evenly
align-itemsAligns items along the cross axis; common values: flex-start, flex-end, center, baseline, stretch
align-contentAligns flex lines along the cross axis in wrapping containers; offers alignment values similar to align-items

🧱 Flex Item Properties

Flex items—the container's children—also have properties that control their behavior:

PropertyValues and meaning
orderSets visual order; lower numbers come first; defaults to 0
flex-growSets the growth factor; defaults to 0, meaning no growth
flex-shrinkSets the shrink factor; defaults to 1, allowing shrinking
flex-basisSets the initial main-axis size; defaults to auto, or accepts a specific size
align-selfOverrides the container's cross-axis alignment for this item, using values from align-items

🧪 Example: Build a Navigation Bar with Flexbox

Let's build a simple responsive navigation bar with Flexbox:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Flexbox Navigation Example</title>
        <style>
            * {
                box-sizing: border-box;
            }
            body {
                margin: 0;
                font-family: Arial, sans-serif;
            }
            .nav {
                display: flex;
                background-color: #004466;
                padding: 0 20px;
                align-items: center; /* Center along the cross axis */
                justify-content: space-between; /* Place groups at opposite ends of the main axis */
                height: 50px;
            }
            .nav .logo {
                color: white;
                font-size: 20px;
                font-weight: bold;
            }
            .nav ul {
                display: flex;
                list-style: none;
                margin: 0;
                padding: 0;
            }
            .nav ul li {
                margin-left: 20px;
            }
            .nav ul li a {
                color: white;
                text-decoration: none;
                font-size: 16px;
                padding: 8px 12px;
                display: block;
            }
            .nav ul li a:hover {
                background-color: #0077aa;
                border-radius: 4px;
            }
        </style>
    </head>
    <body>
        <nav class="nav">
            <div class="logo">MySite</div>
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">Products</a></li>
                <li><a href="#">Services</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </nav>
    </body>
</html>

How It Works

  • .nav becomes a flex container with a horizontal main axis by default.
  • justify-content: space-between places the logo and navigation menu at opposite ends.
  • align-items: center centers them vertically.

❓ Knowledge Check

Q1: How does Flexbox differ from float-based layouts?

Flexbox is designed for one-dimensional layouts, making alignment and space distribution straightforward. Floats primarily allow content such as text to wrap around an element and are less convenient for centering or evenly distributed layouts.

Q2: How do you wrap items when there are too many for one line?

Set flex-wrap: wrap; on the container. Items that do not fit move onto another line.

Q3: Why does align-items: stretch make items fill the cross axis?

Stretching is the default cross-axis alignment behavior in Flexbox. Items with an automatic cross size stretch to fill the available space in their flex line.


🧪 Exercises

Exercise 1: Create a Horizontally Centered Button Group

Use Flexbox to arrange a group of buttons horizontally in the center with even spacing between them.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Button Group Exercise</title>
        <style>
            .button-group {
                display: flex;
                justify-content: center;
                gap: 20px;
                margin-top: 30px;
            }
            .button-group button {
                padding: 10px 20px;
                font-size: 16px;
                cursor: pointer;
            }
        </style>
    </head>
    <body>
        <div class="button-group">
            <button>Button 1</button>
            <button>Button 2</button>
            <button>Button 3</button>
        </div>
    </body>
</html>

Copy the code into your editor and preview the result!


Exercise 2: Center a Card Vertically

Center a card horizontally and vertically inside a fixed-height container.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Vertically Centered Card</title>
        <style>
            .container {
                display: flex;
                justify-content: center;
                align-items: center;
                height: 300px;
                border: 2px solid #333;
                margin: 50px;
            }
            .card {
                width: 200px;
                height: 150px;
                background-color: #0099cc;
                color: white;
                display: flex;
                justify-content: center;
                align-items: center;
                font-size: 18px;
                border-radius: 8px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="card">Centered Card</div>
        </div>
    </body>
</html>

Copy the code into your editor and preview the result!


🎉 You have learned Flexbox concepts, core properties, and simple layout techniques! As you practice, its flexibility will help you build frontend layouts more easily and efficiently. Keep going!