I'd love some clarity on the concept of stacking contexts

After encountering a bug with a non-clickable video player inside a jquery ui dialog, I resolved the issue by changing position:relative; to position:inherit;

Other solutions included removing position:relative; altogether or adjusting the z-index of the player class to something other than 1.

Upon further reading, it became clear that these changes impacted the stacking context and ultimately fixed the problem. However, I still lack a full understanding of what was happening in my specific scenario or how stacking contexts function in general. Can anyone provide additional examples or suggestions to shed light on this?

<div class="player"> 
    <div id="videoPlayer_wrapper" style=" position: relative; width:580px; height: 192px;">
        <object type="application/x-shockwave-flash" data="/flash/player.swf" width="100%" height="100%" bgcolor="#000000" id="videoPlayer" name="videoPlayer" tabindex="0">
        </object>
    </div> 
</div>

The CSS for player is as follows:

.player {
    margin-bottom: 20px;
    position: relative;
    z-index: 1;
}

Answer №1

Phillip Walton's article on stacking contexts was incredibly insightful and helped me immensely in understanding the concept. I embarked on a debugging journey of my own issue which led me to explore this topic further.

It's worth noting that the pink square with z-index: 100; is positioned below the light blue square with z-index: 1; due to its placement within a stacking context created by the .A class using the transform property.

https://i.sstatic.net/hQoIh.png

If you'd like to experiment with this yourself, check out this jsbin link which provides an easier platform for testing than inline code on SO: https://jsbin.com/lataga/2/edit?css,output

div {
  width: 200px;
  height: 200px;
  padding: 1rem;
}

.A {
  position: absolute;
  background-color: red;

  /*
    Adding a transform here creates a
    new stacking context for the children of .A
    */
  transform: translateX(0);

  /*    
    Other properties like opacity < 1 can also trigger creation of stacking context
    */
  /*    opacity: 0.99; */

  /*
    Uncomment the following z-index value to see how raising .A affects child elements.
    */
  /*    z-index: 3; */
}

.a {
  position: relative;

  /*
    Even a much higher z-index can't lift .a above .b when constrained within a lower stacking context
    */
  z-index: 100;

  margin-left: 125px;
  background-color: pink;
}

.B {
  position: absolute;
  margin-top: 75px;
  /*    z-index: 2; */
  background-color: blue;
}

.b {
  margin-left: 50px;
  background-color: lightblue;

  /*
    Explicitly specifying z-index, even though not necessary due to natural stacking order rules
    */
  z-index: 1;
}
<div class="A">
  A: 1
  <div class="a">a: 1.100</div>
</div>
<div class="B">
  B: 2
  <div class="b">b: 2.1</div>
</div>


Understanding Stacking Contexts

  • When positioning an HTML element or assigning a z-index value, a stacking context is created (also happens when non-full opacity is set).
  • Stacking contexts can be nested within each other, forming a hierarchy of stacking contexts.
  • Each stacking context operates independently: only descendant elements are part of the stacking process.
  • Once stacked, the entire element is considered within the parent stacking context's ordering.

Note: The stacking context hierarchy is a subset of the HTML elements' hierarchy as only specific elements create their stacking contexts. Elements without their stacking contexts are integrated into the parent stacking context.

Find more information here


In Simple Terms:

Every stacking context originates from a single HTML element. Once established, it organizes all child elements within a particular section of the stacking order. This means that an element contained in a low-level stacking context cannot appear in front of another element in a higher-level stacking context, irrespective of any z-index values!

...

[...] Besides opacity, several modern CSS properties also form stacking contexts. These include transformations, filters, CSS regions, paged media, and potentially others. Essentially, if a CSS property necessitates offscreen rendering, it must initiate a new stacking context.

More insights here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using CSS files in the web directory in CakePHP and connecting them to HTML

Could someone offer some help with cakephp? I'm experiencing an issue related to routes that I can't seem to resolve. I've organized all my css files in webroot/css within my app. In my default layout, I included a html link <link href=" ...

Create a CSS path that connects two points by drawing a line

My image is currently animating along a path, but I want to make the path itself visible. Here's the CSS code: div { width:10px; height:10px; background:red; position:relative; -webkit-animation-name:Player1; -webkit-animatio ...

What is the best way to reference the className within nested SCSS when working with Next.js and React.js?

I am working on customizing a navbar that includes a hamburger button. My goal is to change the style, specifically the bar color and the appearance of the hamburger button, upon tapping. I have already attempted using &:active and .activeBar in the SCSS f ...

Positioning an HTML control on top of a Silverlight Application, ensuring it moves in sync with the application as it scrolls

   I am facing a challenge with my Silverlight Application (VS2010/C#) that runs in full screen mode.    There is also an HTML control positioned over the Silverlight Application.    When I maximize the brows ...

Tips for aligning text vertically in flexbox arrangements

As I work on building my portfolio, I have implemented links as buttons using flexbox to make responsiveness easier. The height of these flexboxes is set dynamically using JavaScript with percentages. Despite trying various suggestions, none of them provi ...

Is there a way to create rectangles with a designated percentage of blue color using CSS or Bootstrap 3?

There's an image on my laptop that perfectly illustrates what I mean, but unfortunately, I'm not sure how to share it with you. In essence, I am looking to create a rectangle filled with a specific percentage of the color blue. Could you assist m ...

Sibling proximity: an excess of elements separating the regulations

Is there a way to make a hidden p element appear when hovering over the first visible element in a tree structure? I found some help on Stack Overflow suggesting the use of adjacent sibling selectors, and while it works well with a simple example, it fails ...

Creating a dynamic animation for a navigation bar's underline

I'm attempting to add a smooth underline effect to the links in my navbar when they are hovered over. Unfortunately, the entire ul element is being underlined instead of just the selected link. Is there a way to resolve this issue? a { text-deco ...

Connect jQuery's resizable controls

Check out this jSFiddle. I am attempting to add event handlers for resizing $('oWrapper_'+num). However, the resizing does not occur. This is because $('#oWrapper_'+num) has not been added to the dom at the time of execution, so the se ...

How to verify if a node contains plaintext with jsoup?

Is there a way to determine if a node in jsoup has plain text without including link text, instead of using the Element.hasText() method? Any suggestions would be greatly appreciated. Thank you ...

After transitioning to TypeScript, the CRA app is encountering issues loading CSS files compiled by SASS

My React application was originally created using create-react-app and I had been successfully loading scss files with the node-sass package. However, after deciding to switch to TypeScript following the steps outlined in , my css files are no longer being ...

The form is functioning properly on mobile devices but is currently experiencing issues on the server

Everything runs smoothly when accessing the website and using the form on localhost. However, once it's uploaded to a server, the form only functions correctly on desktop devices. On mobile, the form fails to filter and displays all professionals inst ...

implementing jquery typewriter plug-in for a responsive bootstrap image

I am using Bootstrap and my image response dynamically, but I am having trouble adding text to it. I would like to use a jQuery typewriter plugin for the text on the image, which should resize according to screen size. Here is my HTML code and I have used ...

Learn the best practices for incorporating jQuery and other JavaScript libraries in your Angular2 projects

Is it possible to integrate a demo showcasing Bootstrap carousel with CSS3 animations in Angular2 using HTML, CSS, and JS? I have created my own implementation in Plunker with Angular2, but I am facing issues with the animated inner content of the carousel ...

Issue with JavaScript ScrollTop

Simply put, I am trying to determine the total scroll size for a text area in a unit that scrollTop can align with. However, I am at a loss on how to do so. I've tried scrollHeight and various other methods without success. Any advice or suggestions ...

CSS Float issue on resizing: avoid element displacement due to margin or padding (responsive layout)

Having an issue that I need help with: My header includes a menu, news sliding with flexslider, and a search input: The problem occurs when I resize the browser manually or reload it with a smaller width; the right element jumps down, causing a pushdown ...

Steer clear of using absolute positioning in Material-UI and React for better styling and layout

Is there a way to achieve the same layout without using position: absolute for the "Hello There" button? I want to remove that CSS property while keeping the design intact. Any better suggestions? Feel free to check out my codesandbox: CODESANDBOX -----&g ...

What is the reason behind the issue where max-height disrupts border-radius?

My inline svg is styled with the following CSS: .img { border-radius: 15px; overflow: hidden; max-width: 70vw; margin-top: 6vh; max-height: 50vh; z-index: -1; text-align: center; } Everything seems to work as expected, except f ...

Stylish CSS: Jagged 3-dimensional rotated text

Have a look at this Codepen to see the issue. As the text moves into the background, more aliasing appears. A screenshot taken in Chrome on macOS (similar appearance in FF & Safari): https://i.sstatic.net/n746I.png I've experimented with different ...

CSS - Maximizing One Column's Width While Allocating Percentage Space for Two Columns?

It's been quite some time since I delved into the world of web development and I seem to have forgotten how to tackle this particular issue. Despite hours spent searching online, I've gained knowledge about flex-boxes and other useful tools, but ...