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

"Utilize CSS Flexbox to create a layout with set width and uniform

Within a container, I have organized three sections. As I adjust the size of my browser to a maximum width of 668px, I want section 1 and section 3 to be displayed in one row while section 2 is placed below them. The width of section 2 should be proportion ...

Optimal method for integrating Google Font in GWT application

After successfully installing the Roboto font on my machine, I proceeded to include it in my welcome.html file by adding the following code: <link href='http://fonts.googleapis.com/css?family=Roboto:regular,medium,bold,thin' rel='stylesh ...

What causes jquery height and javascript height to both be returned as 0?

I'm facing an issue with a visible div on my screen - despite being visible, its height always returns as 0. I've attempted various jQuery and JavaScript methods to retrieve the height, but it consistently shows as 0. Here's the structure of ...

Is it possible to implement a custom radio tab index without using JavaScript

Is it possible to apply the tabindex attribute on custom radio buttons, hide the actual input element, and use keyboard shortcuts like Tab, Arrow Up, and Arrow Down to change the value? Check out this example on StackBlitz ...

Is it permissible to use DEFER or ASYNC when including a stylesheet?

I am curious if script files are able to utilize the DEFER and ASYNC keywords on a resource include. Are these keywords also compatible with stylesheet (CSS) includes? The syntax I imagine would be: <link rel="stylesheet" type="text/css" href="./path/ ...

Maintaining the image's aspect ratio while cropping it to fit within the maximum or minimum height

Is there a way to make an image responsive by resizing it while keeping its aspect ratio intact at all sizes? I want the height of the image to stay fixed above 650px, cropping the top and bottom to maintain the ratio without stretching. Additionally, I do ...

Enhancing a Bootstrap 4 navbar menu system with padding tweaks

Just starting out with Bootstrap 4 and creating a practice page with a header to familiarize myself with the navbar system. I have put together this jsFiddle (full code) for reference, but essentially, my page structure is as follows: index.html <nav ...

Tips for implementing border-radius on Internet Explorer 8:

Similar Question: How to create rounded borders in IE8 using CSS? I typically use css border-radius for the design of my webpages. While they display well in Firefox, I am facing compatibility issues with IE8. Can anyone offer any assistance? Thank yo ...

Incorporating a navigation bar at the top and a sidebar on the left side of the lower half of an HTML webpage

Currently utilizing materializecss and bootstrap, I am aiming to create a design that resembles: --------------------------------- --------------------------------- | | | | The objective is to split the page into two horizontal sections. The ...

Customize Cell Styling with Bootstrap Full Calendar CSS

I am attempting to implement a Bootstrap calendar feature where cells are colored green if the timestamp is greater than today's date. This can be achieved by: $checkTime > $today cell.css = green background I came across this code snippet on St ...

What is the best way to add three dots at the end of text when it exceeds the available space?

What CSS property is utilized to display three dots at the end of text when it overflows? ...

Eliminating excess space beneath nested images while adjusting the size without distorting the image

Struggling with getting an image to scale properly inside a nested div in a fluid layout. Looking for advice on how to prevent the image from becoming smaller than its parent div. EDIT: Here is an updated CodePen link demonstrating that using min-height s ...

The challenge of margin collapse

I am currently working on resolving a margin collapse issue. I am puzzled by the overlapping paragraphs and I really need to understand why this problem is happening. I suspect it may be a collapsing margin issue. Please take a look below: https://i.sta ...

Is there a way to prevent the letters from moving when I hover over them?

What occurs when the numbers are hovered over: https://gyazo.com/20b6426d435551c5ee238241d3f96b4d Whenever I hover over the pagination numbers, they shift to the right, and I am unsure of what mistake I made in my code that's causing this. Below, I ...

Issues with absolutely positioned slideshows

Currently, I am attempting to design a slideshow using absolute positioning to layer images on top of each other. However, I am encountering a problem where the text below the slideshow is also being stacked on top of the pictures. I have tried enclosing t ...

Can a child element be positioned above its parent's y-scrollbar using CSS?

I have created a selection box using <ul> and <li> elements. Some of the list items are wider than the parent box, so I used an :after pseudo-element to overlay them with full content when hovered over. Everything was working perfectly, until ...

I cannot seem to alter the background color of my image through the use of external CSS

body { background-color: #f6f7d4; } h1, h3, hr { color: #68b8ab; } hr { width: 100px; border-style: dotted none none; border-color: gray; border-width: 5px; } img { background-color: black; } Although the external CSS code a ...

Stop the page from scrolling

I'm trying to find a way to disable page scrolling, so I used this style for the body: overflow: hidden; Surprisingly, it worked perfectly on desktop but had no effect on mobile devices. After checking out this resource, it seems that creating a ch ...

HTML links have been disabled

I have been making updates to the charity website I manage, heroinitiative.org. You can view the revamp progress here: (please note that this is not live code, it is simply for my boss to see the progress and does not need to be kept secret, hence why I a ...

How to apply a custom CSS class to a Smarty tag in CMS Made Simple

Greetings! I am currently in the process of revamping a page using CMSmadesimple, However, I have limited knowledge when it comes to utilizing CMSms and Smartytags. The page contains various smartytags that require styling. One particular tag that needs ...