CSS - align-content Property



The align-content property of CSS is used to align the content of a flex container along the cross-axis or a grid's block axis. It applies to flex containers with multiple lines of flex items.

The property align-content has no effect on single line flex containers (flex-wrap: nowrap).

Possible Values

The align-content property can have following values:

  • start: Items are aligned to each other against the start edge of the container in the cross-axis.

  • end: Items are aligned to each other against the end edge of the container in the cross-axis.

  • flex-start: Aligns the content at the start of the flex container.

  • flex-end: Aligns the content at the end of the flex container.

  • center: Aligns the content at the center of the flex container.

  • normal: Items are aligned at their default position, as if no value is set for align-content.

  • baseline, first baseline, last baseline:

    • First baseline, and last baseline are synonym to baseline. First and last refer to the line boxes within the flex items.

    • These values specify the involvment of first- or last-baseline alignment in the alignment of the content.

    • start is the fallback alignment for first-baseline and end for last-baseline.

  • space-between: Distributes the content evenly along the cross-axis, leaving no space before the first line or after the last line.

  • space-around: Distributes the content evenly along the cross-axis, providing equal space before and after each line.

  • space-evenly: Distributes the content evenly along the cross-axis, providing equal space before, between, and after each line.

  • stretch: Stretches the content to fill the container along the cross-axis.

  • safe: Used with an alignment keyword and when the item overflows the container, causing any loss of data, the alignment is set as per the start value.

  • unsafe: Used with an alignment keyword and even if the item overflows the container, causing any loss of data, the alignment value passed is honored.

Applies to

Multi-line flex containers.

Syntax

Positional Alignment

align-content: center; 
align-content: start; 
align-content: end; 
align-content: flex-start; 
align-content: flex-end

Normal Alignment

align-content: normal;

Baseline Alignment

align-content: baseline;
align-content: first baseline;
align-content: last baseline;

Distributed Alignment

align-content: space-between; 
align-content: space-around; 
align-content: space-evenly; 
align-content: stretch;

CSS align-content- start Value

The following example demonstrates that the align-content: start property aligns the flex items at the start of the cross axis of a flex container −

<html>
<head>
<style>
   .flex-container {
      display: flex;
      background-color: green;
      height: 350px;
      width: 100%;
      flex-wrap: wrap;
      align-content: start;
   }
   .flex-container div {
      background-color: yellow;
      padding: 10px;
      margin: 5px;
      width: 90px;
      height: 50px;
   }
</style>
</head>
<body>
   <div class="flex-container">
      <div>Flex item 1</div>
      <div>Flex item 2</div>
      <div>Flex item 3</div>
      <div>Flex item 4</div>
      <div>Flex item 5</div>
      <div>Flex item 6</div>
      <div>Flex item 7</div>
   </div>
</body>
</html>

CSS align-content- end Value

The following example demonstrates that the align-content: end property aligns the flex items at the end of the cross axis of a flex container −

<html>
<head>
<style>
   .flex-container {
      display: flex;
      background-color: green;
      height: 350px;
      width: 100%;
      flex-wrap: wrap;
      align-content: end;
   }
   .flex-container div {
      background-color: yellow;
      padding: 10px;
      margin: 5px;
      width: 90px;
      height: 50px;
   }
</style>
</head>
<body>
   <div class="flex-container">
      <div>Flex item 1</div>
      <div>Flex item 2</div>
      <div>Flex item 3</div>
      <div>Flex item 4</div>
      <div>Flex item 5</div>
      <div>Flex item 6</div>
      <div>Flex item 7</div>
   </div>
</body>
</html>

CSS align-content - center Value

The following example demonstrates that the align-content: center property aligns the flex items at the center of the cross axis of a flex container −

<html>
<head>
<style>
   .flex-container {
      display: flex;
      background-color: green;
      height: 350px;
      width: 100%;
      flex-wrap: wrap;
      align-content: center;
   }
   .flex-container div {
      background-color: yellow;
      padding: 10px;
      margin: 5px;
      width: 90px;
      height: 50px;
   }
</style>
</head>
<body>
   <div class="flex-container">
      <div>Flex item 1</div>
      <div>Flex item 2</div>
      <div>Flex item 3</div>
      <div>Flex item 4</div>
      <div>Flex item 5</div>
      <div>Flex item 6</div>
      <div>Flex item 7</div>
   </div>
</body>
</html>

CSS align-content - flex-start Value

The following example demonstrates that the align-content: flex-start property aligns the flex items at the start of the flex container −

<html>
<head>
<style>
   .flex-container {
      display: flex;
      background-color: green;
      height: 350px;
      width: 100%;
      flex-wrap: wrap;
      align-content: flex-start;
   }
   .flex-container div {
      background-color: yellow;
      padding: 10px;
      margin: 5px;
      width: 90px;
      height: 50px;
   }
</style>
</head>
<body>
   <div class="flex-container">
      <div>Flex item 1</div>
      <div>Flex item 2</div>
      <div>Flex item 3</div>
      <div>Flex item 4</div>
      <div>Flex item 5</div>
      <div>Flex item 6</div>
      <div>Flex item 7</div>
   </div>
</body>
</html>

CSS align-content - flex-end Value

The following example demonstrates that the align-content: flex-end property aligns the flex items at the end of the flex container −

<html>
<head>
<style>
   .flex-container {
      display: flex;
      background-color: green;
      height: 350px;
      width: 100%;
      flex-wrap: wrap;
      align-content: flex-end;
   }
   .flex-container div {
      background-color: yellow;
      padding: 10px;
      margin: 5px;
      width: 90px;
      height: 50px;
   }
</style>
</head>
<body>
   <div class="flex-container">
      <div>Flex item 1</div>
      <div>Flex item 2</div>
      <div>Flex item 3</div>
      <div>Flex item 4</div>
      <div>Flex item 5</div>
      <div>Flex item 6</div>
      <div>Flex item 7</div>
   </div>
</body>
</html>

CSS align-content - space-between Value

The following example demonstrates that the align-content: space-between property evenly distributes space between the flex items along the cross axis of a flex container −

<html>
<head>
<style>
   .flex-container {
      display: flex;
      background-color: green;
      height: 350px;
      width: 100%;
      flex-wrap: wrap;
      align-content: space-between;
   }
   .flex-container div {
      background-color: yellow;
      padding: 10px;
      margin: 5px;
      width: 90px;
      height: 50px;
   }
</style>
</head>
<body>
   <div class="flex-container">
      <div>Flex item 1</div>
      <div>Flex item 2</div>
      <div>Flex item 3</div>
      <div>Flex item 4</div>
      <div>Flex item 5</div>
      <div>Flex item 6</div>
      <div>Flex item 7</div>
   </div>
</body>
</html>

CSS align-content - space-around Value

The following example demonstrates that align-content: space-around property distributes the flex items within a flex container with equal space around each item −

<html>
<head>
<style>
   .flex-container {
      display: flex;
      background-color: green;
      height: 350px;
      width: 100%;
      flex-wrap: wrap;
      align-content: space-around;
   }
   .flex-container div {
      background-color: yellow;
      padding: 10px;
      margin: 5px;
      width: 90px;
      height: 50px;
   }
</style>
</head>
<body>
   <div class="flex-container">
      <div>Flex item 1</div>
      <div>Flex item 2</div>
      <div>Flex item 3</div>
      <div>Flex item 4</div>
      <div>Flex item 5</div>
      <div>Flex item 6</div>
      <div>Flex item 7</div>
   </div>
</body>
</html>

CSS align-content - space-evenly Value

The following example demonstrates that align-content: space-evenly evenly distributes the space around and between the flex items along the cross axis of a flex container −

<html>
<head>
<style>
   .flex-container {
      display: flex;
      background-color: green;
      height: 300px;
      width: 100%;
      flex-wrap: wrap;
      align-content: space-evenly;
   }
   .flex-container div {
      background-color: yellow;
      padding: 10px;
      margin: 5px;
      width: 90px;
      height: 50px;
   }
</style>
</head>
<body>
   <div class="flex-container">
      <div>Flex item 1</div>
      <div>Flex item 2</div>
      <div>Flex item 3</div>
      <div>Flex item 4</div>
      <div>Flex item 5</div>
      <div>Flex item 6</div>
   </div>
</body>
</html>

CSS align-content - baseline Value

The following example demonstrates that the align-content: baseline property aligns flex items along their baselines of the flex container −

<html>
<head>
<style>
   .flex-container {
      display: flex;
      background-color: green;
      height: 350px;
      width: 100%;
      flex-wrap: wrap;
      align-content: baseline;
   }
   .flex-container div {
      background-color: yellow;
      padding: 10px;
      margin: 5px;
      width: 90px;
      height: 50px;
   }
</style>
</head>
<body>
   <div class="flex-container">
      <div>Flex item 1</div>
      <div>Flex item 2</div>
      <div>Flex item 3</div>
      <div>Flex item 4</div>
      <div>Flex item 5</div>
      <div>Flex item 6</div>
      <div>Flex item 7</div>
   </div>
</body>
</html>

CSS align-content - stretch Value

The following example demonstartes that align-content: stretch property stretches the flex items along the cross-axis of the flex container to fill the available space −

<html>
<head>
<style>
   .flex-container {
      display: flex;
      background-color: green;
      height: 200px;
      width: 100%;
      flex-wrap: wrap;
      align-content: stretch;
   }
   .flex-container div {
      background-color: yellow;
      padding: 10px;
      margin: 5px;
      width: 90px;
   }
</style>
</head>
<body>
   <div class="flex-container">
      <div>Flex item 1</div>
      <div>Flex item 2</div>
      <div>Flex item 3</div>
   </div>
</body>
</html>
Advertisements