Utilizing CSS to create dynamic 3D cube transformations

Exploring the realm of CSS 3D transforms to showcase cubes and cuboids featuring cross-sections has been my current venture. In this endeavor, I'm aiming at compatibility with Chrome, Firefox, and MSIE 11. It has come to my attention that in order to keep MSIE 11 on board, steering clear of transform-type: preserve-3d is paramount since Microsoft does not support it. Consequently, every parent and child transform must be applied to each face of the cube.

While tackling a cube poses no significant challenge when aligning its sides by rotating them correctly, things take a baffling turn when dealing with a cuboid where the ends seem to be slightly misaligned. What causes this discrepancy, and how can it be rectified?

The issue is visually depicted in the following screenshot:

The corresponding HTML goes like this:

<div class="test test1">
    <h1>1.</h1>
    <div class="cube">
        <div class="side front">1</div>
        <div class="side back">6</div>
        <div class="side right">4</div>
        <div class="side left">3</div>
        <div class="side top">5</div>
        <div class="side bottom">2</div>
    </div>
</div>

<div class="test test2">
    <h1>2.</h1>
    <div class="cube cuboid">
        <div class="side front">1</div>
        <div class="side back">6</div>
        <div class="side right">4</div>
        <div class="side left">3</div>
        <div class="side top">5</div>
        <div class="side bottom">2</div>
    </div>
</div>

Meanwhile, the CSS for these elements stands as follows:

    div.test {
        height: 200px;
    }

    /* basic cube */
    .cube {
        font-size: 4em;
        width: 500px;
        margin: auto;
        /* MSIE11 does not support preserve-3d.
          for MSIE all transforms must be applied to all elements */
    }

    .side {
        position: absolute;
        width: 100px;
        height: 100px;
        background: rgba(255,0,0,0.3);
        border: 1px solid black;
        color: white;
        text-align: center;
        line-height: 100px;
    }
    /* for MSIE11 compatibility, avoid using a transform on the parent, combine all parent+child transforms, transform-style: preserve3d is not supported */
    .front {
        transform: rotateX(-40deg) rotateY(32deg) translateZ(50px);
        z-index: 1000;
    }
    .top {
        transform: rotateX(-40deg) rotateY(32deg) rotateX(90deg) translateZ(50px);
        z-index: 1000;
    }
    .right {
        transform: rotateX(-40deg) rotateY(32deg) rotateY(90deg) translateZ(50px);
    }
    .left {
        transform: rotateX(-40deg) rotateY(32deg) rotateY(-90deg) translateZ(50px);
        z-index: 1000;
    }
    .bottom {
        transform: rotateX(-40deg) rotateY(32deg) rotateX(-90deg) translateZ(50px);
    }
    .back {
        transform: rotateX(-40deg) rotateY(-148deg) translateZ(50px);
    }

    /* cuboid - 100 x 100 x 200 */
    .cuboid .front {
        width: 200px;
    }
    .cuboid .top {
        width: 200px;
    }
    .cuboid .right {
        transform: rotateX(-40deg) rotateY(122deg) translateZ(150px);
    }
    .cuboid .back {
        width: 200px;
    }
    .cuboid .bottom {
        width: 200px;
    }

To see this code in action, check out the JSFiddle link provided here: https://jsfiddle.net/6h7mmtgn/

Your insights and suggestions are greatly appreciated.

Answer №1

.cuboid .left,
.cuboid .right {
     margin-top: 16px;
     margin-left: 7px;
}

See the demonstration below:

div.example {
    xwidth: 100%;
    xperspective: 750px;
    height: 200px;
}
/* simple cube */
 .cube {
    font-size: 4em;
    width: 500px;
    margin: auto;
    /* MSIE11 does not support preserve-3d.
  for MSIE all transforms must be applied to all elements */
}
.side {
    position: absolute;
    width: 100px;
    height: 100px;
    background: rgba(255, 0, 0, 0.3);
    border: 1px solid black;
    color: white;
    text-align: center;
    line-height: 100px;
}
/* for MSIE11 compatibility, avoid using a transform on the parent, combine all parent+child transforms, transform-style: preserve3d is not supported */
 .front {
    transform: rotateX(-40deg) rotateY(32deg) translateZ(50px);
    z-index: 1000;
}
.top {
    transform: rotateX(-40deg) rotateY(32deg) rotateX(90deg) translateZ(50px);
    z-index: 1000;
}
.right {
    transform: rotateX(-40deg) rotateY(32deg) rotateY(90deg) translateZ(50px);
}
.left {
    transform: rotateX(-40deg) rotateY(32deg) rotateY(-90deg) translateZ(50px);
    z-index: 1000;
}
.bottom {
    transform: rotateX(-40deg) rotateY(32deg) rotateX(-90deg) translateZ(50px);
}
.back {
    transform: rotateX(-40deg) rotateY(-148deg) translateZ(50px);
}
/* cuboid - 100 x 100 x 200 */
 .cuboid .front {
    width: 200px;
}
.cuboid .top {
    width: 200px;
}
.cuboid .right {
    transform: rotateX(-40deg) rotateY(122deg) translateZ(150px);
}
.cuboid .back {
    width: 200px;
}
.cuboid .bottom {
    width: 200px;
}
.cuboid .left, .cuboid .right {
    margin-top: 16px;
    margin-left: 7px;
}
<div class="example test1">
    <h1>1.</h1>
    <div class="cube">
        <div class="side front">1</div>
        <div class="side back">6</div>
        <div class="side right">4</div>
        <div class="side left">3</div>
        <div class="side top">5</div>
        <div class="side bottom">2</div>
    </div>
</div>
<div class="example test2">
    <h1>2.</h1>
    <div class="cube cuboid">
        <div class="side front">1</div>
        <div class="side back">6</div>
        <div class="side right">4</div>
        <div class="side left">3</div>
        <div class="side top">5</div>
        <div class="side bottom">2</div>
    </div>
</div>

View on JSFiddle

Answer №2

In my opinion, the issue lies with the transform-origin property. You may want to consider setting specific origins for each side of your element using the following code:

transform-origin: 51px 51px;

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

Tips for Changing the CSS of an External Component with Material-UI-React

Is there a way to override the default CSS of an external component that is not developed in Material-UI or my project? In styled-components, I can simply take the root classes and replace them with my own custom CSS. How can I achieve a similar result wit ...

Modify the td attributes while retaining the extracted data values from the website

I'm currently utilizing this code to extract data from another website: $searchURL = "http://www.anotherwebsite.com"; $html = file_get_contents($searchURL); $patternform = '/(<tbody.*<\/tbody>)/sm'; preg_match_all($patternfor ...

The content contained within the .each loop within the click event is only executed a single time

While working on coding a menu opening animation, I encountered an issue today. Upon clicking the menu button, the menu opens and the elements inside receive an added class (resulting in a fade-in effect). Clicking the menu button again should close the ...

Incorrect synchronization in the SVG arrow animation

How come the arrow doesn't start moving at the same time as the line? Is there a synchronization issue? I want the arrow to begin its journey simultaneously with the line. .container{ width:100%; padding:0px; background-color: black; } .squig ...

Eliminate extraneous space with the clearfix:after pseudo-element

Could use some help figuring out how to remove the unwanted whitespace caused by clearing the float (specifically after the tabs). Any suggestions on how to fix this issue? Take a look at the code snippet below (jsfiddle): /* Clearfix */ .clearfix:af ...

substitute tags

Is there a way to change the tagName of a tag using jQuery? For example, if I have an HTML document and I want to replace all 'fieldset' with 'div'. Any suggestions would be appreciated! ...

New ways to style Links in NextJs 13

I am relatively new to Next.js and I am attempting to create a menu with a hover effect using the following code: import Link from 'next/link'; const MenuItem = ({ href, label }) => ( <Link href={href} className="menu-item"> ...

Adaptive Container with Images that are not stretched to full width

Is there a way to achieve the same effect as seen in images 2 and 3 here: Although these images already have their own "padding," I'm curious if it can be replicated using just jQuery and CSS? I would appreciate any help or insights on this. Thank y ...

Preventing the cascading effects of past hovers with AngularJS

I am working with AngularJS and have the following HTML code snippet. <div class="row" style="margin-left:60px; text-align:center;"> <div class="col-xs-1 " style="margin-left:25px;width:10px; " ng-repeat="image_thumb_id in image_thumbnail_ ...

Utilizing HTML and JQuery to clear form values

I am attempting to transfer the form data along with an additional parameter "id" to the server using Python and Google App Engine. This is my form: <form method="post" action="/" id="form1" runat="server" enctype='multipart/form-data'> ...

Is it possible to restore the text to its original state?

Currently, I'm working on creating a simple website for server users to order items. However, I've encountered an issue related to the Navigator text. Here is how it currently appears: Upon inspecting the code below, you'll notice that the ...

Animating elements within Firefox can sometimes cause adjacent elements to shift positions

Currently, I am working on a single-page website that utilizes keyboard-controlled scrolling. To achieve the desired scroll effect, I have developed a JavaScript function that animates divs. Here is the JavaScript code: var current_page = "#first"; func ...

Tips for positioning the overlay to match the icon list when hovering- JavaScript/Cascading Style Sheets (CSS)

My challenge involves a list of <li>'s accompanied by an icon that, when hovered over, displays an overlay containing information about the 'test'. The setup looks something like this: test1 test2 test3 and so forth.... Here' ...

Attempting to display a larger version of an image sourced from miniature versions fetched with the assistance of PHP and

I'm dealing with the challenge of displaying thumbnails fetched from a database. PHP is successfully interacting with and presenting my thumbnails. I'm currently faced with the issue of passing the id from the database to the imageID in my JavaSc ...

The Tailwind Typography plugin simply maintains the default font size of heading tags without altering their style

I've been working on parsing an MDX file and converting it into React components. My tech stack includes TailwindCSS, Next.js, and React. Following the Tailwind documentation, I have installed the tailwind/typography plugin. In my tailwind.config.js ...

Is it possible to refresh the chat-box using PHP?

I recently created a chat box application using PHP, but I'm facing an issue with its automatic reload functionality. Is there a way to implement auto-reload in PHP itself, or would it be better to restructure the system to utilize AJAX? Additionally, ...

When you hover over the image, the text will disappear

When I hover over the image of LTC, a thumbnail appears with text and a button. However, I do not want the text "LTC" to be shown when hovering over the image. <div class="col-md-4"> <div class="view view-eighth"> <div class=" ...

Integrate a scrollbar seamlessly while maintaining the website's responsiveness

I encountered an issue where I couldn't add a scrollbar while maintaining a responsive page layout. In order to include a scrollbar in my datatables, I found the code snippet: "scrollY": "200px" However, this code sets the table size to 200 pixels r ...

The onClick function for the "div" element is failing to append

I am currently working on a project that involves searching for strings in a database and then appending the selected string to a text box. So far, I have been successful in retrieving the search results from the database and appending them below the text ...

Can the CSS be used to conceal the PNG image while still preserving the drop shadow effect?

I recently applied the "Filter: drop-shadow" effect to my png photo and it worked perfectly. However, I am now interested in hiding the actual png image while retaining the shadow effect. My initial attempt involved adjusting the translate and drop-shadow ...