What is the best way to set the z-index for elements within CSS containers that are utilized for container queries?

I am thrilled about the introduction of container queries in CSS. It's great to see this feature now available across all browsers. However, I have encountered an issue when working with multiple consecutive containers and placing an absolutely positioned z-indexed element inside one of them - the following container ends up covering this element.

Here is an example of my case: z-indexed-menu covered

  1. The menu (position: absolute, z-index: 9) belongs to the dark blue box with container-type: inline-size
  2. It gets covered by the light blue box, which also has container-type: inline-size

For a more detailed look at the issue:

.block {
  display: inline-block;
  background-color: darkblue;
  color: lightblue;
  margin: 2px;
  width: 100%;
}

.block.container {
  container-type: inline-size;
}

.popout {
  background-color: darkgreen;
  color: lightgreen;
  position: absolute;
  z-index: 9;
}

hr {
  margin-bottom: 4rem;
}
<div class="block">
  <p>Some content in regular inline-block</p>
  <div class="popout">
    <p>Popping out of it.</p>
    <p>Popping out of it.</p>
    <p>Popping out of it.</p>
  </div>
</div>
<div class="block">
  <p>Some content in regular inline-block</p>
</div>
<hr />
<div class="block container">
  <p>Some content in block with
    <pre>container-type: inline-size;</pre>
  </p>
  <div class="popout">
    <p>Popping out of it.</p>
    <p>Popping out of it.</p>
    <p>Popping out of it.</p>
  </div>
</div>
<div class="block container">
  <p>Some content in block with
    <pre>container-type: inline-size;</pre>
  </p>
</div>

https://codepen.io/gofix/pen/yLREXaN

My question is, how can I apply container-type without creating a new kind of absolute layout context?

I attempted to give the elements with container-type: inline-size a lower z-index, which somewhat resolved the issue, but I prefer not to clutter my page with numerous z-index values.

Answer №1

To resolve your issue, consider rearranging the order of your blocks and enclosing them in a

<div style="display: flex; flex-direction: column-reverse;">
.

<div style="display: flex; flex-direction: column-reverse;">
  <div class="block container">
    <p>Content within a block with
      <code>container-type: inline-size;</code>
    </p>
  </div>
  <div class="block container">
    <p>Content within a block with
      <code>container-type: inline-size;</code>
    </p>
    <div class="popout">
      <p>Popping out.</p>
      <p>Popping out.</p>
      <p>Popping out.</p>
    </div>
  </div>
</div>

If this solution does not meet your needs, you may need to acknowledge how subsequent stacking contexts are displayed and move elements that should render outside the block's borders out of the stacking context, for example:

<div class="block">
  <div class="container">
    <p>Content within a block with
      <code>container-type: inline-size;</code>
    </p>
  </div>
  <div class="popout">
    <p>Popping out.</p>
    <p>Popping out.</p>
    <p>Popping out.</p>
  </div>
</div>

(please update the CSS selector from .block.container to .block > .container)

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

Centering an image that is absolutely positioned within a container that is relatively positioned, horizontally

Looking to center an image that is positioned absolutely horizontally within a container that is relatively positioned. After attempting with CSS and not finding success, I ended up using Jquery. http://jsfiddle.net/CY6TP/ [This is my attempt using Jquery ...

Is there a way to create a soft light blue backdrop for text using HTML and CSS?

Is there a way to create a light blue background effect behind text using HTML and CSS? You can view the image reference here ...

Leveraging Angular to dynamically adjust the height of ng-if child elements based on parent

I'm struggling with a few things in my current setup. I have a view that consists of 3 states - intro, loading, and completed. My goal is to create a sliding animation from left to right as the user moves through these states. Here is the basic struc ...

Design a unique border for a div element that extends a top-border all the way through the header and a bottom-border that

I am trying to achieve the design displayed in this mockup: Here is my current progress: https://codepen.io/raney24/pen/VOmjBY .header-border { width: 100%; margin-bottom: 10px; border-left: red solid 1px; border-right: red solid 1px; border- ...

Exploring the possibilities of using rem, em, and px for setting image dimensions

I am in need of clarification on the use of different units for image dimensions. Despite my research, I have not been able to find a consistent answer. Some sources suggest using only rem/em instead of pixels, but I am unsure if this also applies to image ...

When the enter key is pressed in a contenteditable div, line breaks are disregarded

Is there a way to ensure that when the return key is pressed to start a new line for a post, the output actually starts on a new line? I've been having trouble with this and would like to know the most common solution. Check out the demo below for te ...

Having trouble getting the jQuery click event to work on iPhone browsers? Despite already trying the cursor:pointer; method

I am facing an issue with setting a click event on anchor tags in my WordPress site. Surprisingly, the event works perfectly fine on all of my devices including PC, Android phone, and tablet except for my iPhone. Despite trying to set the 'Cursor&apo ...

How to toggle two classes simultaneously using JQuery

Check out this code snippet: http://jsfiddle.net/celiostat/NCPv9/ Utilizing the 2 jQuery plugin allows for the following changes to be made: - The background color of the div can be set to gray. - The text color can be changed to red. The issue arises wh ...

Disappearing Into the Background Excluding Specific Divs

I have a dilemma with fading in and out a background image using jQuery fadeIn and fadeOut. The issue arises because my wrapper div contains elements such as sidebar and navigation that are positioned absolutely within the wrapper div, causing them to also ...

CSS uses color parameters in the same way

Apologies, I spent a few years immersed in the world of IT. Now, onto my query: I have a lengthy ".css" file with numerous structures like color: #567567; within it. Is there a way to utilize a construction similar to color: $mycolor or is this not po ...

Displaying block in front of flex-items

I have been trying to figure out how to get the red box to display in front of the other flex items, but none of my attempts seem to work. .box { font-size: 2.5rem; width: 100px; line-height: 100px; text-align: center; } .front .box { margin: ...

"Enhancing Website Styling with Twitter Bootstrap's Border

Recently delving into the realm of Twitter Bootstrap, I find myself pondering on the best approach to incorporate a border around a parent element. Consider this scenario: <div class="main-area span12"> <div class="row"> <div cl ...

The content has spilled over from the div and leaked into the adjacent div

When the left div's content exceeds the right div's, it spills over into the next container div. Sample HTML: <div class="musictemplate_container"> <div class="musictemplate_left"> something<br> omething< ...

What is the best way to keep a div element fixed at the top of a webpage?

I am facing an issue with a div that contains three SVG files layered on top of each other and meant to be centered on the page. Ideally, when the user scrolls down, I want these SVG files to stick to the top and remain there. In my CSS attempts, I have u ...

Adjust HTML table cell height to dynamically match content and align to top

I am currently working on creating a table layout where the left column adjusts its height based on content, while the right column needs to have a fixed height of 300. <table border="1" cellspacing="0" cellpadding="0"> <tr> <td>&a ...

Displaying text on hover and showing a text box when a link is clicked using CSS and JavaScript

Hovering over the text will display "Edit," and clicking on it will reveal a text box. This function will be used to update content using ajax. HTML: <table> <thead> <tr> <th> Name </th> <th> Age </th> ...

Challenges with Implementing Background Images on my Website with HTML and CSS

Being new to the world of coding, I recently created a website for a college presentation. However, after uploading the website onto the internet, I noticed that the background images from my files are not displaying on the website. It's likely that I ...

The disappearance of IE7 floats is observed when their parent element is styled with position:relative

The issue I'm encountering in IE7 is related to the CSS properties position:relative;, float: right;, and float: left;. While this problem doesn't seem to occur in newer browsers, it's puzzling why position:relative; impacts the behavior of ...

What are the steps to incorporating the League Gothic font into my website design?

Is there a way to incorporate League Gothic font into my website design? Visit the League Gothic GitHub repository Appreciate any guidance on this matter, ...

The table spills over the container and disappears underneath the navbar

After downloading a template table from the internet and customizing it to my liking, I encountered an issue where it does not adhere to the assigned settings (as shown in the image), but that's not the only problem. The dropdown navbar on my website ...