CSS align-content

The align-content property in CSS is used to control the vertical and horizontal alignment of a flex container’s content when there is extra space on the cross-axis. This property is applied to multi-line flex containers (i.e., containers with flex-wrap set to wrap or wrap-reverse).


Syntax of align-content

</>
Copy
align-content: flex-start | flex-end | center | space-between | space-around | space-evenly | stretch | initial | inherit;

Values

ValueDescription
flex-startLines are packed to the start of the container.
flex-endLines are packed to the end of the container.
centerLines are packed to the center of the container.
space-betweenLines are distributed with equal space between them.
space-aroundLines are distributed with equal space around them.
space-evenlyLines are distributed with equal space between, before, and after them.
stretchLines are stretched to fill the container.
initialResets the property to its default value (which is stretch).
inheritInherits the value from its parent container.

Default Value

stretch


Examples for align-content

Using the CSS align-content Property

This example demonstrates how to use the align-content property to arrange content within a flex container.

</>
Copy
.container {
  width: 400px;
  height: 200px;
  display: flex;
  flex-wrap: wrap;
  align-content: space-between;
}

Complete HTML Example

The following example shows how align-content behaves with wrapped flex items inside a container.

index.html

</>
Copy
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS align-content Property</title>
<style>
  .container {
    width: 400px;
    height: 200px;
    display: flex;
    flex-wrap: wrap;
    align-content: center;
    border: 2px solid black;
  }

  .box {
    width: 50px;
    height: 50px;
    background-color: red;
    margin: 5px;
    text-align: center;
    line-height: 50px;
  }
</style>
</head>
<body>
<h1>CSS align-content Property Example</h1>

<div class="container">
  <div class="box">1</div>
  <div class="box">2</div>
  <div class="box">3</div>
</div>

</body>
</html>

Using the align-content Property in JavaScript

In JavaScript, you can dynamically set the align-content property of a flex container using the style.alignContent property.

</>
Copy
element.style.alignContent = "space-around";

Example for Setting align-content Dynamically in JS

index.html

</>
Copy
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic align-content Example</title>
<style>
  .container {
    display: flex;
    flex-wrap: wrap;
    height: 200px;
    border: 2px solid black;
    margin-bottom: 10px;
  }

  .box {
    flex: 0 0 30%;
    height: 50px;
    background-color: lightblue;
    margin: 5px;
    text-align: center;
    line-height: 50px;
  }

  button {
    padding: 10px 20px;
    font-size: 16px;
  }
</style>
</head>
<body>

<h1>Dynamic align-content Example</h1>

<div class="container" id="flexContainer">
  <div class="box">1</div>
  <div class="box">2</div>
  <div class="box">3</div>
  <div class="box">4</div>
  <div class="box">5</div>
  <div class="box">6</div>
</div>

<button id="alignCenter">Align Center</button>
<button id="alignSpaceAround">Align Space Around</button>

<script>
  // Get the flex container and buttons
  const flexContainer = document.getElementById("flexContainer");
  const alignCenterButton = document.getElementById("alignCenter");
  const alignSpaceAroundButton = document.getElementById("alignSpaceAround");

  // Event listener to align content to the center
  alignCenterButton.addEventListener("click", () => {
    flexContainer.style.alignContent = "center";
  });

  // Event listener to align content with space around
  alignSpaceAroundButton.addEventListener("click", () => {
    flexContainer.style.alignContent = "space-around";
  });
</script>

</body>
</html>

Explanation:

  1. A flex container with ID flexContainer is styled with display: flex and flex-wrap: wrap.
  2. Two buttons are provided to dynamically change the align-content property of the container.
  3. The JavaScript code listens for button clicks and sets the alignContent style of the container to either center or space-around.

Browser Support

The align-content property is supported in all modern browsers. Below is a compatibility table:

BrowserVersion
Chrome57.0
Firefox52.0
Safari10.1
Edge16.0
Opera44.0