Applying CSS Translate Z transition with perspective results in elements skewing and distorting in a unique

I'm struggling to grasp the concepts of translateZ and perspective, so I've provided a CodePen link for reference.

Here is my code:

body {
  margin: 0;
  height: 100vh;
}

.cards-container {
  padding: 100px;
  margin: auto;
  margin-top: 100px;
  transform-style: preserve-3d;
}

.cards-container,
.cards-container * {
  height: 400px;
  width: 400px;
  box-sizing: border-box;
  background-color: lightgray;
  transition: all ease 1.6s;
  /* perspective: 1200px; */
}

.card {
  width: 200px;
  height: 200px;
  padding: 50px;
  background-color: lime;
  transform-style: preserve-3d;
}

.card-child {
  width: 100px;
  height: 100px;
  background-color: rgb(255, 230, 0);
}

.cards-container:hover {
  transform: perspective(1200px) rotateX(50deg) rotateY(20deg) rotateZ(-35deg) translateZ(40px);
}

.cards-container:hover .card {
  transform: perspective(1200px) translateZ(80px);
}

.cards-container:hover .card .card-child {
  transform: perspective(1200px) translateZ(60px);
}
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Demo</title>
  <link rel="stylesheet" href="css/main.css">
</head>

<body>
  <script src="index.js"></script>
  <div class="cards-container">
    <div class="card">
      <div class="card-child"></div>
    </div>
  </div>
</body>

</html>

The issue I'm experiencing is difficult to consistently replicate. When hovering over the cards in the example, the transitions usually work smoothly. The goal is for the three cards to transition from a flat layout to a 3D one upon hover, with each card moving above its parent. However, sometimes the transition becomes distorted and goes out of bounds on the z-axis when hovering on and off the cards multiple times. This issue seems related to the perspective property, but I am unsure how to troubleshoot it effectively.

If you have any insights or solutions to this problem, please feel free to share them.

Answer №1

One important aspect to note is that a transition on perspective needs to be handled with caution, as it can contribute to undesired effects when combined with other transformations. It's crucial to maintain consistency in perspective even during non-hover states:

body {
  margin: 0;
  height: 100vh;
}

.cards-container {
  padding: 100px;
  margin: auto;
  margin-top: 100px;
  transform-style: preserve-3d;
}

.cards-container,
.cards-container * {
  height: 400px;
  width: 400px;
  box-sizing: border-box;
  background-color: lightgray;
  transition: all ease 1.6s;
  /* perspective: 1200px; */
}

.card {
  width: 200px;
  height: 200px;
  padding: 50px;
  background-color: lime;
  transform-style: preserve-3d;
}

.card-child {
  width: 100px;
  height: 100px;
  background-color: rgb(255, 230, 0);
}

.cards-container {
  transform: perspective(1200px) rotateX(0deg) rotateY(0deg) rotateZ(0deg) translateZ(0px);
}

.cards-container:hover {
  transform: perspective(1200px) rotateX(50deg) rotateY(20deg) rotateZ(-35deg) translateZ(40px);
}

.cards-container .card {
  transform: perspective(1200px) translateZ(0px);
}

.cards-container:hover .card {
  transform: perspective(1200px) translateZ(80px);
}

.cards-container .card .card-child {
  transform: perspective(1200px) translateZ(0px);
}

.cards-container:hover .card .card-child {
  transform: perspective(1200px) translateZ(60px);
}
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Demo</title>
  <link rel="stylesheet" href="css/main.css">
</head>

<body>
  <script src="index.js"></script>
  <div class="cards-container">
    <div class="card">
      <div class="card-child"></div>
    </div>
  </div>
</body>

</html>

In situations like this, it's advisable to rely on the perspective property rather than using the perspective() transform function.

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

Having trouble with my HTML5 JavaScript counting script, it seems to be

Having trouble finding the issue with my script, as there are no visible errors. If anyone could provide some guidance on what might be causing the problem, that would be greatly appreciated. I suspect Bootstrap might be causing interference since it wor ...

Expert tips for adding spacing between images in carousel sliders and eliminating glitches during slides

After creating a carousel following the guidance of this source and initially sharing it on this platform, I encountered an issue with the padding on the first image. As a solution, I removed the padding from the first image, but this caused the image to b ...

Wordpress problem with Bootstrap JavaScript code involving data-toggle="collapse"

Currently facing an issue with my project. This is my first attempt at creating a WordPress blog, inspired by the HTML site www.humantools.com.mx and building a blog for it at www.humantools.com.mx/blog. There's a strange problem: when logged in, the ...

evaluating each element in a list against another

I'm attempting to check if an element exists in another list, but the lists are not in order and they are of different sizes. let list1 = [10 , 4 , 5] let list2 = [4 , 5] I attempted a solution, however it only works when the elements are in the same ...

How to implement a collapsible tree view component in Angular 2

I am currently working on creating my own collapsible tree view in order to gain a better understanding of Angular 2. I have made some progress with it, but I am facing an issue with applying the hidden property to the specific <li> item that is clic ...

How to turn off pinch to zoom feature in Mozilla Firefox on a Microsoft tablet or touch screen display

My goal is to enable zooming and panning for an SVG across all devices and browsers. I've successfully implemented this using panzoom.js. However, I'm encountering an issue specifically with Mozilla Firefox on tablet devices and Windows touch sc ...

There seems to be an issue with the functionality of the .ht

I am facing an issue with my htaccess file while trying to implement URL rewriting. I am attempting to change the URL from "quotewebster.com/topics.php?topic_id=12" to this: "quotewebster.com/topics/12/" In my htaccess file, I have added the following co ...

The margin persists despite the usage of the * selector and !important declaration

I'm currently working on a website built upon this template: https://github.com/issaafalkattan/React-Landing-Page-Template My issue arises when trying to remove margins in multiple sections. For instance, I want to eliminate the 'orange' ar ...

Paginator for Angular Material Cards

This section contains the TypeScript file for a component import { MatTableDataSource, MatPaginator } from "@angular/material"; import { FoodService } from "./food.service"; import { Food } from "./food"; @Component({ selec ...

The process of automating CSS testing

We are currently in the process of developing a website using SharePoint 2010 for a client who already has an existing PHP website. We are working to ensure that the front end of the new site matches the styling of the client's current PHP site, speci ...

Embed SVG text from a different webpage into the image source

I am working with two aspx pages. The first page (Page1.aspx) includes the following code snippet: <img src="Page2.aspx" /> The second page (Page2.aspx) contains this code in its code-behind file: Dim svgString as String = "<svg height="100" wi ...

Pattern that incorporates a numerical value ranging from 0 to 99, while also permitting a decimal point with up to two digits

When developing an Angular app, we are in need of validation that allows users to enter values between 0 and 99, with the option of up to two decimal points. Here are some examples: 0 -> Pass 0.1 -> Pass 1.12 -> Pass 12.12 -> Pass 1 ...

Developing a quiz using jQuery to load and save quiz options

code: http://jsfiddle.net/HB8h9/7/ <div id="tab-2" class="tab-content"> <label for="tfq" title="Enter a true or false question"> Add a Multiple Choice Question </label> <br /> <textarea name ...

Tips for implementing [class*="col-"] in a SCSS module within Nextjs

Creating a responsive grid system in my nextjs web app using scss module has been challenging. /* For desktop: */ .col-1 { width: 8.33%; } .col-2 { width: 16.66%; } .col-3 { width: 25%; } @media only screen and (max-width: 768px) { /* For mobile ...

Resolving problems with Ajax and JSON

I am having trouble with an AJAX call to load JSON data into the DOM. Even though the data is loaded in the network tab, it doesn't appear on the page and instead shows [object Object]. $.getJSON('json/redstone.json', function(data) { ...

Adjust text dynamically using PHP

Hello everyone, I am currently working on a form where I would like to have a text displayed and calculate the price when a user selects an option from two dropdown menus. The first dropdown menu includes: <select id="recipient" name="recipient" tabin ...

Troubleshooting: Unable to get the Bootstrap active navigation code to function

I've been struggling to make this code work in order to add the active class to my navigation list item, but it just won't cooperate. Home <li id="orange"> <a href="hotels.php"><span class="glyphicon glyphico ...

JavaScript event triggered upon full completion of page rendering

Similar Question: How can I execute a JavaScript function after the page has fully rendered? I am aware that the onload() event can be utilized to perform actions once the page is completely loaded. Is there an equivalent event that signifies when the ...

I am having trouble styling a pre-existing class in bootstrap using my custom CSS file

html <section id="title"> <div class="container-fluid"> <!-- Nav Bar --> <nav class="navbar navbar-expand-lg navbar-dark"> <a class=" navbar-brand">tindog</a> ...

Designing HTML Emails Using Nested Tables

Why is my text-decoration="none" not displaying properly? I have tried placing it in various locations like table, tr, td, but I am unable to make it work. Are there any common mistakes when styling email designs? Here is the code snippet: <table cel ...