CSS Viewer Cube in React without Three.js
Last Updated :
12 Mar, 2024
A CSS viewer cube is a visually appealing and interactive way to display images or content on a webpage. It resembles a 3D cube that can be rotated along its axes to reveal different faces. While traditionally, libraries like Three.js have been used to achieve this effect.
In this article, we'll explore how to create a CSS viewer cube in React without relying on Three.js.
We will discuss about the approaches of creating a CSS viewer cube without Three.js using React:
Steps to Create a React Application and Installing Module:
Step 1: Create a React App using the following command.
npx create-react-app my-react-app
Step 2: Switch to the project directory
cd my-react-app
Step 3: Installing the required packages:
npm install styled-components react-icons
Updated Dependencies in package.json File:
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2",
"styled-components": "^5.3.3",
"react-icons": "^4.3.1"
}
Example: This approach involves defining CSS animations for the cube's rotation and using CSS transforms to manipulate its orientation.
JavaScript
import React from 'react';
import styled, {
keyframes
} from 'styled-components';
const rotateAnimation = keyframes`
from {
transform: rotateX(0deg) rotateY(0deg);
}
to {
transform: rotateX(360deg) rotateY(360deg);
}
`;
const CubeContainer = styled.div`
width: 200px;
height: 200px;
position: relative;
perspective: 800px;
`;
const Cube = styled.div`
width: 100%;
height: 100%;
position: absolute;
transform-style: preserve-3d;
animation: ${rotateAnimation} 10s infinite linear;
`;
const CubeFace = styled.div`
width: 200px;
height: 200px;
position: absolute;
background-color: ${({ color }) => color};
border: 2px solid black;
`;
const FrontFace = styled(CubeFace)`
transform: translateZ(100px);
`;
const BackFace = styled(CubeFace)`
transform: translateZ(-100px) rotateY(180deg);
`;
const RightFace = styled(CubeFace)`
transform: rotateY(90deg) translateZ(100px);
`;
const LeftFace = styled(CubeFace)`
transform: rotateY(-90deg) translateZ(100px);
`;
const TopFace = styled(CubeFace)`
transform: rotateX(90deg) translateZ(100px);
`;
const BottomFace = styled(CubeFace)`
transform: rotateX(-90deg) translateZ(100px);
`;
const CubeViewer = () => {
return (
<CubeContainer>
<Cube>
<FrontFace color="red"></FrontFace>
<BackFace color="blue"></BackFace>
<RightFace color="green"></RightFace>
<LeftFace color="yellow"></LeftFace>
<TopFace color="orange"></TopFace>
<BottomFace color="purple"></BottomFace>
</Cube>
</CubeContainer>
);
};
export default CubeViewer;
Output:

CSS Viewer Cube React State and Event Handling:
Example: Here, we'll use React state to store the current rotation state of the cube and handle events to update it accordingly.
JavaScript
import React, { useState, useEffect } from 'react';
import styled from 'styled-components';
const CubeContainer = styled.div`
width: 200px;
height: 200px;
position: relative;
perspective: 800px;
margin:250px;
`;
const Cube = styled.div`
width: 100%;
height: 100%;
position: absolute;
transform-style: preserve-3d;
transform: ${({ rotation }) => rotation};
`;
const CubeFace = styled.div`
width: 200px;
height: 200px;
position: absolute;
background-color: ${({ color }) => color};
border: 2px solid black;
`;
const FrontFace = styled(CubeFace)`
transform: translateZ(100px);
`;
const BackFace = styled(CubeFace)`
transform: translateZ(-100px) rotateY(180deg);
`;
const RightFace = styled(CubeFace)`
transform: rotateY(90deg) translateZ(100px);
`;
const LeftFace = styled(CubeFace)`
transform: rotateY(-90deg) translateZ(100px);
`;
const TopFace = styled(CubeFace)`
transform: rotateX(90deg) translateZ(100px);
`;
const BottomFace = styled(CubeFace)`
transform: rotateX(-90deg) translateZ(100px);
`;
const CubeViewer = () => {
const [rotation, setRotation] = useState({ x: 0, y: 0 });
useEffect(() => {
const handleMouseMove = (e) => {
setRotation({ x: -e.clientY, y: e.clientX });
};
document.addEventListener('mousemove',
handleMouseMove);
return () => {
document.removeEventListener('mousemove',
handleMouseMove);
};
}, []);
return (
<CubeContainer>
<Cube rotation={`
rotateX(${rotation.x}deg) rotateY(${rotation.y}deg)`}>
<FrontFace color="red"></FrontFace>
<BackFace color="blue"></BackFace>
<RightFace color="green"></RightFace>
<LeftFace color="yellow"></LeftFace>
<TopFace color="orange"></TopFace>
<BottomFace color="purple"></BottomFace>
</Cube>
</CubeContainer>
);
};
export default CubeViewer;
Output:

Conclusion:
Creating a CSS viewer cube in React without using Three.js is achievable through CSS animations, React state management, and CSS layout techniques like Grid or Flexbox. By combining these approaches, you can create engaging and interactive 3D effects for your React applications.
Similar Reads
CSS Viewer Cube without Three.js using Vanilla JavaScript
Creating a CSS viewer cube without three.js using Vanilla JavaScript offers a lightweight alternative for rendering 3D cubes in web applications. This article explores various approaches to implementing a CSS viewer cube without relying on three.js, providing developers with flexibility and simplici
4 min read
React.js without JSX
JSX: Consider the following code snippet, const sample = <h2>Greetings</h2>; The above code snippet somewhat looks like HTML, and it also uses a JavaScript-like variable but is neither HTML nor JavaScript, it is JSX. The JSX is basically a syntax extension of regular JavaScript and is us
2 min read
How to create a Responsive Carousel in React JS ?
A carousel, often seen in web and mobile applications, is a user interface component used to display a set of content items such as images or text in a rotating manner. It allows users to view multiple items within a limited space by scrolling horizontally or vertically through the content. In this
2 min read
Rotating 3D Image Previewer Cube using CSS
The 3D Rotating Image Previewer cube is an effect in which a set of images appear on the faces of a revolving 3D cube. This effect can be created using HTML and CSS. Approach: The best way to animate HTML objects is by using CSS @keyframes and by setting transition state at different animation state
3 min read
Install & Setup Tailwind CSS with Next.js
Tailwind is a popular utility first CSS framework for rapidly building custom User Interfaces. It provides low-level classes, those classes combine to create styles for various components. You can learn more about Tailwind CSS here. Next.js: Next.js is a React-based full-stack framework developed b
3 min read
Create a Video Streaming Platform with React
This article focuses on Creating and Designing a Video Streaming Application using React JS. In this application, users can stream any video just by entering the video link in the input field, the preview and streaming of the video is started in the application itself. Proper error messages are show
4 min read
How to Set Box Shadow in React JS ?
The CSS property box-shadow is used to set box shadow in React. This can apply to any React.js component. The CSS box-shadow property creates shadow effects around an element's frame possible to set several effects at once by separating them with commas. A box shadow is defined by the element's X an
3 min read
How to use TreeView Component in ReactJS ?
Explore the functionality of ReactJS's TreeView component, a powerful tool for seamlessly navigating and displaying hierarchical structures in web applications. This comprehensive guide delves into the integration process, empowering you to represent complex data relationships in an organized and us
2 min read
How to create a simple Responsive Footer in React JS ?
A responsive Footer in the React JS application is part of webpages that adjusts layout dynamically and signals to the user that they have reached the end of the webpage and provides useful links to other areas of the website that the user may want to visit. Preview Image of Final Output: Prerequisi
3 min read
How to Implement Reveal on Scroll in React using Tailwind CSS ?
In React, the reveal on the scroll is a technique, where elements on a page gradually appear or animate as the user scrolls down. Prerequisites:NPM & Node.jsReact JSTailwind CSSTo implement reveal in scroll in react using Tailwind CSS we have these methods: Table of Content Using the Intersectio
4 min read