CSS Slide Animation: Step-by-Step Guide
In this article
You can create a slide animation in CSS with just a few lines: define a @keyframes rule that moves an element from one position to another, then apply it with the animation property (or use transition on a property like transform for a hover-triggered slide). The key is to animate transform: translateX() or translateY() rather than left or margin, because transforms are handled by the GPU and stay smooth. The steps below walk through a working slide-in animation, a hover slide, and the fixes for the things that usually go wrong.
What You'll Need
- A text editor (VS Code, Sublime Text, Notepad++ — anything that saves plain text).
- A modern browser (Chrome, Firefox, Safari, Edge) to preview the result.
- An HTML file with a linked CSS file, or a
<style>block inside the HTML for quick testing. - Basic familiarity with CSS selectors and properties. No JavaScript is required for a pure CSS slide.
Step-by-Step
- Create the HTML element you want to slide. A simple
<div class="slide-box">Slide me</div>is enough. Give it a class so you can target it in CSS without affecting other elements. - Set a starting position and basic styling. In your CSS, give the element a width, height, background colour, and a
transformthat places it off-screen or offset. For a slide-in from the left, usetransform: translateX(-100%);. This is the position the element will animate from. - Define the keyframes. Write
@keyframes slideIn { from { transform: translateX(-100%); } to { transform: translateX(0); } }. Thefromblock is the start state, thetoblock is the end state. You can also use percentages (0%, 50%, 100%) for multi-stage slides. - Apply the animation to the element. Add
animation: slideIn 0.6s ease-out forwards;to the.slide-boxrule.slideInis the keyframes name,0.6sis duration,ease-outis the timing function, andforwardskeeps the element at the final position after the animation ends. Withoutforwards, the element snaps back to its original spot. - Preview and adjust. Open the HTML file in your browser. If the element does not appear to move, check that the keyframes name matches exactly (case-sensitive) and that the element is not hidden by
display: noneoroverflow: hiddenon a parent. Tweak the duration and easing until the motion feels right. - Add a hover-triggered slide (optional). If you want the slide to happen only on hover, skip the
animationproperty and use a transition instead: settransition: transform 0.3s ease;on the base rule, then on.slide-box:hover { transform: translateX(20px); }. The element slides when the pointer enters and slides back when it leaves. - Make it accessible. Wrap the animation in a
@media (prefers-reduced-motion: reduce)query that setsanimation: none;ortransition: none;for users who have asked their system to reduce motion. This is a small addition that prevents discomfort for some visitors. - Test across browsers and screen sizes. Resize the window and check the slide still looks correct. If the element slides off the edge on small screens, use percentage-based transforms or clamp the distance with
calc()rather than fixed pixel values.
Common Mistakes
- Animating
leftormargininstead oftransform. These properties trigger layout recalculations on every frame and cause janky motion. Usetransform: translate()for smooth results. - Forgetting
forwards. Withoutanimation-fill-mode: forwards(or the shorthandforwards), the element jumps back to its pre-animation state when the animation finishes. - Misspelling the keyframes name. CSS is case-sensitive here;
slideinandslideInare different names. A mismatch means no animation at all. - Overflow hidden on a parent. If a parent container has
overflow: hidden, an element sliding in from outside will be clipped and invisible until it enters the parent's box. Check parent styles when the slide seems to do nothing. - Animating
display.displayis not animatable. If you need to show and slide an element, animateopacityandtransformtogether, or toggle a class that changesvisibilityafter the animation.
Tips
- Keep slide durations between 0.2s and 0.8s for UI elements; longer feels sluggish.
- Use
will-change: transform;sparingly on elements that animate frequently — it hints the browser to optimise, but overuse can hurt performance. - Combine slide with fade for a polished effect: animate both
transformandopacityin the same keyframes. - For a slide that should loop, add
infiniteto the animation shorthand and usealternateto reverse direction each cycle. - Test with browser developer tools' animation inspector (available in Chrome and Firefox) to slow down and debug timing.
Troubleshooting
If the element does not slide at all, first confirm the CSS file is linked correctly and the class name in HTML matches the selector. Check the browser console for syntax errors — a missing brace or semicolon can break the entire rule. If the element slides but then disappears, add forwards to the animation shorthand. If the motion is jerky, switch from left/margin to transform and avoid animating properties that trigger layout. If the slide is clipped, inspect parent elements for overflow: hidden and remove or adjust it. If the animation runs on page load but you wanted it on hover, remove the animation property and use a transition with a :hover rule instead. When testing on mobile, remember that hover does not exist on touch screens — use a tap or scroll trigger via JavaScript, or design the slide to run automatically.
Frequently Asked Questions
Can I slide an element without keyframes?
Yes. Use a transition on transform and change the transform value with a class or pseudo-class like :hover. The browser interpolates between the two states.
Why does my slide animation restart every time I scroll?
CSS animations run once by default unless you set animation-iteration-count: infinite. If it appears to restart, the element is likely being re-rendered or the class is being toggled by JavaScript. Check for scripts that add or remove classes on scroll.
How do I make a slide animation responsive?
Use relative units like percentages or vw in your transforms, or wrap the distance in calc() with a min() or max() function. Test at several viewport widths.
Does slide animation work in all browsers?
CSS transforms and keyframe animations are supported in all modern browsers. Very old versions of Internet Explorer need vendor prefixes, but those are rarely a concern today.
Can I pause a slide animation?
Yes. Set animation-play-state: paused; on the element, then toggle it to running with a class change. This is often used with JavaScript on hover or click.
What is the difference between transition and animation for sliding?
transition is simpler and reacts to state changes (hover, class toggle). animation with keyframes gives you multi-step control, looping, and automatic playback on load. Use transition for simple hover slides, animation for sequenced or looping motion.