CSS Slide-In Template: Using transform: translateX for Smooth Left/Right Animations
📝 Usage
This template is used when you want to make an element slide in from the left or right.
📘 Explanation
This code uses transform: translateX to slide an element from the left or right. The animation takes 4 seconds to complete.
🔹 Partial Code
<style>
.slide-in-left {
transform: translateX(-100%);
animation: slideInLeft 4s ease-out forwards;
}
.slide-in-right {
transform: translateX(100%);
animation: slideInRight 4s ease-out forwards;
}
@keyframes slideInLeft {
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(0);
}
}
@keyframes slideInRight {
0% {
transform: translateX(100%);
}
100% {
transform: translateX(0);
}
}
</style>
💻 Full Working Code
<div class="slide-in-left">
Element sliding in from the left
</div>
<div class="slide-in-right">
Element sliding in from the right
</div>
<style>
.slide-in-left {
transform: translateX(-100%);
animation: slideInLeft 4s ease-out forwards;
}
.slide-in-right {
transform: translateX(100%);
animation: slideInRight 4s ease-out forwards;
}
@keyframes slideInLeft {
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(0);
}
}
@keyframes slideInRight {
0% {
transform: translateX(100%);
}
100% {
transform: translateX(0);
}
}
</style>
🚀 Practical Uses of Slide-In Animations
Slide-in animations using transform: translateX are an effective way to add dynamic elements to web pages. This technique offers more than just decoration; it can be applied to important UI elements that capture user attention.
🎯 Effective Use Cases
Slide-in from the left (.slide-in-left) is ideal for displaying new content or important notifications. On the other hand, slide-in from the right (.slide-in-right) is often used for sidebar menus or supplementary information. Shortening the animation duration from 4s to 2s, for example, can create a snappier impression.
⚡ Performance Optimization
The transform property benefits from GPU acceleration, making animations smoother compared to those using left or margin. Adding will-change: transform; gives the browser a hint to optimize rendering.
🔧 Customization Examples
If you want to add a delay to the animation, you can add animation-delay: 0.5s;. Also, using custom easing functions like cubic-bezier(0.68, -0.55, 0.265, 1.55) instead of ease-out can create more unique movements.
In responsive design, it’s important to disable animations on mobile devices or shorten their duration. This can be achieved with @media queries, helping to balance usability and performance.