Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9169795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:55:41+00:00 2026-06-17T15:55:41+00:00

I have been playing with CSS trying to create a 3d box that you

  • 0

I have been playing with CSS trying to create a 3d box that you can select the face with vanilla javascript.

It is simply changing the className of the box divs and using the transition property to smoothly transition between locations.

here is a jsfiddle to show a working example http://jsfiddle.net/synthet1c/VdDmA/1/

It looks cool at the moment but it is not quite behaving the way I want… Does anyone know how I can keep the box solid when it is going through it’s transition? currently if the face is going from 360deg to 90deg the face will rotate 270deg in the wrong direction. I understand why it’s doing it but can’t make a workaround for it.

I added all the browser prefixes but have only used it on firefox.

cheers for any advice,

Andrew
#right, #back, #left, #front
{
height: 150px;
width: 150px;
position: absolute;
border: 1px solid rgba(200,200,200,0.7);
background-color: rgba(0,0,255,0.5);
margin: 0px;
}

.right{
    transform: rotateY(90deg) translatez(75px) translatex(-75px);
    transition: all 4s;
}

.back{
    transform: rotateY(180deg) translatez(0px) translatex(0px);
    transition: all 4s;
}

.left{
    transform: rotateY(270deg) translatez(75px) translatex(70px);
    transition: all 4s;
}

.front{
    transform: rotateY(0deg) translatez(150px) translatex(0px);
    transition: all 4s;
}

var id = function(elem){
    var theId = document.getElementById(elem);
    return theId;
}

function button1(){
    id('front').className = 'front';
    id('right').className = 'right';
    id('back').className = 'back';
    id('left').className = 'left';
}

function button2(){
    id('front').className = 'right';
    id('right').className = 'back';
    id('back').className = 'left';
    id('left').className = 'front';
}   

function button3(){
    id('front').className = 'back';
    id('right').className = 'left';
    id('back').className = 'front';
    id('left').className = 'right';
}   

function button4(){
    id('front').className = 'left';
    id('right').className = 'front';
    id('back').className = 'right';
    id('left').className = 'back';
}   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:55:42+00:00Added an answer on June 17, 2026 at 3:55 pm

    It’s actually pretty simple. You just need to do a check to see whether the difference, let’s call it dif, in absolute value between the angle you’re currently at and the angle you want to get to is over 180° and if it is, you rotate by 360° minus dif in absolute value, the direction of the rotation being given by the sign of dif.

    I’ve also made some changes to the CSS, and if you want to understand more about how you can create a realistic looking cube, you can check my (really) detailed answer to a similar question.

    demo

    HTML:

    <div class='buttons'>
        <button class='btn'>Front</button>
        <button class='btn'>Left</button>
        <button class='btn'>Back</button>
        <button class='btn'>Right</button>
    </div>
    <div class='house'>
        <div class='face front'>Front</div>
        <div class='face back'>Back</div>
        <div class='face right'>Right</div>
        <div class='face left'>Left</div>
    </div>
    

    Relevant CSS:

    body /* or parent of .house */ { perspective: 45em; }
    .house { position: relative; transform-style: preserve-3d; transition: 1s; }
    .house, .face { width: 10em; height: 10em; }
    .face {
        box-sizing: border-box;
        position: absolute;
        padding: 1em;
    }
    .front { transform: translateZ(5em); background: rgba(255, 165, 0, .75); }
    .back { 
        transform: rotateY(180deg) translateZ(5em);
        background: rgba(30, 144, 255, .75);
    }
    .right {
        transform: rotateY(90deg) translateZ(5em);
        background: rgba(220, 20, 60, .75);
    }
    .left {
        transform: rotateY(-90deg) translateZ(5em);
        background: rgba(127, 255, 0, .75);
    }
    

    JavaScript:

    (function(){
        var btnsEl = document.querySelector('.buttons'), currentAngle = 0;
        btnsEl.addEventListener('click', function(e){
            var b = e.target.innerHTML.toLowerCase(), 
                house = document.querySelector('.house'), 
                btns = {'front': 0, 'left': 90, 'back': 180, 'right': -90}, 
                dif = btns[b] - currentAngle%360;
            if(!e.target.classList.contains('btn') || dif === 0) return;
            currentAngle += (Math.abs(dif) > 180) ? 
                            (Math.abs(dif) - 360)*Math.abs(dif)/dif : 
                            dif;
            house.style['-webkit-transform'] = 'rotateY(' + currentAngle + 'deg)';
            house.style['transform'] = 'rotateY(' + currentAngle + 'deg)';
        }, false); 
    }());
    

    Also, you can do a rotating 3D box with pure CSS. However, the > 180° rotation problem can only be solved with JavaScript.

    pure CSS version of the demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been playing with Expect/TCL today and I was hoping someone can tell
I have been playing around with Google Apps Script today and I am trying
I have been playing around with NSURLConnection. Now I'm trying to grab some data
I have been playing around with GC.GetTotalMemory(). When I create a local variable of
I have been trying to center the slider that I have inside the header.
I've been playing with a div I have that changes on a user action.
I have been playing around with a search control and i have noticed that
I have been playing around with some CSS3 + JavaScript today. Below you have
I have been playing around with different scripts, I found one that works for
I have been playing with the Ruby library shoes. Basically you can write a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.