CSS: nested sticky element contained within an absolute element

I am facing an issue with a table that has a sticky column, and the absolute menu within it is appearing behind the sticky column instead of in front. I have tried adjusting the z-index property but it didn't work. Can someone please assist me with this? (Apologies for my poor English)

My codepen: codepen

.sticky {
  position: sticky;
  right: 0;
  background-color: yellow;
}
.menu {
  position: absolute;
  left: 0;
  background-color: red;
  z-index: 1;
}

body {
  margin: 0;
}

.wrapper {
  overflow-x: auto;
  position: relative;
  white-space: nowrap;
  max-width: 600px;
}
th,
td {
  min-width: 300px;
  text-align: center;
}
th:last-child,
td:last-child {
  min-width: 50px !important;
}

.sticky {
  position: sticky;
  right: 0;
  background-color: yellow;
}
.menu {
  position: absolute;
  left: 0;
  background-color: red;
  z-index: 1;
}
<body>
    <div class="wrapper">
      <table>
        <thead>
          <tr>
            <th>Col</th>
            <th>Col</th>
            <th>Col</th>
            <th class="sticky">Action</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>-----------------------------------</td>
            <td>-----------------------------------</td>
            <td>-----------------------------------</td>
            <td class="sticky">
              . . .
              <div class="menu">
                <div>item</div>
                <div>item</div>
                <div>item</div>
              </div>
            </td>
          </tr>
          <tr>
            <td>-----------------------------------</td>
            <td>-----------------------------------</td>
            <td>-----------------------------------</td>
            <td class="sticky">. . .</td>
          </tr>
          <tr>
            <td>-----------------------------------</td>
            <td>-----------------------------------</td>
            <td>-----------------------------------</td>
            <td class="sticky">. . .</td>
          </tr>
        </tbody>
      </table>
    </div>
  </body>

Answer №1

To ensure proper positioning, make sure to include the z-index property in the parent element of your absolutely positioned element:

body {
  margin: 0;
}

.wrapper {
  overflow-x: auto;
  position: relative;
  white-space: nowrap;
  max-width: 600px;
}
th,
td {
  min-width: 300px;
  text-align: center;
}
th:last-child,
td:last-child {
  min-width: 50px !important;
}

.sticky {
  position: sticky;
  right: 0;
  background-color: yellow;
}
.menu {
  position: absolute;
  left: 0;
  background-color: red;
}
.z-index {
  z-index: 1;
}
<body>
    <div class="wrapper">
      <table>
        <thead>
          <tr>
            <th>Col</th>
            <th>Col</th>
            <th>Col</th>
            <th class="sticky">Action</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>-----------------------------------</td>
            <td>-----------------------------------</td>
            <td>-----------------------------------</td>
            <td class="sticky z-index">
              . . .
              <div class="menu">
                <div>item</div>
                <div>item</div>
                <div>item</div>
              </div>
            </td>
          </tr>
          <tr>
            <td>-----------------------------------</td>
            <td>-----------------------------------</td>
            <td>-----------------------------------</td>
            <td class="sticky">. . .</td>
          </tr>
          <tr>
            <td>-----------------------------------</td>
            <td>-----------------------------------</td>
            <td>-----------------------------------</td>
            <td class="sticky">. . .</td>
          </tr>
        </tbody>
      </table>
    </div>
  </body>

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

What is the best way to connect a fresh post to a different URL in ReactJS and Redux?

Currently, I am working with Redux and ReactJS to develop a discussion platform. One of the key features I am trying to implement is where users can input the title and content of their post, which will then be submitted to create a new post. After submit ...

Conceal div2 upon clicking button1 and reveal div1 using Angular

When the "Edit" button is clicked, I want to display <div ng-if="hideShow">. Conversely, when the "Add" button is clicked, I want to hide <div ng-if="hideShow1"> and display another <div ng-if="hideShow1">. Currently, clicking on the "Ed ...

Adjust background color (scrolling) as element reaches specified point (Responsive)

Hello everyone, I'm a newcomer to Javascript and I've tried searching for solutions on my own before reaching out here. My goal is to change the background color when specific div tags with certain ids are 150 pixels away from the top of the bro ...

The mobile version is experiencing issues with the functionality of the responsive sidebar

Having an issue with the sidebar on this page. In the responsive version, particularly on smartphones, I can't get it to go underneath the content. The sidebar stays connected to the left but doesn't wrap around. Here is the CodePen link. If t ...

Receive a distinct key alert after including a key attribute to a div element containing multiple sub nodes in react version 18.2.0

When attempting to render the div element on the page, I encountered a warning stating: "Each child in a list should have a unique 'key' prop." <div {...{}} key={"formKey"}> <input type="text" /> <button> ...

Delete the element that was generated using jQuery

Is there a way to add and remove an overlay element using JavaScript? I am struggling to get JavaScript to remove an element that was created with JavaScript. Any suggestions? Check out this JS Fiddle example Here is the HTML code: <div id="backgroun ...

What is the best way to resize a mouse-over div based on the size of the screen

When using Quora, there is a feature that displays a pop-up card when you hover over a username or picture. This card will always stay within the visible window area and adjust its position based on the screen size. For example, take a look at these Quora ...

What is the best way to navigate my Page while my Dialog Component is displayed?

Is there a way to enable scrolling on the background when a dialog component opens up? By default, scrolling is disabled when the dialog appears. How can we allow scrolling in the background? Here is the code snippet: import React, { useState } from &apos ...

Youtube no longer supports looping videos by adding "loop=1" to the URL

I'm trying to embed a YouTube video in my WordPress website with the loop parameter set to 1 so that it replays automatically. However, the video is not auto-replaying and instead showing related videos. Below is the HTML code I am using: <iframe ...

Steps for embedding a dynamic 3D model onto a website with THREE.js

Seeking guidance on integrating animated 3D models onto a webpage using THREE.js. Currently, I have successfully loaded a static 3D model named 'aaa.gltf' with auto rotation and orbit control functionality. However, when trying to load another an ...

Implementing a function as the `data-*` attribute for an element in Angular 6

I have a question about my component.ts file: import { Component, OnInit, AfterViewInit, ElementRef, Inject } from '@angular/core'; import { DOCUMENT } from '@angular/platform-browser'; import { environment } from '../../../enviro ...

How to fix the issue of a static background image in Bootstrap that does not

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!--Bootstrap--& ...

Styling Drawn Elements on a Canvas Using CSS

Can CSS Animations be applied to a circle drawn on a canvas using JavaScript? Specifically, I am using an AngularJS directive for this purpose: https://github.com/angular-directives/angular-round-progress-directive/blob/master/angular-round-progress-direct ...

How to toggle the display of a div by its id using index in Javascript

Currently, I am encountering a problem with toggling the div containers. When I click on the video button, I want the other divs to close if they are already open. However, due to the toggling mechanism, if a div is closed and I click on the corresponding ...

What are the steps to modify the text on a button using jQuery?

For some reason, this seemingly simple task is not working as expected. Can anyone explain why? In my HTML code, I have defined a button like this: <button id="T4">Hide</button> And in jQuery, I've written a function to change the text ...

Using Django to showcase identical forms multiple times within a single view

I am looking for a way to open the same form multiple times while looping through some items. Below is the structure of the form: class CancelRefundForm(forms.forms.Form): cancel = forms.BooleanField(label='Check to cancel the refund', required= ...

Having trouble viewing the search field text in Firefox 3.6?

The customized search form in this version is inspired by the default twenty eleven Wordpress one. While the default text appears almost everywhere, it may not be displayed properly in older versions of Firefox and Internet Explorer. This could be due to ...

best practices for choosing items from a dropdown menu using Angular

I have a dropdown list displaying existing tags/chips created by users in the past. However, I'm having an issue where when I select a tag from the dropdown list, it doesn't show up in my input field (similar to the Chart tag currently). I am abl ...

Utilizing XSLT to Embed a YouTube Video and Modify the SRC Attribute

Hey, I'm having trouble embedding a YouTube video in XSLT. The page loads fine with all the data except for the video embed. When I tried changing just the source attribute, the whole XSLT failed to load. I found a workaround here , which fixed the lo ...

Can you explain the distinction between <P> and <p> tags in HTML?

Currently, I am examining HTML documents by parsing them through Java and I have noticed that the p tag is one of the most frequently utilized tags. While I understand that it is used to create a new line, I am puzzled by the fact that in some documents I ...