HTML DOM Style alignSelf property


The HTML DOM alignSelf property is used for specifying a given item alignment present inside a flexible container. The alignSelf property is used to override the specified align-items value in an element’s grid or flex display layout.

Syntax

Following is the syntax for −

Setting the alignSelf property −

object.style.alignSelf = "auto|stretch|center|flex-start|flex-end|baseline|initial|inherit"

Values

Following are the values of the alignSelf property −

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.
baselineTo position the items at the container baseline
initialFor setting this property to initial value.
InheritTo inherit the parent property value.

Example

Let us look at the example for the Style alignSelf property −

Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
   #container {
      width: 180px;
      height: 220px;
      padding: 10px;
      border: 1px solid #333;
      display: flex;
      align-items:baseline;
      flex-flow: row wrap;
   }
   .ele {
      width: 60px;
      height: 60px;
      background-color: skyblue;
   }
   .ele:nth-child(2n) {
      background-color: orange;
   }
</style>
<script>
   function changeAlign(){
      document.getElementsByClassName("ele")[1].style.alignSelf="flex-end";
   }
</script>
</head>
<body>
<h2>Demo Heading</h2>
<div id="container">
<div class="ele"></div>
<div class="ele"></div>
<div class="ele"></div>
</div>
<p>Change the align Self property of the above div by clicking the below 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

33 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements