CSS3 Transform can sometimes cause unpredictable z-index or depth arrangements

Could there possibly be a glitch in CSS3, or am I just making a simple mistake?

Take a look at the page I'm currently working on: View Page

I wish I could demonstrate the error, but its behavior seems to be completely unpredictable. Here's what's happening:

The flag is supposed to be consistently behind the purple layout. However, it appears randomly on top, below, or switches between positions without any JavaScript code running. Sometimes it starts at the bottom and ends up on top during animation, only to return to its original position later.

In short, the bug is erratic and lacks any discernible pattern (despite my efforts of tracking time and animations).

Does anyone have tips on how to resolve this issue? Here's an image for reference, in case you're not experiencing the same problem (commonly seen in Safari and Chrome):

Answer №1

Recently, I encountered a similar issue and found that the transform: translate3d(0,0,0); solution did not work for me.

The reason behind this is that z-index only stacks elements in a 2D world. Therefore, when using 3D transformations, z-index does not have any effect.

So, the real question here is:

How can we stack elements in a 3D environment?

The answer is simple:

Use the Z axis!

It may sound straightforward, but it can be confusing due to browser rendering. To illustrate this concept, I created a JSFiddle. In the fiddle, there are four flags arranged in two rows. Additionally, I included two other divs (score) representing an element that should overlay the others. The first row demonstrates a bug, while the second row does not.

To observe the bug, hover your cursor over an element with the CSS properties pointer on the flag and col-resize. Notice how the cursor changes when moving over the green element from left to right.

I created some diagrams to clarify the issue. Here, you can see our elements represented as they should be by the browser:

The problem becomes more evident now. On the right side, the green element is above the blue one, which is the desired outcome. However, on the left side, the use of rotateY() causes the blue element to "leave the screen," overlapping the green element. If this is unclear, visualize the scenario "from above":

We can imagine that the blue element leaves the screen by half its size. My workaround involves translating the blue element along the Z-axis to -10000px to ensure it remains far away and does not overlap with the green element during rotation. The order of operations here is crucial: first, the translation translate3d(0, 0, -10000px), followed by the perspective and rotation resulting in:

transform: translate3d(0, 0, -10000px) perspective(1200px) rotateY(-45deg);
. As illustrated in my final diagram from above:

Now, the blue element is distanced enough to avoid going over the green one. However, a potential issue arises if the blue element exceeds 10,000 pixels in size, causing it to overlap once again.

In conclusion, I acknowledge that this solution serves as more of a workaround than a definitive fix. Nonetheless, it proved beneficial in my case, and I hope it assists you in addressing your initial problem without straying too far off track.

Answer №2

Could you provide the browser versions for reference? I'm unable to replicate the issue on the latest versions of Chrome, Safari, and Firefox.

It seems like the problem could be linked to a known bug:

You might want to consider including this in the flat items:

transform: translate3d(0,0,0);

This will ensure that they are treated as 3D elements, reducing the likelihood of encountering any bugs.

Additionally, make sure you are using all necessary browser prefixes. The transform property doesn't seem to be working on Firefox at all, so it's worth checking if this is an issue across multiple browsers.

Answer №3

The bug doesn't seem to be replicable in Chrome, Safari, or Firefox. After researching on stackoverflow, I found a similar question that might be relevant to your situation (Complex Webkit Bug? Relative Position + Z-Index + Overflow Hidden + CSS3 Transform)

My suggestion would be to incorporate some Javascript prior to triggering the sliding event, to calculate z-index and assign a higher value to the "purple blocks," or better yet, eliminate CSS transitions altogether and opt for Javascript transitions exclusively.

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 reinitialize a particular input field by using either the entered value or the total sum from buttons?

I successfully created a functional sum amount and reset button for a specific field that works perfectly outside of a form tag. However, I now need to place all the input fields and buttons inside a form tag to enable validation before submission. The de ...

The response detail error code 2 indicates that the post method API body check has failed within the Agora REST API, resulting in

const Authorization = `Basic ${Buffer.from(`${config.CUSTOMERID}:${config.CUSTOMER_SECRET}`).toString("base64")}`; const acquire = await axios.post(`https://api.agora.io/v1/apps/${config.agoraAppId}/cloud_recording/acquire`,{ ...

What could be causing the PAGE CSS to malfunction?

I am looking to export HTML text as a word document with A4 size and portrait orientation. My JavaScript currently allows me to export the text, but it appears like a webpage format rather than in A4 or portrait mode. I have tried adding @page CSS styling ...

Altering the background color of the navigation bar when scrolling

So, my navigation bar starts with a transparent background, but I wanted it to change to a different background color when a user scrolls down. I found a solution in this question: Changing nav-bar color after scrolling? My current code now looks like th ...

Is there a PHP function that functions similarly to JavaScript's "array.every()" method?

As I work on migrating JavaScript code to PHP, I am facing the challenge of finding a replacement for the array.every() function in PHP. I have come across the each() function in PHP, but it doesn't quite meet my requirements. ...

The function json.stringify fails to invoke the toJson method on every object nested within the main object

When using IE 11, I encountered an issue where calling Stringify on my objects did not recursively call toJson on all objects in the tree. I have implemented a custom toJson function. Object.prototype.toJSON = function() { if (!(this.constructor.name == ...

Experience the power of dynamic site regeneration with GatsbyJS

Currently, I am working on a website built in GatsbyJS that deals with large datasets of dynamic content fetched using React fetch upon page load. The challenge here is to display semi-live data that updates every 5 minutes. I am curious about how I can m ...

Having trouble sending a FormData object with Axios for a user uploaded file in a React, Node.JS project

My goal is to enable users to upload images to my application, which consists of a React frontend and a Node backend. The process involves sending the uploaded file from the client to the server, followed by making a request from the server to Firebase clo ...

Is there a way to use JavaScript to determine our current position in a CSS Keyframe animation based on a percentage?

After some thought, I realized that it's possible to detect when a CSS animation starts, finishes, or repeats by using the animationstart, animationiteration, and animationend events. For example: document.getElementById('identifier') ...

Error message is not shown by React Material UI OutlinedInput

Using React and material UI to show an outlined input. I can successfully display an error by setting the error prop to true, but I encountered a problem when trying to include a message using the helperText prop: <OutlinedInput margin="dense&quo ...

Excluding node modules when not included in tsconfig

Within my Angular project, there is a single tsconfig file that stands alone without extending any other tsconfigs or including any additional properties. Towards the end of the file, we have the following snippet: "angularCompilerOptions": { ...

Sit tight as we prepare all static assets for loading on the screen

Currently, I am developing a vuejs application that will incorporate video elements. To enhance user experience, we are interested in preloading these videos upon the initial loading of the web application. I am considering using a listener like, documen ...

Is there a way to integrate PHP functionality into JavaScript?

When it comes to using JavaScript and PHP together, a common challenge is retrieving information from a database and utilizing that data in a JavaScript function. In my case, I need this functionality for implementing password change functionality on a w ...

Create an interactive quiz using JQuery that focuses on sorting and organizing

Hey there, I need some advice on creating a quiz for ordering and sorting in HTML5/JQuery. Basically, I want to display a random set of steps on the page that users have to organize correctly. I found an example using jQuery and jquery-ui-1.8.5.custom.min. ...

Counting duplicate values associated with the same key in a JSON array using JavaScript/NodeJS

Hello everyone, I could really use some assistance in solving this issue. If this has already been asked before, please direct me to the original question. Currently, I am working with a JSON array of elements. For example: var data = [{"key":"Item1"},{ ...

IE9 presents a frustrating issue where the jQuery select box value is returning as

I've been battling a bug for over 2 hours now. My javascript code is supposed to generate a two-level select box for tasks and subtasks from JSON data. However, I recently discovered that this code doesn't function properly on IE9, and possibly ...

The sidebar on the right side of the screen is content to relax beneath my

The positioning of my sidebar is currently below the content on the left side of the page, but I want it to sit beside the content on the right side. I've tried various solutions from similar questions without success. Here is an example of how I woul ...

Tips for automatically closing a dropdown menu when it loses focus

I'm having some trouble with my Tailwind CSS and jQuery setup. After trying a few different things, I can't seem to get it working quite right. In the code below, you'll see that I have placed a focusout event on the outer div containing th ...

What is the best way to include a ref objectId in a Node.js application?

Main Model { name: { type: String, trim: true, required: true }, carModels: [{ type: ObjectId, ref: 'CarModel' }] } Second Model { name: { type: String, trim: true, required: true ...

ngSwitchCase provider not found

While following a tutorial and trying to implement what I learned, I encountered an error that I'm having trouble understanding. The browser console shows an error message stating [ERROR ->]<span *ngSwitchCase="true">, but I can't figure ...