CSS Flexbox

CSS Flexbox: Flexible Box Layout

The Flexible Box Layout module (Flexbox) introduces a one-dimensional layout system that handles space distribution and item alignment effectively. It works seamlessly for horizontal (row) or vertical (column) arrangements, making it a go-to solution for modern, responsive designs.

Before Flexbox, web layouts heavily relied on properties like block, inline, table, and float, which were often clunky and difficult to use for complex responsive designs.

Why use Flexbox?


Basic Flexbox Example

To start using Flexbox, you must define a container element as a flex container by setting its display property to flex.

Simple Flex Container Example:

<!DOCTYPE html>
<html>
<head>
    <style>
        .flex-container {
            display: flex;
            background-color: #f4f4f9;
            padding: 10px;
            border: 2px solid navy;
        }
        .flex-item {
            background-color: lightblue;
            color: navy;
            margin: 5px;
            padding: 20px;
            text-align: center;
            border: 1px solid navy;
            flex: 1; /* Makes all items equal width */
        }
    </style>
</head>
<body>
    <div class="flex-container">
        <div class="flex-item">Item 1</div>
        <div class="flex-item">Item 2</div>
        <div class="flex-item">Item 3</div>
    </div>
</body>
</html>

In this example:


Core Flexbox Concepts

To master Flexbox, you must understand its components and how it calculates space along its axes.

Flex Container vs. Flex Items

The Two Axes of Flexbox

Flexbox operates strictly on two axes. How items are aligned depends entirely on which axis you are targeting.

1. Main Axis

The main axis is the primary axis along which flex items are laid out. By default, this is horizontal (left to right).

2. Cross Axis

The cross axis is always perpendicular (at a 90-degree angle) to the main axis. By default, this is vertical (top to bottom).


Flex Direction

The flex-direction property defines the direction of the Main Axis, dictating how items are placed in the container.

Property Value Description
row (Default) Left to Right. Main axis is horizontal.
row-reverse Right to Left. Main axis is horizontal, but items start from the right.
column Top to Bottom. Main axis is vertical.
column-reverse Bottom to Top. Main axis is vertical, but items start from the bottom.

Aligning and Justifying Content

Flexbox provides powerful properties applied to the Container for aligning its inner items.

1. justify-content (Main Axis Alignment)

Aligns items along the Main Axis.

2. align-items (Cross Axis Alignment)

Aligns items along the Cross Axis.

3. align-content (Multi-line Alignment)

Aligns entire rows within a flex container when there is extra space on the cross axis. (Note: This only works if flex-wrap: wrap is applied and there are multiple lines of content).


Practical Examples of CSS Flexbox

1. Responsive Grid Design with Flexbox

Flexbox excels at creating responsive designs by adjusting items to fit various screen sizes. You can use flex-wrap and media queries to ensure your layout adapts seamlessly to mobile devices.

Responsive Flexbox Grid:

<!DOCTYPE html>
<html>
<head>
    <style>
    .responsive-container {
        display: flex;
        flex-wrap: wrap; /* Allows items to drop to the next line */
        justify-content: space-around;
        background-color: navy;
        padding: 10px;
    }
    .responsive-container div {
        background-color: lightblue;
        color: navy;
        font-weight: bold;
        margin: 10px;
        padding: 20px;
        text-align: center;
        /* Grow/shrink as needed, base width 200px */
        flex: 1 1 200px; 
    }
    /* Mobile view: Stack items vertically */
    @media (max-width: 600px) {
        .responsive-container {
            flex-direction: column;
        }
    }
    </style>
</head>
<body>
    <h3>Resize the browser window!</h3>
    <div class="responsive-container">
        <div>Item 1</div>
        <div>Item 2</div>
        <div>Item 3</div>
    </div>
</body>
</html>

2. Horizontal Navigation Bar

Flexbox is the modern standard for creating clean, perfectly aligned navigation bars.

Flexbox Navbar Example:

<!DOCTYPE html>
<html>
<head>
    <style>
        .navbar {
            display: flex;
            justify-content: space-between; /* Spreads items evenly */
            align-items: center; /* Vertically centers the text */
            background-color: navy;
            padding: 10px 20px;
            font-family: sans-serif;
        }
        .nav-brand {
            color: white;
            font-size: 24px;
            font-weight: bold;
        }
        .nav-links {
            display: flex; /* Nested flex container! */
            gap: 15px; /* Adds space between links */
        }
        .navbar a {
            color: white;
            text-decoration: none;
            padding: 10px 15px;
            border-radius: 4px;
            transition: background 0.3s;
        }
        .navbar a:hover {
            background-color: lightblue;
            color: navy;
        }
    </style>
</head>
<body>
    <div class="navbar">
        <div class="nav-brand">IntricateDevo</div>
        <div class="nav-links">
            <a href="#home">Home</a>
            <a href="#courses">Courses</a>
            <a href="#contact">Contact</a>
        </div>
    </div>
</body>
</html>

Exercise

?

Which Flexbox property is used to align items horizontally along the main axis (e.g., centering them or spacing them out)?