Creating a 3D Cube Effect using CSS Tutorial
In this article, you can learn to create a simple 3D Cube Effect transition using CSS. This tutorial's main purpose is to provide students and beginners with a reference for learning some CSS techniques and tricks to build or develop creative user interfaces or designs. Here, I will be providing simple web page scripts that demonstrate the creation of a simple HTML element with a 3D Cube Effect.
What is the 3D Cube Effect?
The 3D Cube Effect is a 3-dimensional effect that simulates the transition or animation like an actual cube. This simple effect can be used for any HTML block element.
How to create a 3D Cube Effect?
We can simply achieve the 3D Cube Effect animation or transition for HTML elements using only Pure CSS. CSS comes with multiple useful properties which allow us to create 3D effects such the transform, transform-style, and perspective. These 3 CSS properties allow us to achieve the cube effect. Check out the following sample web page script that I created to understand more about how can you create a 3D cube effect animation.
About the Sample Web Page
The scripts that I will be providing will result in a simple web page that contains a single box. The 3D cube effect will be triggered when the user hovers over the box. When the box has hovered, another box will be shown from the top of the current box and the current box will be hidden using the 3D cube effect as the transition of changes.
HTML
Here's the HTML file script of the web page named index.html. It contains the HTML elements used for creating the Cube Effect.
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="preconnect" href="https://fonts.googleapis.com">
- <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
- <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" />
- <link rel="stylesheet" href="style.css">
- </head>
- <body>
- <div id="wrapper">
- <hr style="margin:auto; width:25px">
- <div id="cube-wrapper">
- <div class="cube">
- <div class="top-box">
- Top View
- </div>
- <div class="front-box">
- Hover Me
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
Stylesheet
Here's the CSS file script named style.css that contains the styles or the code for designing the page layout and the box element.
- @import url('https://fonts.googleapis.com/css2?family=Courgette&family=Secular+One&display=swap" rel="stylesheet');
- :root{
- --secular-font: 'Secular One', sans-serif;
- }
- *{
- box-sizing: border-box;
- }
- body *{
- font-family: 'Rubik', sans-serif;
- }
- /**
- Page Design
- */
- body,
- html{
- height: 100%;
- width: 100%;
- margin: 0;
- padding: 0;
- }
- body{
- background-color: #363636;
- }
- /* Page Wrapper */
- #wrapper{
- width: 100%;
- padding: 3em 5em;
- }
- .page-title{
- font-size: 2.5rem;
- font-weight: 500;
- color: #fff;
- letter-spacing: 3px;
- font-family: var(--secular-font);
- text-align: center;
- }
- /* Cube Wrapper and Container */
- #cube-wrapper{
- width: 200px;
- height: 200px;
- margin: auto;
- margin-top: 2em;
- }
- .cube{
- width: 100%;
- height: 100%;
- position: relative;
- transform-style: preserve-3d;
- perspective: 450px;
- perspective-origin: top;
- transition: transform .5s ease;
- }
- .front-box,
- .top-box{
- position: absolute;
- width: 100%;
- height: 100%;
- top: 0;
- left: 0;
- box-shadow: 0px 0px 7px #fff;
- transition: transform .5s ease;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 40px;
- font-weight: 800;
- color: #fff;
- text-shadow: 0px 0px 5px #000000;
- letter-spacing: 5px;
- text-align: center;
- }
- .top-box{
- background-color: #B6E2A1;
- transform: translate(0, -100%) rotateX(90deg);
- transform-origin: bottom;
- }
- .front-box{
- /* display: none; */
- background-color: #AEE2FF;
- transform-origin: top;
- }
- .cube:hover .top-box{
- transform: translate(0, 0) rotateX(0deg);
- }
- .cube:hover .front-box{
- transform: translate(0, 100%) rotateX(-90deg);
- }
The script above contains all the properties needed to create a 3D cube effect when hovering the element on the web page.
Snapshots
On page load, the box on the web page will display like the following image.
The following image is the snapshot of the box while it is transitioning to display the top view of the cube.
After the successful transition, the box will be displayed like the following image.
DEMO
Here's the overall result of the scripts that I provided above.
There you go! I have also provided the complete source code zip file of the scripts and it is free to download on this site. To download it, you can simply click the download button located below this tutorial's content.
That's it! I hope this Creating a 3D Cube Effect using CSS Tutorial will help you with what you are looking for and will be useful for your current and future web application projects.
Happy Coding =)
Comments
Add new comment
- Add new comment
- 347 views