Javascript DOM CSS Style order Property

Introduction

Set the order of the flexible items:

Click the button to change the order of the four DIV's:

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#main {// w ww  . j  av  a 2  s.co m
  width: 400px;
  height: 150px;
  border: 1px solid #000000;
  display: flex;
}

#main div {
  width: 70px;
  height: 70px;
}
</style>
</head>
<body>

<div id="main">
  <div style="background-color:coral;" id="myRedDIV"></div>
  <div style="background-color:lightblue;" id="myBlueDIV"></div>
  <div style="background-color:lightgreen;" id="myGreenDIV"></div>
  <div style="background-color:pink;" id="myPinkDIV"></div>
</div>
<button onclick="myFunction()">Test</button>

<script>
function myFunction() {
  document.getElementById("myRedDIV").style.order = "4";
  document.getElementById("myBlueDIV").style.order = "3";
  document.getElementById("myGreenDIV").style.order = "1";
  document.getElementById("myPinkDIV").style.order = "2";
}
</script>

</body>
</html>

The order property specifies the order of a flexible item relative to the rest of the flexible items inside the same container.

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

Property Values

Value Description
number Default value 0. Specifies the order for the flexible item
initial Sets this property to its default value.
inherit Inherits this property from its parent element.

The order property returns a String representing the order property of an element.




PreviousNext

Related