0% completed
The z-index property controls the stacking order of positioned elements. It determines which element sits on top when elements overlap. This property is especially useful when using positioning (like absolute or fixed) to create complex layouts.
A higher z-index value means the element will be closer to the front, while a lower value means it will be further back. Remember, z-index only works on elements that have a position value other than static.
Follow the code block below to set z-index in CSS:
/* Set z-index for a positioned element */ z-index: value; /* e.g., z-index: 10; */
Explanation:
In this example, we create two overlapping <div> elements. We use z-index to control which element appears on top.
Explanation:
.box1):
z-index of 1, which places it behind Box 2..box2):
z-index of 2, so it appears in front of Box 1.z-index.In this example, we create a header, content area, and a floating sidebar. The sidebar overlaps the content. We use z-index to ensure the sidebar appears over the content but below the header.
Explanation:
.header):
position: fixed and a high z-index: 3 so it stays on top of other elements..sidebar):
z-index: 2 to overlap the content but remain below the header..content):
z-index: 1 so it stays behind the sidebar.This lesson shows how the z-index property is used to control the stacking order of overlapping elements. By adjusting the z-index values, you can manage which elements appear on top in your layout.
.....
.....
.....