Javascript DOM CSS Style alignSelf Property

Introduction

Set the alignments for one of the items inside a flexible element to fit the container:

document.getElementById("myBlueDiv").style.alignSelf = "stretch";

Click the button to set the value of the alignSelf property to "stretch".

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#main {// ww w.ja  va  2 s.  c om
  width: 220px;
  height: 300px;
  border: 1px solid black;
  display: flex;
  align-items: flex-start;
}

#main div {
  flex: 1;
}

#myBlueDiv {
  align-self: center;
}
</style>
</head>
<body>

<div id="main">
  <div style="background-color:coral;">RED</div>
  <div style="background-color:lightblue;" id="myBlueDiv">BLUE</div>
  <div style="background-color:lightgreen;">Green div with more content.</div>
</div>
<button onclick="myFunction()">Test</button>

<script>
function myFunction() {
  document.getElementById("myBlueDiv").style.alignSelf = "stretch";
}
</script>

</body>
</html>

The alignSelf property specifies the alignment for the selected item inside the flexible container.

The alignSelf property overrides the flexible container's alignItems property.

Property Values

Value Description
auto Default. The element inherits its parent container's align-items property, or "stretch" if it has no parent container
stretchThe element is positioned to fit the container
center The element is positioned at the center of the container
flex-start The element is are positioned at the beginning of the container
flex-end The element is positioned at the end of the container
baseline The element is positioned at the baseline of the container
initialSets this property to its default value.
inheritInherits this property from its parent element.

The alignSelf property returns a String representing the align-self property of an element.




PreviousNext

Related