Home
CSS
CSS Z-index
Tryit: z-index example
Run ❯
Get your
own
website
Result Size:
625 x 534
×
Change Orientation
Save Code
Change Theme, Dark/Light
Go to Spaces
<!DOCTYPE html> <html> <head> <style> .container { position: relative; } .black-box { position: relative; z-index: 1; border: 2px solid black; height: 100px; margin: 30px; } .gray-box { position: absolute; z-index: 3; /* gray box will be above both green and black box */ background: lightgray; height: 60px; width: 70%; left: 50px; top: 50px; } .green-box { position: absolute; z-index: 2; /* green box will be above black box */ background: lightgreen; width: 35%; left: 270px; top: -15px; height: 100px; } </style> </head> <body> <h1>Z-index Example</h1> <p>An element with greater stack order is always above an element with a lower stack order.</p> <div class="container"> <div class="black-box">Black box (z-index: 1)</div> <div class="gray-box">Gray box (z-index: 3)</div> <div class="green-box">Green box (z-index: 2)</div> </div> </body> </html>