Mastering the Art of Manipulating Z-Index Stacking Context in CSS Using Transforms

I'm struggling to position the red square on top of the table. After reading articles about Z-index Stacking Context and browsing through this stack overflow page about overriding CSS Z-Index Stacking Context, I still can't seem to figure it out. It seems like using transform properties might be a solution, but it's not working in my specific case. Any advice would be greatly appreciated. Thanks!

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

  • html
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Home</title>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width" />
    <link rel="stylesheet" href="styles.css" />
    <script type="module" src="script.js"></script>
  </head>
  <body>
    <div class="top">
      <table>
        <thead>
          <tr>
            <th colspan="2">The table header</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>The table body</td>
            <td>with two columns</td>
          </tr>
        </tbody>
      </table>
    </div>
    <div class="bottom">
      <div>
        <div><span class="red">Red</span></div>
        <div><span class="green">Green</span></div>
        <div><span class="blue">Blue</span></div>
      </div>
    </div>
  </body>
</html>
  • css
.top {
  z-index: 2;
  position: relative;
}
.bottom {
  z-index: 1;
  position: relative;
}
table,
td {
  border: 1px solid #333;
  z-index: 4;
  position: relative;
  background-color: #fff;
}
thead,
tfoot {
  background-color: #333;
  color: #fff;
}
.red,
.green,
.blue {
  position: absolute;
  width: 100px;
  color: white;
  line-height: 100px;
  text-align: center;
}
.red {
  z-index: 111;
  top: -40px;
  left: 20px;
  background: red;
  transform: translateZ(1px);
}
.green {
  top: -20px;
  left: 60px;
  background: green;
}
.blue {
  top: -10px;
  left: 100px;
  background: blue;
}
body,
div:first-child {
  transform-style: preserve-3d;
}

Answer №1

If you're looking to implement a new logic, you can try adjusting the CSS by adding a z-index property at the end of the div :first-child.

[![enter image description here][1]][1]

[1]:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Home</title>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width" />
    <link rel="stylesheet" href="styles.css" />
    <script type="module" src="script.js"></script>
  </head>
  <style>
    .top {
      z-priority: 2;
      position: relative;
    }
    .bottom {
      z-priority: 1;
      position: relative;
    }
    table,
    td {
      border: 1px solid #333;
      z-priority: 4;
      position: relative;
      background-color: #fff;
    }
    thead,
    tfoot {
      background-color: #333;
      color: #fff;
    }
    .red,
    .green,
    .blue {
      position: absolute;
      width: 100px;
      color: white;
      line-height: 100px;
      text-align: center;
    }
    .red {
      z-priority: 111;
      top: -40px;
      left: 20px;
      background: red;
      transform: translateZ(1px);
    }
    .green {
      top: -20px;
      left: 60px;
      background: green;
    }
    .blue {
      top: -10px;
      left: 100px;
      background: blue;
    }
    body,
    div:first-child {
      z-priority: -1;
      transform-style: preserve-3d;
    }
  </style>
  <body>
    <div class="top">
      <table>
        <thead>
          <tr>
            <th colspan="2">The table header</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>The table body</td>
            <td>with two columns</td>
          </tr>
        </tbody>
      </table>
    </div>
    <div class="bottom">
      <div>
        <div><span class="red">Red</span></div>
        <div><span class="green">Green</span></div>
        <div><span class="blue">Blue</span></div>
      </div>
    </div>
  </body>
</html>

m/Rxs7O.png

Answer №2

I managed to achieve the desired outcome (I believe) by simply commenting out some of your CSS code. No new additions were made.

Take a look at the code snippet below or visit my modified version of your stackblitz.

/* .top {
  z-index: 2;
  position: relative;
} */

.bottom {
  /* z-index: 1; */
  position: relative;
}
table,
td {
  border: 1px solid #333;
  z-index: 4;
  position: relative;
  background-color: #fff;
}
thead,
tfoot {
  background-color: #333;
  color: #fff;
}
.red,
.green,
.blue {
  position: absolute;
  width: 100px;
  color: white;
  line-height: 100px;
  text-align: center;
}
.red {
  z-index: 111;
  top: -40px;
  left: 20px;
  background: red;
  /* transform: translateZ(1px); */
}
.green {
  top: -20px;
  left: 60px;
  background: green;
}
.blue {
  top: -10px;
  left: 100px;
  background: blue;
}
/* body,
div:first-child {
  transform-style: preserve-3d;
} */
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Home</title>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width" />
    <link rel="stylesheet" href="styles.css" />
    <script type="module" src="script.js"></script>
  </head>
  <body>
    <div class="top">
      <table>
        <thead>
          <tr>
            <th colspan="2">The table header</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>The table body</td>
            <td>with two columns</td>
          </tr>
        </tbody>
      </table>
    </div>
    <div class="bottom">
      <div>
        <div><span class="red">Red</span></div>
        <div><span class="green">Green</span></div>
        <div><span class="blue">Blue</span></div>
      </div>
    </div>
  </body>
</html>

Answer №3

.top {
  position: relative;
}
.bottom {
  position: relative;
}
table,
td {
  border: 1px solid #333;
  z-index: 0;
  position: relative;
  background-color: #fff;
}
thead,
tfoot {
  background-color: #333;
  color: #fff;
}
.red,
.green,
.blue {
  position: absolute;
  width: 100px;
  color: white;
  line-height: 100px;
  text-align: center;
}
.red {
  z-index: 1;
  top: -40px;
  left: 20px;
  background: red;

}
.green {
  top: -20px;
  left: 60px;
  background: green;
  z-index: -3;
}
.blue {
  top: -10px;
  left: 100px;
  background: blue;
  z-index: -2;
}
<!DOCTYPE html>
    <html lang="en">
      <head>
        <title>Home</title>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width" />
        <link rel="stylesheet" href="styles.css" />
        <script type="module" src="script.js"></script>
      </head>
      <body>
        <div class="top">
          <table>
            <thead>
              <tr>
                <th colspan="2">The table header</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>The table body</td>
                <td>with two columns</td>
              </tr>
            </tbody>
          </table>


        </div>
        <div class="bottom">
           <div>
            <div><span class="red">Red</span></div>
            <div><span class="green">Green</span></div>
            <div><span class="blue">Blue</span></div>
          </div>
        </div>
      </body>
    </html>

Modified some parts of your code, now with the ability to use negative numbers for z-index. Excluded the following code:

body,
div:first-child {
  transform-style: preserve-3d;
}

https://jsfiddle.net/d3x746bc/1/

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

Transitioning background with background sizing set to cover

I have been searching for an answer to this question, but haven't found one yet. Currently, I have a series of divs with background-images set to 'background-size: cover'. My goal is to make the images zoom in and grow on hover. However, i ...

What is the best way to show TypeScript code using "<code>" tags within an Angular application?

I am looking to showcase some TypeScript (angular code) as plain text on my website using prismjs. However, the Angular framework is executing the code instead. How can I prevent it from running? I have attempted enclosing it within pre and code tags wit ...

Change the background color of a Bootstrap dropdown when toggled

I recently created this code snippet: <div class="dropdown"> <button class="btn btn-secondary dropdown-toggle buttonForProfile" type="button" id="dropdownMenuButton" data-bs-toggle='dropdo ...

I am interested in incorporating a vertical scrollbar into a gridview in ASP.NET

I've successfully bound data to a grid view in my ASP.NET application. I've implemented pagination, but instead of that, I'd like to incorporate a scroll bar to prevent lengthy paging. Below is the code for my grid view: <div class="box ...

Mobile navigation menu options on the left and right sides

I've encountered an issue with my mobile navigation. I have two navigation menus - one on the left and one on the right, designed to slide out when triggered. The left menu functions correctly with a smooth transition, but the right menu abruptly pops ...

What is preventing me from adjusting the padding of the mat-button?

Trying to adjust the default padding of a mat-button, but encountering issues with changing the component's style. Any suggestions on how to subscribe to the default padding (16px)? I've attempted modifying CSS properties to set the padding of a ...

Signature incorporating visual elements in emails

I am attempting to create an HTML + Images Signature in Mozilla Thunderbird. I understand that this may not be the most ideal software for designing email signatures, but I am looking for a free alternative. While my image is attached to the email itself ...

Interactive JQuery plugin for scrolling through thumbnails with customizable buttons

I am attempting to create a jQuery thumbnail scroller with buttons that respond to mousedown and mouseup events. I have successfully implemented the scrolling functionality, but I am facing issues when trying to refactor it into a reusable function. Below ...

Learn how to swap out the traditional "back to top" button with a customized image and make it slide onto or off the page instead of simply fading in and out

As a newcomer, I am trying to replicate a unique "back to top" effect that caught my eye on another website. Instead of the traditional fade-in approach when scrolling down, the "back to top" image in question elegantly slides out from the bottom right c ...

Interested in mastering the art of storing data in forms using PHP? Ever wondered why this functionality doesn't seem to function in an HTML file?

Hello, I'm new to coding and I need some help. Recently, I learned how to create forms in HTML and now I want to save and display the data using PHP. I followed along with some PHP exercises on a website called W3Schools, but when I tried to run the f ...

Utilizing jQuery's .clone() function to duplicate HTML forms with radio buttons will maintain the radio events specific to each cloned element

I'm currently developing front-end forms using Bootstrap, jQuery, HTML, and a Django backend. In one part of the form, users need to input "Software" information and then have the option to upload the software file or provide a URL link to their hoste ...

Images that have been resized on Android appear blurry when viewed on the second page

Customizing the Toolbar: #main_bar { background: url(../images/logo.jpg) 99% 50% no-repeat; background-size: auto 80%; } The original size of logo.jpg is 220x266px The toolbar has a fixed height of 46px Therefore, the background image appears t ...

What causes the vuetify tag not to render as HTML in the browser?

I ran into a styling issue while using Vuetify in my Nuxt 2 project. Upon inspecting the element in dev tools, I noticed that some Vuetify tags were not being converted to HTML as expected (they were displayed as div class="tag_name"). These unco ...

Could we customize the appearance of the saved input field data dropdown?

I'm currently working on styling a form that includes an input field. The challenge I'm facing is that the input field needs to be completely transparent. Everything works fine with the input field until I start typing, which triggers the browser ...

"An issue arises with jqPlot axes and legend when exporting images, as they are not rendering

After successfully rendering my jqPlot, I encountered an issue when exporting it as an image. The axes text and legend labels aren't displaying correctly in the exported image - the text seems to be shifting. Here's a comparison of the actual plo ...

What is the best way to add an active class to a clicked menu item that already has a class

I have menu items on the master page, and if users click on that menu item, I want to show it as active. My goal is to append "Active" to the existing class to indicate that it is active. _Layout.cshtml: <div class="menu-items-navigation"> <di ...

Displaying and Concealing Elements with AngularJS and Implementing Alternating Styles

Within my collection of div elements showcasing product information, there is a need to display or hide specific fields based on the product type. While this functionality works smoothly, we now aim to enhance readability by implementing alternating row st ...

The module cannot be located due to an error: Package path ./dist/style.css is not being exported from the package

I am facing an issue with importing CSS from a public library built with Vite. When I try to import the CSS using: import 'rd-component/dist/style.css'; I encounter an error during the project build process: ERROR in ./src/page/photo/gen/GenPhot ...

Encountering issue: LineChart is not recognized as a constructor, resulting in failure to display chart on webpage

I am struggling with displaying a chart in my HTML file that should show the details of a specific process from the past few minutes. Whenever I try to initialize the chart using google.charts.load('current', {'packages':['line&apo ...

Reposition the div element on mobile devices using media queries

Hello, I'm facing an issue on mobile with my website: dev site Today, I implemented a dropdown menu with flags to allow language switching. However, the dropdown in mobile is appearing under the hamburger nav bar. I was thinking of using media querie ...