CSS: z-index

By Xah Lee. Date: . Last updated: .

What is Z Index

When a element goes into a layer, it may overlap with other elements. To control which goes in front, use the property z-index .

The z-index property is meaningful only if the element has one of:

Example

There are 2 overlapping boxes on this page, box A and B. Their positions are fixed relative to the window.

Both have position:fixed. One of them has larger z-index than the other, so it's in front.

BOX A
BOX B

Here is the code:

<div id="boxA3790">BOX A</div>

<div id="boxB7452">BOX B</div>
#boxA3790, #boxB7452 {
 width: 80px;
 height: 80px;
 position: fixed;
 color: black;
}

#boxA3790 {
 top: 300px;
 right: 20px;
 z-index: 5;
 background-color: salmon;
}

#boxB7452 {
 top: 320px;
 right: 0px;
 z-index: 6;
 background-color: gold;
}

CSS Position