In this article, we will walk through how to create a stunning 3D wave animation effect using HTML and CSS. This interactive animation responds to user actions, creating a smooth transition and depth effect.
Step 1: The HTML Structure
We begin by setting up a basic HTML structure. We use a div
container to hold the items we want to animate.
3D wave animation
Explanation:
.wrapper
: A flex container that centers the content on the screen..items
: Contains multiple items (images in this case) that will participate in the 3D animation..item
: Each item contains a background image that reacts to hover and focus events.
Step 2: The CSS Styling
The magic of the 3D animation happens entirely in the CSS file. We’ll use several CSS properties like perspective
, transform
, and transition
to create the wave effect.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
:root{
--index: calc(1vw + 1vh);
--transition: cubic-bezier(.1, .7, 0, 1);
}
body{
background-color: #141414;
}
.wrapper{
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.items{
display: flex;
gap: 0.4rem;
perspective: calc(var(--index) * 35);
}
.item{
width: calc(var(--index) * 3);
height: calc(var(--index) * 12);
background-color: #222;
background-size: cover;
background-position: center;
cursor: pointer;
filter: grayscale(1) brightness(.5);
transition: transform 1.25s var(--transition), filter 3s var(--transition), width 1.25s var(--transition);
will-change: transform, filter, rotateY, width;
}
.item::before, .item::after{
content: '';
position: absolute;
height: 100%;
width: 20px;
right: calc(var(--index) * -1);
}
.item::after{
left: calc(var(--index) * -1);
}
.items .item:hover{
filter: inherit;
transform: translateZ(calc(var(--index) * 10));
}
/*Right*/
.items .item:hover + *{
filter: inherit;
transform: translateZ(calc(var(--index) * 8.5)) rotateY(35deg);
z-index: -1;
}
.items .item:hover + * + *{
filter: inherit;
transform: translateZ(calc(var(--index) * 5.6)) rotateY(40deg);
z-index: -2;
}
.items .item:hover + * + * + *{
filter: inherit;
transform: translateZ(calc(var(--index) * 2.5)) rotateY(30deg);
z-index: -3;
}
.items .item:hover + * + * + * + *{
filter: inherit;
transform: translateZ(calc(var(--index) * .6)) rotateY(15deg);
z-index: -4;
}
/*Left*/
.items .item:has( + :hover){
filter: inherit;
transform: translateZ(calc(var(--index) * 8.5)) rotateY(-35deg);
}
.items .item:has( + * + :hover){
filter: inherit;
transform: translateZ(calc(var(--index) * 5.6)) rotateY(-40deg);
}
.items .item:has( + * + * + :hover){
filter: inherit;
transform: translateZ(calc(var(--index) * 2.5)) rotateY(-30deg);
}
.items .item:has( + * + * + * + :hover){
filter: inherit;
transform: translateZ(calc(var(--index) * .6)) rotateY(-15deg);
}
.items .item:active, .items .item:focus {
width: 28vw;
filter: inherit;
z-index: 100;
transform: translateZ(calc(var(--index) * 10));
margin: 0 .45vw;
}
Explanation:
perspective
: This creates the depth effect, making the items appear to pop out when hovered over.transform
: ThetranslateZ
property allows elements to move along the z-axis, giving the illusion of depth. We also userotateY
to slightly rotate neighboring items, creating a wave-like effect.transition
: This smoothens the transformation by specifying the duration and easing function.:hover
: When you hover over an item, it moves forward, while adjacent items tilt backward or forward to complete the wave.
Step 3: Adding Interactive Elements
We also made the items focusable using the tabindex="0"
attribute, so the effect can be triggered with both mouse hover and keyboard navigation.
video Preview Of 3D Wave Animation
Conclusion
Creating a 3D wave animation using just HTML and CSS demonstrates the power and flexibility of modern web technologies. With the right use of CSS properties like perspective
, transform
, and transition
, we can add depth and interactivity to otherwise static web elements, making them visually engaging.
This project not only enhances user experience but also shows how CSS can be leveraged to achieve complex animations without relying on JavaScript. The use of hover effects and keyboard navigation ensures accessibility, making the design functional for all users.
With a little creativity, CSS animations can bring your web pages to life and create an immersive experience for users. Feel free to experiment with additional transitions, different item layouts, or even add custom animations to further enhance your design.
Let’s continue exploring the endless possibilities of CSS!
If you want to master web development from basic to advanced, be sure to check out my e-books for an in-depth guide!