TransitionsPremium

Smooth Fade

Two stacked skins of the same card hand over in a 0.7 s crossfade, while one's icon spins and shrinks and the other's comes to take its place: the moon seems to become a sun. The whole card is the button. CSS + 0.9 KB of JavaScript.

CSSJSFadeOpacity

Updated

This effect is part of Effect.Labs — 846 vanilla effects, some free, some premium. Transitions has 52 effects, including 9 free. Explore the category →

3 usage examples

Switching to red light — a sky map app for stargazing — Smooth Fade example 1

① Switching to red light — a sky map app for stargazing

WhenNext to her telescope, an observer taps her sky map's screen: the night-blue background and the white stars turn dark red in under a second, and the eye in the corner of the screen pivots into a red lamp. A second tap brings back the standard display.
WhyAfter twenty minutes in the dark, a screen that changes in one go is dazzling. The fade lets the pupil follow, and the pivoting icon confirms the mode without a line of text to read.
SettingsThe scene fills the whole screen (540 × 360 px, no rounding). Both layers carry the same SVG map, drawn with currentColor: only the layer's color changes, white on night blue or red on black. 44 px icon placed top right. In the capture, two real mouse clicks do the round trip.
Outlet on or off — a smart power strip's app — Smooth Fade example 2

② Outlet on or off — a smart power strip's app

WhenBefore printing, a remote worker taps the "Printer" tile of his power strip: it goes from slate gray to light yellow, the power symbol turns and gives way to a lightning bolt. At the end of the day he taps "Charger", which goes dark.
WhyFour identical tiles side by side: color says at a glance which ones draw power, and the 0.7 s fade suggests a device starting up rather than a box being ticked.
SettingsFour 120 × 124 px .sfd-scene elements in a grid; dark layer for "off", light layer for "on", icons replaced (power symbol and lightning bolt). Outlets already on start with sfd-on, aria-pressed="true" and the aria-hidden values swapped in the HTML. No scene script: two real clicks from the capture.
Scheduled sunset — a smart aquarium's light bar — Smooth Fade example 3

③ Scheduled sunset — a smart aquarium's light bar

WhenAt 9 pm, the time set for night, the app's "Lighting" card leaves its end-of-day gradient for a deep blue: the sun shrinks as it turns, the moon takes its place, and the text announces the bar at 2%.
WhyIn the tank, light goes down gradually; a card that flipped abruptly would say the opposite. The fade and the transforming icon tell the same story as the light bar.
Settings240 × 110 px card, layers laid out in a row (icon left, text right), gradient backgrounds. It starts on the light layer (sfd-on in the HTML). Nobody clicks: the app's clock calls click() on the scene, which the capture simulates; a tap is still possible to force the mode by hand.

How it works

The .sfd-scene (300 × 220 px) holds two .sfd-layer elements laid exactly on top of each other: .sfd-dark, visible at the start, and .sfd-light, at opacity 0. Each has its background, its text colors, its heading, its sentence and its icon. A click on the scene adds the .sfd-on class: the first layer goes from 1 to 0 opacity, the second from 0 to 1, over the same duration and the same curve (cubic-bezier(.4, 0, .2, 1)). The next click goes the other way.

What sets the effect apart from a plain fade is the icon. Both .sfd-icon elements sit in the same spot. The one in the leaving layer turns 120° while shrinking to 40% of its size; the one in the arriving layer starts at −120° and 40% and comes back upright and full size. Since one fades out while the other fades in, the eye reads a single icon transforming as it pivots.

The curve is not symmetrical: the two layers cross at 245 ms, not at half the duration (opacities of 0.67 and 0.33 at 200 ms, 0.15 and 0.85 at 400 ms, same values in the three engines tested). At that instant both are half transparent, so a quarter of whatever lies behind the card shows through it; on a dark background that goes unnoticed, on a light one the card pales for a short moment. The FAQ gives the rule to add to avoid it. The script itself only toggles the class and keeps the ARIA attributes up to date, for every .sfd-scene on the page.

Accessibility

  • Reduced motion: a fade is not a movement, so it is kept but brought down to 150 ms; the icon no longer turns or changes size. Read 60 ms after the click: layers at 0.41 and 0.59 opacity, toggle finished before 320 ms.
  • Keyboard: the scene carries role="button" and tabindex="0". Tab reaches it, including in WebKit with no special setting; Enter and Space toggle it, and pressing Space does not scroll the page (checked). 2 px yellow outline on keyboard focus.
  • Screen readers: aria-pressed follows the state and the masked layer gets aria-hidden="true". The button is therefore named by the visible layer's text. That text is read in full: keep a short heading and a brief sentence.
  • No interactive element inside the card: a button must not contain a link or a field. For about 0.3 s the two layers' texts overlap and cannot be read; put nothing there that must be read at that moment.

Browser compatibility

CSS (transition on opacity and transform, inset, flexbox with gap) + 0.9 KB of JavaScript. Nothing but 2D: crossing at 245 ms, state held, return, keyboard and reduced motion give the same readings in Chromium 149, Firefox 151 and WebKit 26.5. The minimum versions come from inset: Chrome 87, Firefox 66, Safari 14.1; flexbox gap, which spaces the icon, the heading and the sentence, is covered by the same versions.

Chrome 87+✓ Full (measured in Chromium 149)
Firefox 66+✓ Full (measured in Firefox 151)
Safari 14.1+✓ Full (measured in WebKit 26.5)
Edge 87+✓ Full (Chromium engine)
Mobile iOS 14.5+✓ Not checked on a device
Android Chrome 87+✓ Full (emulated touch)

Without JavaScript, the dark layer stays on screen, text and icon included. Without transition support, the change is immediate: the card goes from one skin to the other in one go, with no content lost.

The code

HTML structure to paste into your page (the CSS + JS come with your purchase):

index.html — structure
<div class="sfd-stage">
  <div class="sfd-scene" role="button" tabindex="0" aria-pressed="false">
    <div class="sfd-layer sfd-dark">
      <div class="sfd-icon" aria-hidden="true"><svg viewBox="0 0 24 24">…</svg></div>
      <h3>…</h3>
      <p>…</p>
    </div>
    <div class="sfd-layer sfd-light" aria-hidden="true">
      <div class="sfd-icon" aria-hidden="true"><svg viewBox="0 0 24 24">…</svg></div>
      <h3>…</h3>
      <p>…</p>
    </div>
  </div>
</div>

One-time payment, no subscription — the code is yours forever

Customize

Options passed to the API or data-* attributes:

Option / propertyDefaultEffect
Duration (CSS — .7s in the .sfd-layer transition and in the .sfd-icon one) 0.7 s for both Keep the same value in both places, otherwise the icon finishes turning before or after the fade. 0.4 s for a small tile, 1.2 s for a mood change on a large screen.
Icon rotation and shrink (CSS — rotate(120deg) scale(.4) and rotate(-120deg) scale(.4)) 120° and 40% 90° and 60% give a subtle switch; 180° and 0 make the icon vanish in a spin. Keep opposite signs between the two rules so the movement looks continuous.
Layer skins (CSS — background on .sfd-dark and .sfd-light, colors of their h3 and p) indigo #1e1b4b and cream #fefce8 The class names constrain nothing: two status colors, two photos or two gradients will do, as long as each layer has an opaque background.
Scene size (CSS — .sfd-scene { width; height; border-radius }) 300 × 220 px, 16 px rounding, max-width: 100% Layers follow the scene (inset: 0). The sentence has a reserved height of two lines (min-height: 2.8em) so heading and icon do not move from one layer to the other: adapt it to your texts.
Starting state (HTML — sfd-on class on .sfd-scene) absent: dark layer shown To start on the light layer, also write aria-pressed="true", aria-hidden="true" on .sfd-dark and aria-hidden="false" on .sfd-light.

FAQ

Crossfade Transition is the bare crossfade: two states, two opacities, 0.8 s, nothing else moves. Smooth Fade adds the icon that pivots and shrinks while the next one arrives, text colors specific to each layer and a reserved sentence height so nothing jumps. If your two states have no icon, the former is enough and weighs slightly less; if the change must be read on a pictogram (moon and sun, open and closed padlock), take this one.
Because both layers are semi-transparent at the same time: at 245 ms each is at 50%, and a quarter of what is behind the card comes through. To prevent it, keep the bottom layer opaque and only fade the top one: add .sfd-scene.sfd-on .sfd-dark { opacity: 1 }. Checked on a white background: the card no longer pales, the light layer simply settles over the dark one.
Yes. .sfd-dark and .sfd-light are only names: the first is the layer shown at the start, the second the one that arrives. On and off, day and night, normal and alert work the same way, with the icons of your choice in .sfd-icon. However, the effect does not change your page's theme: it fades two contents inside a card, and it is up to you to hook a real theme change to the click.