HTML DOM Style alignContent property


The alignContent property is used to align items in a flexbox or grid. It is used to align items on the vertical axis when they do not use all the available space.

Syntax

Following is the syntax for −

Setting the alignContent property −

object.style.alignContent = "stretch|center|flex-start|flex-end|space-between|space-around|initial|inherit"

Values

Following are the values −

ValueDescription
StretchIt is the default value and used to stretch the items to fit the container.
CenterThis is used for positioning the items at the center of the container.
flex-startTo position items at the beginning of the container.
flex-endTo position the items at the end of the container.
space-betweenTo position the items with space between the lines.
space-aroundTo position the items with space before, between and after the lines.
InitialFor setting this property to initial value.
InheritTo inherit the parent property value.

Example

Let us look at the example for the alignContent property −

Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
   #container {
      width: 180px;
      height: 220px;
      padding: 10px;
      border: 1px solid #333;
      display: flex;
      flex-flow: row wrap;
      align-content: space-around;
   }
   .ele {
      width: 60px;
      height: 60px;
      background-color: skyblue;
   }
   .ele:nth-child(2n) {
      background-color: orange;
   }
</style>
<script>
   function changeAlign(){
      document.getElementById("container").style.alignContent="flex-start";
   }
</script>
</head>
<body>
<div id="container">
<div class="ele"></div>
<div class="ele"></div>
<div class="ele"></div>
<div class="ele"></div>
<div class="ele"></div>
<div class="ele"></div>
</div>
<p>Change the align content property of the above div by clicking the belwo button</p>
<button onclick="changeAlign()">CHANGE</button>
</body>
</html>

Output

This will produce the following output −

On clicking the CHANGE button −

Updated on: 19-Feb-2021

23 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements