CSS Animation Example: Shake Error
The "Shake Error" is a universal interaction pattern used to indicate a failed form submission or invalid input, mimicking a person shaking their head "no."
This animation utilizes a highly specific @keyframes definition that rapidly changes the transform: translate3d() property back and forth across the X-axis. Since it's a short, fast animation, using a custom cubic-bezier timing function ensures it feels sharp and snappy rather than smooth.
Here is the code to create the Shake Error animation.
Accessibility Considerations
Rapid shaking can be particularly difficult for users with vestibular issues. For these users, an error state should still be visibly communicated without motion. We can use the prefers-reduced-motion media query to detect if the user has requested that their system minimize non-essential motion.
Here we halt the keyframes entirely and instead apply a static red background to the input to indicate an error.
@media (prefers-reduced-motion: reduce) {
.shake-input.error {
animation: none;
/* Provide an alternative static visual cue */
background-color: #fff3f3;
}
}