HTML | DOM Style zIndex Property

The Style zIndex property is used for set or return the stack order of a positioned element. The element which has a lower stack order will be behind of another element with higher stack order.
For example, the element with stack order(1) will be in front of the element with stack order(0).
The Style zIndex property is generally used when the user wants to create overlapping elements.

Syntax :

  • To get the property:
    object.style.zIndex;
  • To set the property:
    object.style.zIndex = "auto|number|initial|inherit"

Property Values:



  • auto: It is used to let the browser determine the stack order of the element.
  • number: It uses an integer that defines the stack order of the element.
  • initial: It is used to set the default value.
  • inherit: It is used to inherit property from its parent.

Below program illustrates the Style zIndex property :
Example: Changing the stack order of an <img> element.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>Style zIndex Property</title>
    <style>
        #MyImage {
            position: absolute;
            left: 200px;
            top: 80px;
            z-index: -1
        }
          
        h1 {
            color: green;
        }
          
        h2 {
            font-family: Impact;
        }
          
        body {
            text-align: center;
        }
    </style>
</head>
  
<body>
<center>
    <h1>GeeksforGeeks</h1>
    <h2>Style zIndex Property</h2>
  
    <img id="MyImage" src=
         width="200" 
         height="200">
  
    <button type="button" ondblclick="stack()">
      Bring Logo In The Front
    </button>
  
    <p>This is the logo of Geeksforgeeks.It has z-index as 0.</p>
    <br>
    <p>To bring the logo in the front,
    double-click the "Bring Logo In The Front" button.</p>
  
    <script>
        function stack() {
            document.getElementById("MyImage").style.zIndex = "1";
        }
    </script>
</center>
</body>
  
</html>

chevron_right


Output:

  • Before clicking the button:
  • After double clicking on the button:

Supported Browsers: The browser supported by HTML | DOM Style zIndex Property are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Apple Safari


My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.