Javascript DOM CSS Style flexGrow Property

Introduction

Let the blue DIV element grow five times wider than the rest of the flexible items:

document.getElementById("myBlueDiv").style.flexGrow = "5";

Click the button to set the value of the flexGrow property to "5" for the blue div element.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#main {//from   ww w.ja v  a  2 s .c om
  width: 350px;
  height: 100px;
  border: 1px solid #c3c3c3;
  display: flex;
}

#main div:nth-of-type(1) {flex-grow: 1;}
#main div:nth-of-type(2) {flex-grow: 1;}
#main div:nth-of-type(3) {flex-grow: 1;}
</style>
</head>
<body>

<div id="main">
  <div style="background-color:coral;">a</div>
  <div style="background-color:lightblue;" id="myBlueDiv"></div>
  <div style="background-color:red;">b</div>
</div>
<button onclick="myFunction()">Test</button>
<script>
function myFunction() {
  document.getElementById("myBlueDiv").style.flexGrow = "5";
}
</script>

</body>
</html>

The flexGrow property sets how much the item will grow relative to the rest of the flexible items inside the same container.

If the element is not a flexible item, the flexGrow property has no effect.

Property Values

Value Description
number A number setting how much the item will grow relative to the rest of the flexible items. Default value is 0
initial Sets this property to its default value.
inherit Inherits this property from its parent element.

The flexGrow property returns a String representing the flex-grow property of an element.




PreviousNext

Related