CSS Cross Fade Animation: Replace Slide Transitions
In this article
To change an animation from slide to cross fade in CSS, replace the transform-based slide keyframes with opacity-based keyframes and set the incoming and outgoing elements to overlap using absolute positioning or a grid stack. The shortest working version is: remove transform: translateX(...) from your keyframes, animate opacity from 0 to 1 on the incoming slide and 1 to 0 on the outgoing slide, and place both slides in the same grid cell or absolutely positioned container so they occupy the same space during the transition.
What You'll Need
- A working slide animation in CSS (keyframes or transition-based) that you want to convert.
- Access to your stylesheet or the
<style>block in your HTML. - A browser with developer tools for testing (Chrome, Firefox, Safari, or Edge).
- Basic familiarity with CSS selectors and the
@keyframesrule.
Step-by-Step
- Locate the slide animation. Search your CSS for
@keyframesrules that usetransform: translateX,translate3d, orleft/rightoffsets. Also check fortransition: transformon slide elements. Note the animation name and the element it applies to. - Decide on the stacking method. A cross fade requires both slides to occupy the same visual space at the same time. The most reliable approach is a parent container with
display: gridand all slides placed ingrid-area: 1 / 1. Alternatively, set the parent toposition: relativeand each slide toposition: absolute; inset: 0. Grid stacking is simpler because it keeps the container height driven by the tallest slide. - Remove the slide movement. Delete
transform: translateX(...)(or equivalent) from the keyframes. If the slide usedleftormargin-leftoffsets, remove those too. The goal is that position no longer changes during the animation. - Write the cross fade keyframes. Create two keyframe sets: one for the incoming slide and one for the outgoing slide. For example:
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes fadeOut { from { opacity: 1; } to { opacity: 0; } }
Applyanimation: fadeIn 0.4s ease forwards;to the slide becoming visible andanimation: fadeOut 0.4s ease forwards;to the slide leaving. Theforwardsfill mode keeps the final opacity after the animation ends. - Control stacking order. During the cross fade, the incoming slide should sit above the outgoing slide so it becomes fully visible. Add
z-index: 1to the incoming slide andz-index: 0to the outgoing one, or rely on DOM order if the incoming slide comes later in the markup. Without this, a fading-out slide can briefly cover the new one. - Set pointer events correctly. A slide that has faded to
opacity: 0is still clickable unless you disable it. Addpointer-events: noneto hidden slides andpointer-events: autoto the active slide. This prevents invisible elements from blocking buttons or links. - Adjust timing and easing. Cross fades usually look best between 200ms and 500ms with
easeorease-in-out. If the outgoing and incoming animations run simultaneously, a linear fade can look flat; a slight overlap where both are partially visible is normal. Keep both durations equal unless you deliberately want a dip in opacity. - Test in the browser. Open the page, trigger the transition, and watch for flicker, a flash of background, or a slide that jumps position. Use the browser's animation inspector (in Chrome DevTools under the Elements panel, or Firefox's Animations panel) to slow down and inspect the fade.
- Remove leftover slide styles. Check for
overflow: hiddenon the parent that was needed for horizontal sliding but is no longer required, and fortransitionproperties ontransformthat can now be deleted. Leaving them in place rarely breaks the fade but makes future edits confusing.
Common Mistakes
- Fading without stacking. If the slides are still laid out side by side, animating opacity just makes one disappear while the other sits off-screen. The cross fade only works when both slides share the same space.
- Forgetting
forwards. Withoutanimation-fill-mode: forwards, the incoming slide snaps back toopacity: 0when the animation ends. - Animating
displayorvisibility. These properties are not smoothly animatable in most browsers. Use opacity for the fade and toggle visibility or pointer events separately if needed. - Leaving the outgoing slide clickable. A fully transparent slide still receives clicks unless
pointer-events: noneis set. - Unequal durations. If the fade-out is much faster than the fade-in, the container shows a blank background in between, which reads as a flicker rather than a cross fade.
Tips
- Use
will-change: opacitysparingly on slides that animate frequently; it can help the browser promote the layer, but applying it to many elements wastes memory. - For accessibility, respect
prefers-reduced-motionby wrapping the animation in a media query that setsanimation: noneor a very short duration. - If you use JavaScript to switch slides, add the incoming class first, then remove the outgoing class after the animation duration so both slides exist during the fade.
- Test with a background color on the container. A cross fade between two transparent slides can look like nothing happened.
Troubleshooting
If the fade does not appear at all, confirm the animation name in animation matches the @keyframes name exactly and that the element is not set to display: none at the moment the animation should run. If you see a flash of the page background, the outgoing slide is finishing before the incoming slide starts; equalize the durations or add a small overlap. If the incoming slide appears but is not clickable, check pointer-events and z-index. If the layout collapses when slides are absolutely positioned, give the parent a fixed height or switch to grid stacking so the container height follows the content. If the animation stutters on mobile, reduce the number of simultaneously animated properties and avoid animating box-shadow or filter alongside opacity.
Frequently Asked Questions
Can I cross fade without JavaScript?
Yes, for simple cases such as hover states or CSS-only carousels using :target or radio inputs. For timed slideshows that advance automatically, you generally need JavaScript or CSS animations with delays.
Does opacity animation perform well?
Opacity is one of the cheapest properties to animate because it can be handled by the compositor. It performs better than animating left, width, or transform in layouts that trigger reflow.
Why does my cross fade show a white flash?
The most common cause is a gap between the outgoing and incoming animations, or a background color on the container that becomes visible when both slides are partially transparent. Set a background on the container that matches the design, and keep the two animation durations equal.
Can I use transition instead of @keyframes?
Yes. If you toggle a class that changes opacity from 0 to 1, a transition: opacity 0.4s ease produces the same visual result. Keyframes are more useful when you need multiple steps or automatic playback.
Does cross fade work in all browsers?
Opacity animation and transitions are supported in all current major browsers. Very old browsers may need vendor prefixes, but modern versions of Chrome, Firefox, Safari, and Edge do not.
How do I cross fade between more than two slides?
Stack all slides in the same grid cell, set every slide to opacity: 0 and pointer-events: none, then apply an active class that sets opacity: 1 and pointer-events: auto. Transition the opacity property on all slides so switching the active class produces the fade.