Why Does z-index Not Work with Fixed Positioning in CSS ? - GeeksforGeeks (2024)

Last Updated : 11 Sep, 2023

Comments

Improve

In this article, we will see why does z-index not work with fixed positioning. The problem mainly is when an element has fixed positioning it doesn’t get under another element with default positioning with an even higher z-index.

This happens because when an element has a fixed positioning, it is removed from the document’s usual flow and placed relative to the browser window rather than the element it is contained within. A new stacking context is created for an element and its descendants when a fixed location is applied to it.

Example: In the example, we will see the h3 has a fixed positioning and the box has static positioning.

HTML

<!DOCTYPE html>

<html>

<head>

<title>

Offset a background image

from the right using CSS

</title>

<style>

h3 {

position: fixed;

z-index: 1;

}

.container {

padding: 2rem;

display: flex;

flex-direction: row;

}

.box {

width: 250px;

height: 250px;

border: 1px solid black;

justify-content: center;

}

.box1 {

bottom: 5rem;

background-color: rgb(247, 247, 247, 0.8);

z-index: 20;

margin-top: -2rem;

}

p {

margin: 0 15px;

padding: 100px 0px;

}

</style>

</head>

<body>

<h1 style="color: green">

GeeksforGeeks

</h1>

<h3>

Offset a background image

from the right using CSS

</h3>

<div class="container">

<div style="bottom: 2rem;">

<div class="box box1">

<p>Box 1 (This has static position)

<br>

(z-index = 20)

</p>

</div>

</div>

</div>

</body>

</html>

Output: In the above example, we have seen that the z-index of the box is higher than the h3 but due to the fixed positioning h3 is stacking out of that box.

Why Does z-index Not Work with Fixed Positioning in CSS ? - GeeksforGeeks (1)

Approach

To solve this error, we can set the position of the element which we want to be at the top to relative. We set to relative positioning because elements with relative positioning are positioned within their normal flow of the document and do not create a new stacking context. So when the position of the elements other than the fixed-positioned element is set to relative the z-index starts working regularly.

Syntax:

div{ 
position: relative;
/* Write other rules here */
}

Example 1: This example demonstrates the approach where we use the property position: relative to solve the z-index problem.

HTML

<!DOCTYPE html>

<html>

<head>

<title>

Offset a background image from

the right using CSS

</title>

<style>

h3 {

position: fixed;

z-index: 1;

}

.container {

padding: 2rem;

display: flex;

flex-direction: row;

}

.box {

width: 250px;

height: 250px;

border: 1px solid black;

justify-content: center;

}

.box1 {

bottom: 5rem;

background-color: rgb(247, 247, 247, 0.8);

z-index: 20;

margin-top: -2rem;

}

.box2 {

bottom: 2rem;

position: relative;

margin-left: 5rem;

z-index: 20;

background-color: rgb(247, 247, 247, 0.8);

}

p {

margin: 0 15px;

padding: 100px 0px;

}

</style>

</head>

<body>

<h1 style="color: green">

GeeksforGeeks

</h1>

<h3>

Offset a background image from

the right using CSS

</h3>

<div class="container">

<div style="bottom: 2rem;">

<div class="box box1">

<p>Box 1 (This has static position)

<br>

(z-index = 20)

</p>

</div>

</div>

<div>

<div class="box box2">

<p>Box 2 (This has relative position)

<br>

(z-index = 20)

</p>

</div>

</div>

</div>

</body>

</html>

Output:

Why Does z-index Not Work with Fixed Positioning in CSS ? - GeeksforGeeks (2)

In the above example, <h3> having the title of the article has a fixed positioning and the first box has default static positioning and the second box has relative positioning. You can see that despite having the same z-index(greater than that of the <h3>) the first box is staying behind where the second one stays over the <h3>:

Example 2: The example demonstrates how the property position: relative to solves the z-index problem even when the elements are added inside a flexbox.

HTML

<!DOCTYPE html>

<html>

<head>

<title>

Why does z-index not work

with fixed positioning?

</title>

<style>

.container {

padding: 2rem;

display: flex;

flex-direction: row;

}

.box {

width: 150px;

height: 150px;

border: 1px solid black;

background-color: rgb(247, 247, 247, 0.8);

}

.box-m {

position: fixed;

z-index: 1;

margin-left: 8rem;

background-color: rgba(58, 164, 0, 0.8);

color: white;

}

.box1 {

z-index: 20;

}

.box2 {

position: relative;

margin-left: 5rem;

z-index: 20;

}

p {

margin: 0 15px;

padding: 50px 0px;

}

</style>

</head>

<body>

<h1 style="color: green">

GeeksforGeeks

</h1>

<h3>

Why does z-index not work

with fixed positioning?

</h3>

<div>

<div class="box box-m">

This Box is outside Flexbox.

<br>

<br>

Fixed position

</div>

</div>

<div class="container">

<div style="bottom: 2rem;">

<div class="box box1">

<p>Box 1 (This has static position)

<br>

(z-index = 20)

</p>

</div>

</div>

<div>

<div class="box box2">

<p>Box 2 (This has relative position)

<br>

(z-index = 20)

</p>

</div>

</div>

</div>

</body>

</html>

Output:

Why Does z-index Not Work with Fixed Positioning in CSS ? - GeeksforGeeks (3)



P

priyanshuchatterjee01

Why Does z-index Not Work with Fixed Positioning in CSS ? - GeeksforGeeks (4)

Improve

Next Article

How CSS Positioning and Flexbox Work?

Please Login to comment...

Why Does z-index Not Work with Fixed Positioning in CSS ? - GeeksforGeeks (2024)

FAQs

Does the z-index work with position fixed? ›

Note: z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky) and flex items (elements that are direct children of display:flex elements).

Why is the z-index not working in CSS? ›

This is because browsers paint positioned elements before non-positioned elements. This means that our relative positioned element is painted first and seems to take priority in the z layer over non-positioned elements.

Does z-index apply to static position? ›

To control an element with the z-index property, the element must have a value for position that is not static (the default). z-index will apply to elements with a position of relative, fixed, absolute, or sticky.

Can I use z-index with position absolute? ›

z-index only works for positioned elements

If you want to control the stacking order of elements, you can do so with z-index . But z-index will only take affect if the element also has a position value of absolute , relative or fixed .

What is the z-index for fixed navbar? ›

z-index: 9999; An unusually high z-index value is used to significantly reduce the possibility that an HTML element is rendered on top of the fixed navigation bar, as long as there are no other z-index values higher than 9999 . That's all there is to it.

What is the purpose of z-index in CSS positioning? ›

Z Index ( z-index ) is a CSS property that defines the order of overlapping HTML elements. Elements with a higher index will be placed on top of elements with a lower index. Note: Z index only works on positioned elements ( position:absolute , position:relative , or position:fixed ).

What is the best practice of using z-index in CSS? ›

If you want to create a custom stacking order, you can use the z-index property on a positioned element. Note: When no z-index property is specified, elements are rendered on the default rendering layer (Layer 0).

What is the limit of z-index in CSS? ›

The maximum range is ±2147483647. In CSS code bases, you'll often see z-index values of 999, 9999 or 99999. This is a perhaps lazy way to ensure that the element is always on top. It can lead to problems down the road when multiple elements need to be on top.

What does z-index 0 mean in CSS? ›

z-index:0 is always the "default layer" (the layer in which all elements without an explicit z-index reside), and z-index:auto means: "Sets the stack order equal to its parent".

Why doesn't position sticky work? ›

If the content within the scroll container overflows, the sticky element may not stick as expected. This can happen if the content inside the scroll container extends beyond its boundaries. To fix this issue, ensure that the scroll container has an appropriate height and that its content does not overflow.

Can CSS z-index be negative? ›

CSS z-index property

The z-index sets the stack order of the element and negative numbers are allowed. It is your responsibility to set the z-index in the way that you want your elements to appear.

Does z-index only effects elements that have a position value other than? ›

z-index only affects elements that have a position value other than static (the default). Elements can overlap for a variety of reasons, for instance, relative positioning has nudged it over something else.

Does z-index work with fixed position? ›

In this article, we will see why does z-index not work with fixed positioning. The problem mainly is when an element has fixed positioning it doesn't get under another element with default positioning with an even higher z-index.

Which position does the z-index not work with? ›

z-index only works on positioned elements (relative, absolute, fixed, sticky) so if you set a z-index on an element with a static position, it won't work.

What happens if two absolutely positioned elements overlap and neither has a specified z-index? ›

If no value for z-index is set, the browser will use the document source order to dictate z-index instead. This demo has 3 empty <div> elements, with negative margin, making them overlap. The later elements sit on top of the earlier elements.

Does position absolute work with position fixed? ›

An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed). However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.

When should I use z-index? ›

If you want to create a custom stacking order, you can use the z-index property on a positioned element. Note: When no z-index property is specified, elements are rendered on the default rendering layer (Layer 0).

References

Top Articles
Latest Posts
Article information

Author: Zonia Mosciski DO

Last Updated:

Views: 6302

Rating: 4 / 5 (71 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Zonia Mosciski DO

Birthday: 1996-05-16

Address: Suite 228 919 Deana Ford, Lake Meridithberg, NE 60017-4257

Phone: +2613987384138

Job: Chief Retail Officer

Hobby: Tai chi, Dowsing, Poi, Letterboxing, Watching movies, Video gaming, Singing

Introduction: My name is Zonia Mosciski DO, I am a enchanting, joyous, lovely, successful, hilarious, tender, outstanding person who loves writing and wants to share my knowledge and understanding with you.