Explore Align-Content Property in Flexbox

CSSCSSBeginner
Practice Now

Introduction

In this lab, you will explore the align-content property in Flexbox, a powerful CSS layout technique for creating flexible and responsive web designs. Through a step-by-step approach, you'll learn how to set up a flexbox container with multiple items, apply different alignment strategies, and understand how the align-content property influences multi-line flex layouts.

The lab will guide you through creating an HTML structure, adding CSS styling, and experimenting with various align-content values. By the end of this exercise, you'll gain practical skills in controlling the distribution and alignment of flex items across multiple lines, enabling you to create more sophisticated and visually appealing web page layouts.

Set Up HTML Structure for Flexbox Container

In this step, you'll learn how to create the basic HTML structure for a flexbox container that will demonstrate the align-content property. We'll start by setting up a simple HTML file with multiple flex items to showcase different alignment techniques.

Open the WebIDE and create a new HTML file named index.html in the ~/project directory. We'll create a basic structure with a flex container and several child elements.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Flexbox Align-Content Example</title>
    <style>
      /* CSS styles will be added in the next step */
    </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 class="flex-item">Item 4</div>
      <div class="flex-item">Item 5</div>
      <div class="flex-item">Item 6</div>
    </div>
  </body>
</html>

Let's break down the HTML structure:

  • We've created a <div> with the class flex-container that will serve as our flex container.
  • Inside the container, we've added six <div> elements with the class flex-item.
  • This structure will allow us to demonstrate how the align-content property works with multiple lines of flex items.

Example output when viewed in a browser:

[Basic layout with 6 items in a single line or multiple lines]

The key points to understand are:

  • The container will hold multiple flex items
  • We'll use multiple items to show how align-content affects multi-line layouts
  • The basic structure is now ready for styling in the next step

Create Basic CSS Styling for Flex Container

In this step, you'll add CSS styling to transform the HTML container into a flexbox layout and prepare it for exploring the align-content property. Open the index.html file in the WebIDE and update the <style> section with the following CSS:

<style>
    .flex-container {
        display: flex;
        flex-wrap: wrap;
        width: 300px;
        height: 300px;
        border: 2px solid #333;
        background-color: #f0f0f0;
    }

    .flex-item {
        width: 100px;
        height: 100px;
        margin: 5px;
        background-color: #4CAF50;
        color: white;
        display: flex;
        justify-content: center;
        align-items: center;
        font-family: Arial, sans-serif;
    }
</style>

Let's break down the key CSS properties:

  • display: flex; turns the container into a flex container
  • flex-wrap: wrap; allows items to wrap to multiple lines
  • width and height set a fixed container size
  • .flex-item styles create visually distinct flex items
  • justify-content and align-items center the text within each item

Example output when viewed in a browser:

[A grid-like layout with 6 green boxes, each containing centered text]

The key points to understand are:

  • Flexbox is activated with display: flex;
  • flex-wrap enables multi-line layouts
  • Each item is styled to be visually distinct
  • The container is prepared for align-content exploration

Implement Stretch Align-Content Property

In this step, you'll explore the align-content: stretch property, which is the default behavior for multi-line flex containers. Open your index.html file and update the .flex-container CSS to explicitly set the stretch alignment:

<style>
    .flex-container {
        display: flex;
        flex-wrap: wrap;
        width: 300px;
        height: 300px;
        border: 2px solid #333;
        background-color: #f0f0f0;
        align-content: stretch; /* Explicitly set stretch alignment */
    }

    .flex-item {
        width: 100px;
        height: 100px;
        margin: 5px;
        background-color: #4CAF50;
        color: white;
        display: flex;
        justify-content: center;
        align-items: center;
        font-family: Arial, sans-serif;
    }
</style>

The align-content: stretch property does the following:

  • Distributes extra space evenly between and around flex lines
  • Stretches flex lines to fill the container's cross-axis
  • Ensures that flex items expand to use the full container height
  • This is the default behavior when multiple lines are present

Example output visualization:

[Flex container with items stretched to fill available vertical space]
+-------------------+
|  Item 1  Item 2  |
|  Item 3  Item 4  |
|  Item 5  Item 6  |
+-------------------+

Key points to understand:

  • align-content controls alignment of flex lines
  • stretch is the default value for multi-line flex containers
  • It ensures flex lines use the full container height
  • Helps create responsive and evenly distributed layouts

Experiment with Different Align-Content Values

In this step, you'll explore various align-content property values to understand how they affect the layout of flex items in a multi-line container. Update your CSS to experiment with different alignment options:

<style>
    .flex-container {
        display: flex;
        flex-wrap: wrap;
        width: 300px;
        height: 300px;
        border: 2px solid #333;
        background-color: #f0f0f0;
        /* Uncomment different align-content values to see their effects */
        /* align-content: stretch; */
        /* align-content: flex-start; */
        /* align-content: flex-end; */
        /* align-content: center; */
        /* align-content: space-between; */
        /* align-content: space-around; */
    }

    .flex-item {
        width: 100px;
        height: 100px;
        margin: 5px;
        background-color: #4CAF50;
        color: white;
        display: flex;
        justify-content: center;
        align-items: center;
        font-family: Arial, sans-serif;
    }
</style>

Explore these align-content values:

  1. stretch (default): Stretches lines to fill container
  2. flex-start: Aligns lines to the start of the container
  3. flex-end: Aligns lines to the end of the container
  4. center: Centers lines in the container
  5. space-between: Distributes lines with equal space between them
  6. space-around: Distributes lines with equal space around them

Example output visualizations:

stretch:        flex-start:     flex-end:
+----------+    +----------+    +----------+
| 1  2  3  |    | 1  2  3  |    | 1  2  3  |
| 4  5  6  |    |          |    |          |
+----------+    +----------+    +----------+

center:         space-between:  space-around:
+----------+    +----------+    +----------+
|   1 2 3  |    | 1  2  3  |    |  1  2  3 |
|   4 5 6  |    |          |    |          |
+----------+    +----------+    +----------+

Key points to understand:

  • Uncomment different align-content values
  • Observe how lines are positioned within the container
  • Understand the impact of each alignment option

Understand Multi-Line Flex Layout Alignment

In this step, you'll create a comprehensive example to demonstrate how multi-line flex layout alignment works. Update your HTML and CSS to explore the relationship between flex-wrap and align-content:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Multi-Line Flex Layout Alignment</title>
    <style>
      .flex-container {
        display: flex;
        flex-wrap: wrap;
        width: 400px;
        height: 400px;
        border: 3px solid #333;
        background-color: #f0f0f0;
        align-content: center; /* Try different values */
      }

      .flex-item {
        width: 120px;
        height: 120px;
        margin: 5px;
        background-color: #4caf50;
        color: white;
        display: flex;
        justify-content: center;
        align-items: center;
        font-family: Arial, sans-serif;
        font-size: 18px;
      }
    </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 class="flex-item">Item 4</div>
      <div class="flex-item">Item 5</div>
      <div class="flex-item">Item 6</div>
    </div>
  </body>
</html>

Key concepts to understand:

  • flex-wrap: wrap allows items to flow into multiple lines
  • align-content controls the alignment of these multiple lines
  • Different align-content values create unique layout patterns

Example layout visualizations:

align-content: center       align-content: space-between
+----------------+          +----------------+
|   1  2  3      |          | 1  2  3        |
|   4  5  6      |          |                |
+----------------+          | 4  5  6        |
                            +----------------+

Experiment with these align-content values:

  1. center: Centers lines vertically
  2. space-between: Distributes lines with equal space
  3. space-around: Adds equal space around lines
  4. flex-start: Aligns lines to the top
  5. flex-end: Aligns lines to the bottom

Summary

In this lab, participants explore the align-content property in Flexbox by creating a structured HTML layout with multiple flex items. The lab begins by setting up a basic HTML file with a flex container and six child elements, demonstrating the foundational structure needed to experiment with multi-line flex layout alignment.

Through a step-by-step approach, learners will apply CSS styling to transform the container into a flexible layout, implement different align-content values, and understand how this property controls the alignment of flex lines within a multi-line flex container. The practical exercise provides hands-on experience in manipulating flex layouts, helping developers gain insights into advanced Flexbox positioning techniques.