Sub topic 4
Which properties can be animated, and how values interpolate
This chapter assumes you can write a basic transition and know the transition shorthand. You should also know what a computed value is.
What "animatable" means
A browser animates a property by calculating values between a start value and an end value. This calculation is called interpolation. On every frame, the timing function turns elapsed time into a progress number. The browser then blends the two values by that amount.
A property is animatable only if its type defines how to blend two values. The CSS specifications list this for each property under "Animation type".
Common interpolation types
| Type | Example properties | How it blends |
|---|---|---|
| Number | `opacity`, `flex-grow` | Straight numeric blend |
| Length and percentage | `width`, `margin`, `left` | Blends as a length. Mixed units use `calc()` |
| Color | `color`, `background-color` | Blends each channel, in sRGB by default |
| Transform list | `transform` | Blends function by function |
| Shadow and filter lists | `box-shadow`, `filter` | Blends item by item |
| Discrete | `display`, `visibility` | Flips from one value to the other, with no in-between values |
Lengths with different units still work. Going from 100px to 50% is blended through an internal calc(), so the result is smooth.
Transform lists must line up
When both transform values use the same functions in the same order, each function is blended on its own. If the lists differ, the browser falls back to blending the full matrices. That fallback can produce an unexpected path.
.card {
transform: translateX(0) rotate(0deg);
transition: transform 400ms ease-out;
}
.card:hover {
/* Same functions, same order: each one blends separately */
transform: translateX(100px) rotate(45deg);
}Writing rotate(45deg) translateX(100px) as the end value would move the element along a different path. Keep the order the same in both states.
Values that do not animate smoothly
autokeywords. A change fromheight: 0toheight: autohas no numeric midpoint, so it jumps. Common workarounds are animatinggrid-template-rowsfrom0frto1fr, or using a numericmax-height. Newer browsers also offer opt-in keyword interpolation withinterpolate-size.- Discrete properties.
displayandvisibilityflip at a set point. A later chapter covers how to include them in transitions. - Custom properties. A plain
--angleis only a string to the browser, so it changes with no blending. Registering it with@propertygives it a type and makes it animatable.
@property --angle {
syntax: "<angle>";
inherits: false;
initial-value: 0deg;
}
.dial {
--angle: 0deg;
transform: rotate(var(--angle));
transition: --angle 400ms ease-out;
}
.dial:hover {
--angle: 90deg;
}Choosing properties on purpose
Being animatable does not make a property a good choice. Animating width or top forces layout work on every frame. The performance chapter explains this cost and why transform and opacity are the usual targets.
Exercise
A panel goes from height: 0 to height: auto on hover with transition: height 300ms, but it snaps open. Why, and what is one fix?
Show answer
auto is a keyword with no numeric value, so the browser has nothing to blend between and the height jumps. One fix is to put the content in a grid and transition grid-template-rows from 0fr to 1fr. Another is to transition max-height between two numeric lengths.