Colour · gradient recipeAnimated CSS gradient — a smooth colour flow

The background is three times wider than the block and slowly travels back and forth. For people who turned animation off in their system the flow stops — a mandatory part of the recipe.

TextButtonBorderAnimation← Back to the builder
01Example02Code03Steps04Mistakes05Questions06Recipes07Related
01

Live example

change the colours — the code below follows
Recipe settings
Colours
#6C8CFF0%
#B18CFF100%
Open in the builder →
02

Ready code

prefers-reduced-motion is mandatory
.gradient-animated {
  background: #6c8cff; /* fallback */
  background: linear-gradient(90deg, #6c8cff, #b18cff, #6c8cff);
  background-size: 300% 100%;
  animation: gradient-shift 8s ease-in-out infinite;
}

@keyframes gradient-shift {
  0%, 100% { background-position: 0% 50%; }
  50%      { background-position: 100% 50%; }
}

@media (prefers-reduced-motion: reduce) {
  .gradient-animated { animation: none; }
}
03

Step by step

  1. 01
    background-size: 300% 100%
    Stretch the gradient wider than the block — only a third is in view.
  2. 02
    @keyframes gradient-shift
    Move background-position from 0% to 100% and back.
  3. 03
    animation: … 8s ease-in-out infinite
    A slow 6–12 second cycle looks calm and does not distract.
  4. 04
    @media (prefers-reduced-motion: reduce)
    Turn the animation off for those whom motion bothers or harms.
04

Common mistakes

No prefers-reduced-motion

An endless animation with no way to stop it fails WCAG 2.3.3 and can cause discomfort.

A jump at the end of the cycle

If the first and last colours differ, the seam shows a jump. Repeat the first colour at the end or use alternate.

Too fast

A cycle shorter than 4 seconds reads as flashing. For a background, 8 seconds or more is better.
05

Frequently asked

Stretch the background wider than the block with background-size: 300% 100% and describe in @keyframes how background-position moves from 0% to 100% and back. The animation property with infinite and ease-in-out repeats the cycle non-stop. To avoid a jump at the seam, the first colour of the gradient is repeated at the end — the recipe code does it for you. The duration is set with a slider, eight seconds by default.
06

Other recipes

Updated

«» added to favorites