Introduction

Z-index is used for serves to stabilize the order of elements that are in the overlap.
Z-Index is an important property of CSS. The z-index property specifies the stack order of an element and its descendants. The z-index property in CSS controls the vertical stacking order of elements that overlap. When elements overlap, z-order determines which one covers the other.
push pop
An element with a greater stack order is always in front of an element with a lower stack order. An element with a larger z-index generally covers an element with a lower one. The “Stack Order” refers to the element’s position on the Z-axis (as opposed to the X-axis or Y-axis). A higher z-index value means the element will be closer to the top of the stacking order.
Z- index Syntax
Z- index: auto
Z- index: integer number
Z- index: negative number
Z- index: inherit
Example
The following is the HTML code I used for this tutorial.
HTML: Structure
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Z-Index tutorial</title>
  5. </head>
  6. <body>
  7. <div id="one">One</div>
  8. <div id="two">Two</div>
  9. <div id="Three">Three</div>
  10. <div id="Four">Four</div>
  11. <div id="Five">Five</div>
  12. </body>
  13. </html>
Use CSS code to make them look and feel for all the HTML elements as we used before in the HTML part. The following is the CSS code I have used in this tutorial.
CSS: Rule
  1. #
  2. one {
  3. border: solid 5 px silver;
  4. background - color: Aqua;
  5. position: absolute;
  6. z - index: 1;
  7. opacity: 0.5;
  8. height: 100 px;
  9. width: 100 px;
  10. }#
  11. two {
  12. border: solid 5 px silver;
  13. background - color: Green Yellow;
  14. position: absolute;
  15. top: 30 px;
  16. left: 35 px;
  17. z - index: 2;
  18. opacity: 0.5;
  19. height: 100 px;
  20. width: 100 px;
  21. }
  22. #
  23. Three {
  24. border: solid 5 px silver;
  25. background - color: Coral;
  26. position: absolute;
  27. top: 60 px;
  28. left: 60 px;
  29. opacity: 0.5;
  30. z - index: 3;
  31. height: 100 px;
  32. width: 100 px;
  33. }
  34. #
  35. Four {
  36. border: solid 5 px silver;
  37. background - color: Yellow;
  38. position: absolute;
  39. top: 90 px;
  40. left: 90 px;
  41. opacity: 0.5;
  42. z - index: 4;
  43. height: 100 px;
  44. width: 100 px;
  45. }
  46. #
  47. Five {
  48. border: solid 5 px silver;
  49. background - color: MediumSpringGreen;
  50. position: absolute;
  51. top: 120 px;
  52. left: 120 px;
  53. opacity: 0.5;
  54. z - index: 5;
  55. height: 100 px;
  56. width: 100 px;
  57. }
Complete code
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Z-Index tutorial</title>
  5. <style>
  6. #one {
  7. border: solid 5px silver;
  8. background-color: Aqua;
  9. position: absolute;
  10. z-index: 1;
  11. opacity: 0.5;
  12. height: 100px;
  13. width: 100px;
  14. }
  15. #two {
  16. border: solid 5px silver;
  17. background-color: Green Yellow;
  18. position: absolute;
  19. top: 30px;
  20. left: 35px;
  21. z-index: 2;
  22. opacity: 0.5;
  23. height: 100px;
  24. width: 100px;
  25. }
  26. #Three {
  27. border: solid 5px silver;
  28. background-color: Coral;
  29. position: absolute;
  30. top: 60px;
  31. left: 60px;
  32. opacity: 0.5;
  33. z-index: 3;
  34. height: 100px;
  35. width: 100px;
  36. }
  37. #Four {
  38. border: solid 5px silver;
  39. background-color: Yellow;
  40. position: absolute;
  41. top: 90px;
  42. left: 90px;
  43. opacity: 0.5;
  44. z-index: 4;
  45. height: 100px;
  46. width: 100px;
  47. }
  48. #Five {
  49. border: solid 5px silver;
  50. background-color: MediumSpringGreen;
  51. position: absolute;
  52. top: 120px;
  53. left: 120px;
  54. opacity: 0.5;
  55. z-index: 5;
  56. height: 100px;
  57. width: 100px;
  58. }
  59. </style>
  60. </head>
  61. <body>
  62. <div id="one">One</div>
  63. <div id="two">Two</div>
  64. <div id="Three">Three</div>
  65. <div id="Four">Four</div>
  66. <div id="Five">Five</div>
  67. </body>
  68. </html>
Output
result

Conclusion

I hope you liked this useful article. It will be useful for HTML and CSS beginners. Please provide your valuable feedback and suggestions.
Live demo link: Jsfiddle
References