Why Doesn't z-index Work with Fixed Positioning Elements?
z-index Not Working with Fixed Positioning: Unraveling the Reason
Fixed positioning is a powerful tool for creating elements that remain in place on the screen regardless of scrolling. However, it can sometimes interfere with the z-index property, which controls the stacking order of elements.
To illustrate, consider the following code snippet:
#over { width: 600px; z-index: 10; } #under { position: fixed; top: 5px; width: 420px; left: 20px; border: 1px solid; height: 10%; background: #fff; z-index: 1; }
As you would expect, the #over element should appear above the #under element since it has a higher z-index. However, in this scenario, #under still overlays #over. Why is this happening?
The answer lies in the default positioning of #over. By default, elements have static positioning, which means they occupy their natural position in the document flow. Fixed positioning elements, on the other hand, are removed from the normal flow and placed at specific coordinates relative to the page'sviewport.
In this case, the fixed position of #under takes it out of the normal flow, and therefore, its z-index becomes irrelevant to elements with static positioning. To make #under appear behind #over, we need to set the positioning of #over to relative. This will place #over in the document flow but allow it to shift its position according to its specified z-index.
Here's the fixed code snippet:
#over { width: 600px; z-index: 10; position: relative; } #under { position: fixed; top: 14px; width: 415px; left: 53px; border: 1px solid; height: 10%; background: #f0f; z-index: 1; }
Now, the #under element appears underneath #over as expected. Remember, for z-index to work properly, elements must be either absolutely or relatively positioned.
The above is the detailed content of Why Doesn't z-index Work with Fixed Positioning Elements?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

CSS Grid is a collection of properties designed to make layout easier than it’s ever been. Like anything, there's a bit of a learning curve, but Grid is

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference
