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!

  • 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

Is there a way to assign the scroll event context to `document.body` instead of `window`?

Discovery As I delved into the intricacies of web development, I stumbled upon a curious observation. In a particular MDN page, I noticed that the left and right sidebars had their scrollbars contained within themselves. However, the content block's ...

Adding a box shadow around the entire div in Internet Explorer 11 with CSS

I am having difficulty applying box-shadow to create a shadow around the entire perimeter of a div. While it works in FireFox, it does not work in IE 11. So far, I have attempted solutions from this StackOverflow thread. For reference, here is a JSFiddle ...

"A lengthy contenteditable div designed to mimic an input field, with the ability to horizontally scroll

When the input is too short for the entire text to be displayed, users have the option to horizontally scroll the text inside the input by dragging with the mouse. Is there a way to implement this functionality in a contenteditable field that appears as ...

How should I refer to this style of font?

After trying to implement a font from bulletproof @font-face as a template for my website, I am facing issues. My goal is to utilize the perspective-sans black regular font. Despite uploading the necessary files - including the css style sheet provided in ...

align text vertically over an image

I've come across this question many times, but none of the answers seem to solve my issue. My challenge is centered around aligning text on an image with regards to vertical positioning. Below is the code I'm working with: <div class="col-sm- ...

Using Jquery's .appendTo method to add content to multiple divs with identical class names

Attempting to use jQuery .appendTo to move divs is causing the repetition of divs in each selector. This action involves taking the heading from each div and individually placing it in the parent div. A more detailed explanation is provided below. Present ...

What is the best way to confine the child elements within a parent element using CSS?

Check out my CSS and HTML code here: https://jsfiddle.net/z2cc11yk/ Here's the HTML: <div class="progressbar-container"> <div class="progressbar-text">125%</div> <div class="progressbar-progress" style="background-color: red; wi ...

The fixed table header is misaligned with the body columns' width

I was looking for a way to add a vertical scrollbar to my table, and I ended up wrapping the entire table in a div. The result was that the table head was fixed in position. Is there a simpler method to include a scrollbar in a table without affecting the ...

The horizontal scrolling with overflow-x is not functioning correctly as it is displaying the scrollbar for the vertical

I am perplexed as to why I am unable to scroll horizontally to view the other pink thumbs. Despite adding an overflow of scroll-x to the thumbs container, which should theoretically allow horizontal scrolling, it only seems to scroll vertically. Could som ...

What is the best way to replicate tags in selenium when an element has multiple class names?

Extracting information from a webpage where a significant amount of text was concealed behind the "see more" tab. Using selenium to click on all such buttons and then extracting data with beautifulsoup. However, some of these buttons have additional space ...

Designing a layout with one box on top and two beneath it, covering the entire page, can be achieved by following these steps

I am currently working on a project where I want to divide the screen into three sections. One section will cover half of the screen to display an image slider, and the remaining half will have two sections which is already done. However, now I need to add ...

How can "this" be properly utilized in jQuery?

I am attempting to retrieve the title attribute of an element from various elements with the same class, each having different attributes. This is my current approach: HTML <div title="title1" class="pager" onclick="location.href='link.aspx& ...

CSS - borders are overlapping with one another

Encountering an issue where the bottom border is overlapping the right border on the same element. Here's a visual representation of the problem: The green right border's bottom is appearing distorted due to the presence of the gray border at t ...

Are we utilizing this JavaScript function properly for recycling it?

Two functions have been implemented successfully. One function adds the autoplay attribute to a video DOM element if the user is at a specific section on the page. The other function smoothly slides in elements with a transition effect. The only limitatio ...

PHP Script unable to locate results

For some reason, the script I have written is not functioning properly. Upon reviewing the code from walmart.com, I verified that name="query" is accurate. However, I am uncertain about the contents within <script></script> <html> < ...

upright scrolling mouse slider

In my project, I have implemented a vertical slider with mousewheel control using SwiperJS from https://swiperjs.com/. Although the slider is working perfectly fine, I am looking to fix the positions while scrolling on the page similar to the example pro ...

Incorporating SCSS directly in React component along with material-ui styling

I'm having trouble incorporating styles into my React component. I'd prefer to use SCSS instead of either using the withStyle function or importing a plain CSS file into a JS file. For instance, I would like to import my SCSS file into ButtonCom ...

Styles for Material UI 5 class names

After upgrading from Mui 4 to 5, I am facing a challenge in using class names effectively. While the SX property allows me to apply styles to individual components, I am struggling with applying the same class to multiple components. In version 4, my code ...

Why does my jQuery IMG center script not work in Chrome and Safari, but it works perfectly in IE?

I am experiencing an issue with centering the thumbnails of products on my e-commerce website. Despite successful centering in IE, FF, and Opera, Chrome and Safari are not aligning the thumbnails properly – they remain at the top of the div instead of be ...

Creating dynamic text bubble to accommodate wrapped text in React using Material-UI (MUI)

I am currently developing a chat application using ReactJS/MUI, and I have encountered an issue with the sizing of the text bubbles. The bubble itself is implemented as a Typography component: <Typography variant="body1" sx={bubbleStyle}> ...