How to set the stack order of a positioned element in JavaScript?

JavascriptObject Oriented ProgrammingFront End Technology

To set the stack order, use the zIndex property. You can try to run the following code to implement zIndex property in JavaScript −


<!DOCTYPE html>
         #myID {
            position: absolute;
            left: 20px;
            top: 20px;
            z-index: -1
      <img id = "myID"
         width = "150" height = "150">
      <button type="button" onclick="myFunction()">Set Stack Order</button>
      <p>Z-index is -1</p>
         function myFunction() {
            document.getElementById("myID").style.zIndex = "1";
Published on 27-Feb-2018 10:35:52