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)
Top Articles
‘Love After Lockup’ Franchise Expanding With New Spinoff ‘Love During Lockup’ (EXCLUSIVE)
Love During Lockup Season 5 Premiere - Meet The Couples! - Word on the Street Reality
Victory Road Radical Red
Dannys U Pull - Self-Service Automotive Recycling
Genesis Parsippany
Occupational therapist
Konkurrenz für Kioske: 7-Eleven will Minisupermärkte in Deutschland etablieren
BULLETIN OF ANIMAL HEALTH AND PRODUCTION IN AFRICA
Teamexpress Login
Walgreens Alma School And Dynamite
Mndot Road Closures
Weather In Moon Township 10 Days
Hillside Funeral Home Washington Nc Obituaries
Help with Choosing Parts
People Portal Loma Linda
Busted Newspaper S Randolph County Dirt The Press As Pawns
Buff Cookie Only Fans
Katherine Croan Ewald
Vipleaguenba
Bridge.trihealth
Indiana Wesleyan Transcripts
2024 INFINITI Q50 Specs, Trims, Dimensions & Prices
Football - 2024/2025 Women’s Super League: Preview, schedule and how to watch
Www.dunkinbaskinrunsonyou.con
8005607994
Aliciabibs
104 Presidential Ct Lafayette La 70503
Victory for Belron® company Carglass® Germany and ATU as European Court of Justice defends a fair and level playing field in the automotive aftermarket
Best Middle Schools In Queens Ny
Kimoriiii Fansly
Himekishi Ga Classmate Raw
The Procurement Acronyms And Abbreviations That You Need To Know Short Forms Used In Procurement
Ups Drop Off Newton Ks
Myaci Benefits Albertsons
Redbox Walmart Near Me
First Light Tomorrow Morning
Rise Meadville Reviews
Devin Mansen Obituary
The best Verizon phones for 2024
Lonely Wife Dating Club בקורות וחוות דעת משתמשים 2021
Miami Vice turns 40: A look back at the iconic series
Best GoMovies Alternatives
R: Getting Help with R
Fedex Passport Locations Near Me
Embry Riddle Prescott Academic Calendar
Skyward Cahokia
Matt Brickman Wikipedia
Yosemite Sam Hood Ornament
Fine Taladorian Cheese Platter
Used Auto Parts in Houston 77013 | LKQ Pick Your Part
Wrentham Outlets Hours Sunday
Unit 4 + 2 - Concrete and Clay: The Complete Recordings 1964-1969 - Album Review
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 5741

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.