Introduction to Flexbox

An introductory lesson to Flexbox layout. Learn the fundamental concepts and basic syntax for creating flexible and responsive layouts.


Introduction to Flexbox

What is Flexbox?

Flexbox, short for Flexible Box Layout, is a CSS layout model that offers a more efficient way to arrange, align, and distribute space among items in a container, even when their size is unknown or dynamic. It provides powerful tools for creating responsive and flexible web layouts without relying on hacks or complex workarounds. It is particularly useful for one-dimensional layouts (either rows or columns).

Introductory Lesson to Flexbox Layout

Let's dive into the fundamental concepts and basic syntax for creating flexible and responsive layouts using Flexbox.

Fundamental Concepts

  • Flex Container: The parent element that holds the flex items. You turn an element into a flex container by setting its `display` property to `flex` or `inline-flex`.
  • Flex Items: The direct children of the flex container. These items will be arranged according to the flexbox properties applied to the container.
  • Main Axis: The primary axis along which flex items are laid out. By default, this is horizontal (left to right).
  • Cross Axis: The axis perpendicular to the main axis. By default, this is vertical (top to bottom).

Basic Syntax

1. Creating a Flex Container

To turn an element into a flex container, use the `display` property:

.container {
    display: flex; /* or inline-flex */
}

2. Flex Container Properties

These properties control the behavior of the flex container and its items:

  • `flex-direction`: Defines the direction of the main axis.
    • `row` (default): Items are placed side by side, left to right.
    • `row-reverse`: Items are placed side by side, right to left.
    • `column`: Items are placed vertically, top to bottom.
    • `column-reverse`: Items are placed vertically, bottom to top.
    .container {
        display: flex;
        flex-direction: row; /* or column, row-reverse, column-reverse */
    }
  • `justify-content`: Defines how flex items are aligned along the *main* axis.
    • `flex-start` (default): Items are packed toward the start of the main axis.
    • `flex-end`: Items are packed toward the end of the main axis.
    • `center`: Items are centered along the main axis.
    • `space-between`: Items are evenly distributed along the main axis; first item is at the start, last item is at the end.
    • `space-around`: Items are evenly distributed along the main axis with equal space around them.
    • `space-evenly`: Items are evenly distributed along the main axis with equal space between them.
    .container {
        display: flex;
        justify-content: center; /* Example */
    }
  • `align-items`: Defines how flex items are aligned along the *cross* axis.
    • `stretch` (default): Items are stretched to fill the container along the cross axis.
    • `flex-start`: Items are aligned to the start of the cross axis.
    • `flex-end`: Items are aligned to the end of the cross axis.
    • `center`: Items are centered along the cross axis.
    • `baseline`: Items are aligned along their baselines.
    .container {
        display: flex;
        align-items: center; /* Example */
    }
  • `flex-wrap`: Specifies whether the flex items should wrap if they overflow the container.
    • `nowrap` (default): Items are all in one line.
    • `wrap`: Items wrap onto multiple lines.
    • `wrap-reverse`: Items wrap onto multiple lines in reverse order.
    .container {
        display: flex;
        flex-wrap: wrap;
    }
  • `flex-flow`: A shorthand property for setting both `flex-direction` and `flex-wrap`. Example: `flex-flow: row wrap;`

3. Flex Item Properties

These properties control the behavior of individual flex items:

  • `order`: Specifies the order in which flex items appear. Defaults to `0`. Items with lower order values appear earlier.
    .item {
        order: 1;
    }
  • `flex-grow`: Specifies how much a flex item will grow relative to the other flex items in the container. Defaults to `0`. If all items have `flex-grow: 1`, they will share the available space equally.
    .item {
        flex-grow: 1;
    }
  • `flex-shrink`: Specifies how much a flex item will shrink relative to the other flex items in the container. Defaults to `1`.
    .item {
        flex-shrink: 0;
    }
  • `flex-basis`: Specifies the initial size of a flex item before free space is distributed according to the values of `flex-grow` and `flex-shrink`. Defaults to `auto`.
    .item {
        flex-basis: 200px;
    }
  • `flex`: A shorthand property for setting `flex-grow`, `flex-shrink`, and `flex-basis`. The recommended value is `flex: `. For example: `flex: 1 1 auto;`
  • `align-self`: Overrides the `align-items` property of the flex container for a specific flex item. Accepts the same values as `align-items`.
    .item {
        align-self: flex-start;
    }

Example: A Simple Navigation Bar

Here's a basic example of how you might use Flexbox to create a simple navigation bar:

<nav class="navbar">
    <a href="#">Home</a>
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Contact</a>
</nav>

<style>
.navbar {
    display: flex;
    justify-content: space-around; /* Distribute items evenly */
    background-color: #333;
    padding: 10px;
}

.navbar a {
    color: white;
    text-decoration: none;
}
</style> 

A Practical Example

Let's create a simple flex container with three items:

Item 1
Item 2
Item 3