CSS Interview Questions
HTML & CSS
Web DevelopmentFrontendQuestion 14
What is the purpose of the z-index property, and how does it work?
Answer:
Purpose of the z-index Property
The z-index property in CSS is used to control the stacking order of elements that overlap. It determines which element appears in front of or behind other elements when they occupy the same space on the page. The z-index property only works on elements that have a position value other than static (i.e., relative, absolute, fixed, or sticky).
How z-index Works
-
Stacking Context:
- Elements on a web page are rendered in a stacking context, which is a three-dimensional conceptualization of how elements are layered on top of one another along the z-axis (which is perpendicular to the screen).
- Each stacking context is independent of other stacking contexts. Changes to
z-indexwithin one stacking context do not affect elements in a different stacking context.
-
The
z-indexValue:- The
z-indexproperty accepts integer values (both positive and negative) and determines the stacking order of elements within the same stacking context. - Elements with a higher
z-indexvalue are positioned in front of elements with a lowerz-indexvalue. - If two elements have the same
z-indexvalue, their stacking order is determined by their order in the HTML document; elements that appear later in the document are stacked on top.
- The
-
Default Stacking Order:
- By default, elements with no
z-indexspecified (or withz-index: auto) are stacked in the order they appear in the DOM, with later elements appearing on top.
- By default, elements with no
Example of z-index in Action
Here’s a simple example demonstrating how z-index works:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.box {
position: absolute;
width: 100px;
height: 100px;
color: white;
text-align: center;
line-height: 100px;
}
.box1 {
background-color: red;
top: 50px;
left: 50px;
z-index: 1; /* This box will be behind box2 and box3 */
}
.box2 {
background-color: green;
top: 100px;
left: 100px;
z-index: 2; /* This box will be between box1 and box3 */
}
.box3 {
background-color: blue;
top: 150px;
left: 150px;
z-index: 3; /* This box will be on top of box1 and box2 */
}
</style>
<title>Z-Index Example</title>
</head>
<body>
<div class="box box1">Box 1</div>
<div class="box box2">Box 2</div>
<div class="box box3">Box 3</div>
</body>
</html>
Explanation:
- Box 1 (
z-index: 1): Positioned at the lowest stacking level, this box will appear behind Box 2 and Box 3. - Box 2 (
z-index: 2): Positioned between Box 1 and Box 3, this box will overlap Box 1 but be behind Box 3. - Box 3 (
z-index: 3): Positioned at the highest stacking level, this box will appear in front of both Box 1 and Box 2.
Important Points About z-index:
-
Positioned Elements: The
z-indexproperty only works on elements that are positioned (position: relative,absolute,fixed, orsticky). If an element hasposition: static,z-indexhas no effect. -
Stacking Contexts: A new stacking context is created whenever an element has a
positionvalue other thanstaticand az-indexvalue other thanauto. Stacking contexts can also be created by other properties, such asopacityless than 1,transform,filter,perspective,clip-path, etc. -
z-index and Negative Values: Negative
z-indexvalues are allowed, which will place an element behind other elements with az-indexof0or greater. An element with a negativez-indexcan still be visible if no other elements cover it. -
Inheritance: The
z-indexvalue is not inherited by child elements. Child elements are positioned within the stacking context of their nearest positioned ancestor.
Summary
- The
z-indexproperty controls the vertical stacking order of elements on a web page. - Elements with a higher
z-indexvalue appear in front of elements with a lowerz-indexvalue within the same stacking context. - The
z-indexproperty only affects elements that are positioned (relative,absolute,fixed, orsticky). - Understanding
z-indexand stacking contexts is crucial for managing the layering of overlapping elements, especially in complex layouts.