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:

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

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

How to extract a div from its parent using jQuery

I need to stack two div boxes on top of each other, and my goal is to hide the upper one when it's being hovered over. HTML: <div id="left_middle"> <div id="rfinder_UP"></div> <div id="rfinder_DWN"></div> </div> C ...

Adding a numerical value to the conclusion of the php variable

It may seem like a basic question, but it has been bothering me for quite some time now... I have a PHP file that is included on a PHP web page to retrieve dynamic content from my MySQL database. Everything works smoothly with this setup except when I tr ...

Print custom jQuery attribute to console or log output

I need help retrieving and displaying a custom jQuery attribute in either an alert or console. Despite attempting to use console.log() to access my custom data attribute, I am unable to see its value appear. The specific value I am trying to display is d ...

Are there any options available for customizing the animation settings on the UI-bootstrap's carousel feature?

If you're interested in seeing an example of the standard configuration, check out this link. It's surprising how scarce the documentation is for many of the features that the UIB team has developed... I'm curious if anyone here has experie ...

Tips for enlarging an image by tapping on it in handlebars

I currently have the handlebars template engine integrated with node.js, and I'm facing an issue where thumbnail images from the database are displaying at a fixed width and height of 70. Is there a way to enable users to click on these images in orde ...

Switch up the CSS variable within an embedded iframe

I'm in a predicament with a specific issue. I am currently utilizing Angular to incorporate an Iframe. Let's imagine the angular app as A and the Iframe as B. B is being loaded within A. Within B, I have utilized CSS variables to define colors. I ...

Troubleshooting CSS compatibility issues in Firefox browser (or HTML rendering as plain text)

In a unique web page comparing two photos, the clicked one changes, but there's an issue in Firefox where HTML displays as text and links don't work. CODE:- * { box-sizing: border-box; } html { height: 100%; } body { height: 100%; mar ...

Maximizing Bootstrap: Enabling dual dropdown options side by side on a single row

I am attempting to design a login form within a Bootstrap navbar dropdown. My goal is to have the bottom item display two items side by side in the same row (login/register), but I am struggling to achieve this. You can view what it currently looks like h ...

Parallax Effect in CSS Background

I am currently working on my website at and have implemented a "parallax-bg" class to create a parallax background effect. Interestingly, the parallax effect is working flawlessly for the hero section at the top and the texture in the middle, but it&apos ...

Ways to create two distinct "on click" actions for a single image to execute two distinct tasks

Clicking on an image will open a slider showing all images before filtering. Once filtered, only the selected images will be displayed in the slider. <div class="gallery row" id="gallery" style="margin:0;"> <!-- Grid column --> <div ...

Stop the caching of HTML pages

After adding the lines below to prevent caching and display the content within an iframe, there seems to be no effect: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-Equiv="Cache-Control" Content="no-cache"&g ...

Tips on fetching data from a different webpage via ajax

Hello, I am new to programming. I am looking for a way to update the content of the current PHP page with content from another PHP page using AJAX without needing to refresh the page. The content that needs to be replaced is located within div elements. ...

What is the best way to retrieve an <a> tag element using Selenium in Python?

I am fairly new to using Selenium and could use some guidance with the following issue. I am attempting to create a birthday bot for Facebook, but navigating through the platform's latest UI has been challenging with JavaScript disabled. I've bee ...

Using Tailwind CSS's width property w-11/12 actually returns a full 100% width instead of the expected 91.66

It seems that the w-11/12 class is not behaving as expected. According to the documentation, it should give a width of 91.666% but instead, it's filling up 100%. This issue only occurs with the value 11, while other values work fine. Has anyone else e ...

Difficulties encountered when trying to send emails on iPhone with HTML formatting and attachments

I'm attempting to send a JPG attachment along with a formatted HTML message using [picker setMessageBody:emailBody isHTML:YES]; as recommended by several forum posts. However, what I've noticed is that the resulting email's content varies d ...

Having trouble implementing styles for an element selected using the document.getElementsByClassName() method

In order to adjust the style of a dropdown menu, I need to change its top value to align it properly. The element is being generated by Drupal (a CMS tool) and is configured with classes for styling purposes in the admin view where content is authored. How ...

Please indicate a width of 3 characters for the HTML input text

Is there a way to create an HTML text box that can only fit 3 characters exactly? I came across this code online: <input type="text" style="width: 10px; padding: 2px; border: 1px solid black"/> This code creates a text box with a width of 10px ...

Clickability issue with searchbar results caused by onBlur event

My searchbar functionality includes showing results in a dropdown list when the user searches. However, I am facing an issue with the onBlur event that changes the display of the list to none when the user clicks away from the search box. The problem aris ...

Changing the color of a Datatable row through a SQL query in an ASP.NET C# application

I am looking to add color to specific rows in my asp.net c# table. I have a table displayed using an ASP:Repeater with HTML: <div class="panel-body"> <div class="table-responsive"> <table class="table table-striped table-bordered tabl ...

I am unable to see the last row of the table when I apply a condition after the table name

I need to display all the data from my table. However, if I include ORDER BY id DESC or any code after $sql="SELECT * FROM $tbl_name, the last row does not appear. <?php include "db.php"; $tbl_name="report"; // Table name $sql="SELECT * FROM $tbl_ ...