Sub topic 4
Which properties can be animated, and how values interpolate
This chapter assumes you can write a basic transition, as covered earlier in this tutorial, and that you know the cascade, computed values and common properties such as color, opacity and width. Here you will learn which properties a browser can animate, how it computes the in-between values, and why some effects silently do nothing.
What "animatable" means
A browser animates a property by interpolation. It takes a start value and an end value, then computes intermediate values for each frame. Interpolation needs a rule for what "halfway between" means. Each CSS property has an animation type that defines that rule.
The animation type is listed in each property's specification, and MDN shows it as "Animation type" in the property's formal definition. The main types are:
| Value type | How it interpolates | Example properties |
|---|---|---|
| Number | Linear between the two numbers | `opacity`, `flex-grow`, `line-height` (unitless) |
| Integer | Interpolated as a number, then rounded | `z-index`, `order` |
| Length | Linear between computed lengths | `width`, `margin`, `font-size`, `border-width` |
| Length-percentage | Linear, using `calc()` to mix units | `width`, `top`, `padding` |
| Color | Per channel, with alpha premultiplied | `color`, `background-color`, `border-color` |
| Transform list | Function by function, or via matrices | `transform` |
| Shadow or filter list | Item by item | `box-shadow`, `filter` |
| Discrete | Flips from start to end value at 50% | `display`, `visibility`, `position` |
Interpolation works on computed values, not on the text you wrote. font-size: 1.5em is computed to pixels before interpolation. currentcolor is resolved to a color. Percentages remain percentages for properties that accept them, so the browser can resolve them against the box at each frame.
Numbers, lengths and mixed units
Number and length interpolation is the simple case. The progress value p runs from 0 to 1, and each frame gets start + (end - start) * p. The easing function shapes p over time, so a 50% time position is not always a 50% value position.
Mixing units works for length-percentage properties. The browser builds a calc() expression that blends both terms:
.drawer {
width: 100px;
transition: width 300ms ease-out;
}
.drawer.is-wide {
width: 50%;
}During the transition, width is effectively calc(100px * (1 - p) + 50% * p). You do not write this yourself.
Keywords are the main trap
A keyword that has no numeric value cannot be interpolated. The most common case is auto:
/* No animation: the width jumps. */
.panel { height: 0; transition: height 300ms; }
.panel.is-open { height: auto; }The browser has no number for auto, so it applies the end value at once. The same applies to line-height: normal, width: min-content and similar sizing keywords. You have three options:
- Animate to a known size, such as
max-height: 20rem. The timing looks off if the real content is much shorter than the limit. - Animate a grid track from
0frto1fron a grid container, with the content in a child that hasoverflow: hiddenandmin-height: 0. - Opt in to keyword interpolation with
interpolate-size: allow-keywords, in browsers that support it.
:root {
interpolate-size: allow-keywords;
}
.panel {
height: 0;
overflow: hidden;
transition: height 300ms ease-out;
}
.panel.is-open {
height: auto;
}The third option is a progressive enhancement. Browsers that lack the property just jump to the end value.
Colors
Colors interpolate per channel in a color space, and the default is sRGB. Transparency is handled with premultiplied alpha, which means each channel is weighted by alpha before blending. This keeps a fade from transparent to a solid color from passing through a muddy gray:
.link {
color: #1d4ed8;
background-color: transparent;
transition: color 200ms, background-color 200ms;
}
.link:hover {
color: #ffffff;
background-color: #1d4ed8;
}Because blending happens in sRGB, the midpoint between two saturated colors can look darker or duller than either end. If this matters for a gradient or a color-mix effect, you can pick another interpolation space, for example linear-gradient(in oklch, red, blue) or color-mix(in oklch, red, blue). These choices apply to gradients and color-mix(). A plain transition on a color property uses the browser's default.
Transforms
transform takes a list of functions. Interpolation depends on whether the two lists are compatible.
- Matching lists are interpolated function by function.
translateX(0) rotate(0deg)totranslateX(100px) rotate(180deg)moves and rotates in step. A rotation of360deggives a full turn. - Lists of different length are padded with identity functions, as long as the shared prefix has matching function types.
- Mismatched function types make the browser convert both lists to matrices and interpolate those. The result is valid, but the path can look odd, because a matrix cannot represent a rotation of more than 180 degrees.
.icon {
transform: translateX(0) rotate(0deg);
transition: transform 400ms ease-in-out;
}
.icon:hover {
/* Same function types in the same order: smooth, predictable motion. */
transform: translateX(24px) rotate(270deg);
}Writing the start state explicitly, even when it is the identity, is the reliable habit. If .icon had no transform at all, the browser would still manage the transition. With more complicated lists, an explicit start state keeps the function order identical.
The individual properties translate, rotate and scale interpolate on their own and are applied in a fixed order. They are useful when you want to animate one part of a transform without rewriting the entire list.
Lists: shadows and filters
box-shadow is a comma-separated list. Two lists interpolate item by item. If one list is shorter, the browser pads it with transparent, zero-size shadows. Two shadows interpolate only if both are inset or both are not. Mixing the two makes the whole property discrete.
filter works like transform. Matching functions in the same order interpolate smoothly, for example blur(0) to blur(4px). A mismatch means the value flips at the midpoint.
.card {
box-shadow: 0 1px 2px rgb(0 0 0 / 0.2);
filter: blur(0);
transition: box-shadow 250ms, filter 250ms;
}
.card:hover {
box-shadow: 0 8px 24px rgb(0 0 0 / 0.3);
filter: blur(1px);
}Discrete properties
A property with a discrete animation type has no in-between values. It holds the start value for the first half of the progress and the end value for the second half, so the switch happens at the 50% point. Easing moves that moment in time. A few properties have their own rules:
visibilityisvisiblefor the whole transition if either end isvisible. This lets a fade-out finish before the element is hidden.displayandcontent-visibilitywere not transitionable at all in older browsers. Newer ones support them throughtransition-behavior: allow-discrete, which a later chapter covers together with@starting-style.position,floatand most keyword-only properties are discrete.
Custom properties and gradients
Gradients in background-image do not interpolate. The browser sees two images of an unsupported type and flips between them. Custom properties have the same limit by default, because the browser treats --angle: 90deg as an unparsed token stream.
The @property rule fixes this by registering a custom property with a syntax. The browser can then interpolate the registered type:
@property --angle {
syntax: "<angle>";
inherits: false;
initial-value: 0deg;
}
.panel {
width: 240px;
height: 120px;
background: linear-gradient(var(--angle), #2563eb, #9333ea);
transition: --angle 600ms ease;
}
.panel:hover {
--angle: 180deg;
}The gradient itself is not interpolated. Instead, the angle is, and the browser rebuilds the gradient on every frame. Because that forces paint work each frame, use this for small elements.
How the browser decides
Common errors
- Animating
height: auto. Use a numeric end value, a grid track, orinterpolate-size. - Expecting a gradient to fade. Animate
opacityon a pseudo-element that holds the second gradient, or register a custom property. - Starting from an unset value. If an element has no declared
width, the computed start is its used size, but the browser may not run the transition. Declare both ends. - Mismatched transform lists. Keep the same functions in the same order.
- Assuming
transition: allis safe. It transitions every animatable property, including ones you did not mean to change, such aspaddingduring a layout switch.
Exercise
A collapsible section uses height: 0 for the closed state and height: auto for the open state, with transition: height 300ms. It snaps open with no animation. Why does this happen, and name two ways to fix it.
Show answer
auto is a keyword with no numeric value, so the browser cannot interpolate between 0 and auto and applies the end value at once. Fixes: animate max-height to a fixed limit, animate a grid row from 0fr to 1fr, or set interpolate-size: allow-keywords where it is supported.
Exercise
You transition transform from translateX(0) to rotate(90deg) translateX(50px). Why can the motion path look strange, and how do you avoid it?
Show answer
The function lists do not match in type and order, so the browser converts both to matrices and interpolates those, which gives a path that is not a simple slide plus rotation. Write both states with the same functions in the same order, for example translateX(0) rotate(0deg) to translateX(50px) rotate(90deg).