CSS Tricks - Pure CSS Animated Gradient Background and More
Let's take a look at how to create a animated background with no Javascript and just plain CSS.
Tip: Keep your code simple & manageable, with super fast page loads leveraging Pure CSS animations.
The best code is no code at all.
- Jeff Atwood(Founder of StackOverflow)
Live Example on CodePen
Note: Many of these were forked and modified from existing codepens.
Animated Gradient Background
Now let's check out a live example on CodePen!
Note: CodePen is an amazing free resource for developers! Check out CodePen Challenges to learn more!
▶️ 🌈 Pure CSS Animated Gradient Background on CodePen
You can build super slick features and animations for your site + blog with just Astro hosted on Cloudflare.
The CSS @keyframes rule is the secret sauce for these slick animations that don't require a single line of JS!
Check out the CSSKeyframesRule docs for more info.
Tip: Keyframes control intermediate steps at explicitly set breakpoints in the animation sequence. These steps are also called waypoints and give more control then transitions.
Learn more about declaring Keyframes here.
Keyframes Example:
@keyframes gradient {
0% {
background-position: 0% 50%;
}
50% {
background-position: 100% 50%;
}
100% {
background-position: 0% 50%;
}
}
In this example we can see keyframes at 0%, 50% and 100% with the background-position property used to define the background image position
style for each keyframe.
As you can see, you define specific CSS styles that change over time as the animation progresses.
You can change the angle, position, color or any number of properties over the course of
an animation's duration.
Clever use of CSS functions likelinear-gradient used with ::before or ::after pseudo-elements with properties like box-shadow can create really
complex and visually dramatic effects!
This gives you an incredible amount of power and versatility in creating novel animations and interactivity that can really make your UIs that much more intuitive and just plain fun to use.
When a UI just clicks for users, it really is a joy!
Programming isn't about what you know; it's about what you can figure out.
- Chris Pine
It is just so intuitive, fun and powerful to create cool new effects with just plain old CSS.
Note: Not that long ago it was a pain just to get stuff centered.
Neon Button Animations
Satisfying Unique Button Hover Animations
You have so much creative freedom to create all different types of animations and behaviors that don't just look cool but also contribute to a intuitive UX!
In this example, for the first button, we use the :hover pseudo class along with the box-shadow property to create a simple glow mouse-over hover.
The more complex examples, leverage :before, :after, :hover:before and :hover:after pseudo elements
to create more unique effects using properties such astransition, box-shadow and background-color.
Live CodePen Example
▶️ Neon Button Animation on CodePen
Box Shadow On-Hover Example:
.btn-1 {
border: none;
background-color: #ff9aff;
box-shadow: 0 0 5px #ef97e8;
}
.btn-1:hover {
box-shadow:
0 0 10px #ef97e8,
0 0 20px #ef97e8,
0 0 20px #fff inset;
}
The opportunities for creativity are endless!
Much More Fun then a Plain Old Spinner
Eye-Catching Loading Animations with Pure CSS
You can get super creative with really expressive loading animations.
▶️ Neon Grid Loaders on CodePen
Pure CSS Simulated IDE Coding
This is probably the coolest thing I've seen though. Simulating an actual IDE with syntax highlighting with just CSS.
▶️ Editor Illustration on CodePen
Think twice before spinning up a React app next time you have a static site to build.
I've been a long-time fan of Gatsby but this super lean stack has really won me over :)
Clean code is code that is easy to read, easy to understand, and easy to modify.
- Robert C. Martin(Uncle Bob)
Macbook Stickers as Dynamic Interactive Elements
At first, glance, it just looks like a img of a Macbook.
But hover over a sticker and you'll see each one is actually a interactive element!
▶️ Personal Concept on CodePen
Pure CSS Text Effects: A Deep Dive
Backgrounds and buttons are only half the story. Type itself can be animated with nothing but CSS,
and Colorion's CSS Text Effects library is a great showcase:
123 copy-and-paste effects, each one a single class and a handful of declarations.
Let's break down a set of them line by line. Once you see the handful of building blocks they all
share, you can mix and match your own.
Note: All snippets below come from text-effects.colorion.co, lightly
trimmed. Every effect shares one set of colour tokens, so re-skinning is a one-line change::root { --ink: currentColor; /* main text colour */ --ink-2: #ff4fd8; /* primary accent */ --ink-3: #4ff8ff; /* secondary accent */ }
The Building Blocks
Almost every text effect is a combination of five techniques:
-
background-clip: text: paint a gradient (or any background) inside the glyphs, then make the text itselfcolor: transparent. -
Stacked
text-shadow: a comma-separated list of shadows builds glows, extrusions and long shadows. -
Pseudo-element copies:
content: attr(data-text)on::before/::afterduplicates the word so each layer can be coloured, clipped and moved on its own. -
Per-letter custom properties: wrap each letter in an element with
style="--i: n"and usecalc(var(--i) * …)to stagger the animation. -
@keyframes+ timing functions:steps()for mechanical, jumpy motion;ease-in-outfor organic, floaty motion.
1. Aurora Gradient Text (background-clip: text)
<div class="fx-aurora">BUILD SOMETHING</div>
.fx-aurora {
background: linear-gradient(115deg, var(--ink-2), var(--ink-3), var(--ink-2), var(--ink-3)) 0 0 /
300% 100%;
-webkit-background-clip: text;
background-clip: text;
color: transparent;
animation: fx-aurora 7s linear infinite;
}
@keyframes fx-aurora {
0% {
background-position: 0% 50%;
filter: hue-rotate(0deg);
}
100% {
background-position: 300% 50%;
filter: hue-rotate(360deg);
}
}
Breakdown:
- The
backgroundshorthand sets a four-stop gradient and, after the/, abackground-sizeof300% 100%. The gradient is three times wider than the text, so only a slice is visible at once. -
background-clip: text(plus the-webkit-prefix that Safari and older Chromium still want) clips that background to the glyph shapes.color: transparentlets it show through. - The keyframes slide
background-positionfrom0%to300%. That is exactly the same trick as the animated background at the top of this post, only applied to text. -
filter: hue-rotate()spins the whole colour wheel on top, so the palette keeps shifting instead of just scrolling. The gradient repeats its stops (2, 3, 2, 3), which keeps the loop seamless.
2. Glitch Text (pseudo-element copies + clip-path)
<div class="fx-glitch" data-text="GLITCH">GLITCH</div>
.fx-glitch {
position: relative;
color: var(--ink);
}
.fx-glitch::before,
.fx-glitch::after {
content: attr(data-text);
position: absolute;
inset: 0;
}
.fx-glitch::before {
color: var(--ink-2);
clip-path: inset(0 0 55% 0);
animation: fx-glitch-a 2.6s steps(1) infinite;
}
.fx-glitch::after {
color: var(--ink-3);
clip-path: inset(45% 0 0 0);
animation: fx-glitch-b 2.6s steps(1) infinite;
}
@keyframes fx-glitch-a {
0%,
12%,
20%,
55%,
100% {
transform: translate(0);
opacity: 0;
}
13% {
transform: translate(-5px, -2px);
opacity: 0.9;
}
16% {
transform: translate(4px, 1px);
opacity: 0.9;
}
48% {
transform: translate(-3px, 2px);
opacity: 0.9;
}
51% {
transform: translate(2px, -1px);
opacity: 0.9;
}
}
/* fx-glitch-b mirrors this with slightly offset timings and opposite directions */
Breakdown:
-
content: attr(data-text)reads thedata-textattribute so the pseudo-elements render a copy of the word with no duplicated markup.position: absolute; inset: 0stacks each copy exactly on top of the original. -
clip-path: inset(0 0 55% 0)keeps only the top 45% of the::beforecopy;inset(45% 0 0 0)keeps only the bottom 55% of::after. Together they form two "slices" in different accent colours, which is the RGB channel-split look. -
steps(1)is the key timing function here. It disables interpolation, so each keyframe snaps into place rather than gliding. That's what makes it read as digital corruption instead of a wobble. - Most of the timeline is
opacity: 0. The glitch only fires in short bursts (13–16% and 48–51%), and the two layers use offset timings so they never quite line up. Short, irregular bursts look far more convincing than a constant jitter.
3. Typewriter (steps() + ch units)
<div class="fx-typewriter">TYPEWRITER</div>
.fx-typewriter {
box-sizing: content-box;
font:
600 24px/1.2 "JetBrains Mono",
monospace;
white-space: nowrap;
overflow: hidden;
width: 10ch;
border-right: 0.55ch solid var(--ink-2);
animation:
fx-type 3.6s steps(10) infinite,
fx-caret 0.65s steps(1) infinite;
}
@keyframes fx-type {
0%,
8% {
width: 0;
}
55%,
100% {
width: 10ch;
}
}
@keyframes fx-caret {
50% {
border-color: transparent;
}
}
Breakdown:
- The
chunit is the width of the0glyph. In a monospace font every character is that wide, so10chis exactly ten letters. -
overflow: hidden+white-space: nowrapturnwidthinto a reveal mask. Animating it from0to10chuncovers the word. -
steps(10)splits that animation into ten discrete jumps, one per character, so letters appear whole rather than sliding in pixel by pixel. The step count must match the character count. - The caret is just
border-right. A second animation withsteps(1)blinks it by togglingborder-colortotransparentat 50%. Two animations, comma-separated, on one element. -
box-sizing: content-boxkeeps the border outside the10chwidth so the caret doesn't eat into the last letter.
4. Neon Sign Flicker (stacked text-shadow)
.fx-neon {
color: var(--ink);
animation: fx-neon 3.2s linear infinite;
}
@keyframes fx-neon {
0%,
18%,
21%,
24%,
100% {
opacity: 1;
text-shadow:
0 0 4px var(--ink),
0 0 11px var(--ink-2),
0 0 24px var(--ink-2),
0 0 48px var(--ink-2);
}
19%,
22%,
62% {
opacity: 0.35;
text-shadow: none;
}
63%,
64.5% {
opacity: 0.6;
text-shadow: 0 0 4px var(--ink-2);
}
}
Breakdown:
- The glow is four shadows with zero offset and growing blur radii (
4px → 48px). A tight white-ish core plus wider coloured halos imitates how a real gas tube bleeds light. This is the same layeredbox-shadowtrick used in the neon buttons above, applied to text. - The flicker comes from keyframe placement, not easing.
18% → 19% → 21% → 22% → 24%packs two quick off/on blinks into a tiny window, then the tube stays lit until a weaker stutter around 62–64.5%. - Irregular, clustered keyframes are what sell "faulty hardware." Evenly spaced keyframes would just look like a pulse.
5. Liquid Fill (gradient hard stops + -webkit-text-stroke)
.fx-liquid {
color: transparent;
-webkit-text-stroke: 1px color-mix(in srgb, var(--ink-3) 65%, transparent);
background: linear-gradient(
0deg,
var(--ink-3) 0 46%,
color-mix(in srgb, var(--ink-3) 55%, transparent) 48%,
transparent 52% 100%
)
0 0 / 100% 220%;
-webkit-background-clip: text;
background-clip: text;
animation: fx-liquid 3.4s ease-in-out infinite;
}
@keyframes fx-liquid {
0%,
100% {
background-position: 0 -110%;
}
50% {
background-position: 0 10%;
}
}
Breakdown:
-
-webkit-text-strokedraws an outline around the transparent glyphs, so the empty "glass" letters are visible before they fill. - The gradient runs bottom-to-top (
0deg) with near-hard stops: solid colour up to 46%, a semi-transparent band at 48% (the meniscus), then nothing from 52%. It's a liquid level, not a smooth fade. -
background-size: 100% 220%makes the gradient over twice the text height. Animatingbackground-position-yraises and lowers the "water line" through the letters. -
color-mix(in srgb, var(--ink-3) 55%, transparent)derives translucent variants from one token instead of hard-coding extra colours. -
ease-in-outgives the slosh a natural slow-down at the top and bottom.
6. Wave Text (per-letter --i + negative delays)
<div class="fx-wave" role="img" aria-label="WAVE">
<b aria-hidden="true" style="--i:0">W</b>
<b aria-hidden="true" style="--i:1">A</b>
<b aria-hidden="true" style="--i:2">V</b>
<b aria-hidden="true" style="--i:3">E</b>
</div>
.fx-wave b {
font-weight: inherit;
display: inline-block;
animation: fx-wave 1.3s ease-in-out infinite;
animation-delay: calc(var(--i) * -0.14s);
}
@keyframes fx-wave {
0%,
100% {
transform: translateY(7px);
}
50% {
transform: translateY(-7px);
}
}
Breakdown:
- Each letter gets its own element and an index custom property (
--i). One keyframe animation is shared; only the delay differs. -
calc(var(--i) * -0.14s)uses a negative delay. Negative delays start the animation partway through its cycle immediately, so the wave is already in motion on page load instead of letters waking up one by one. -
display: inline-blockis required becausetransformdoesn't apply to plain inline elements. - Accessibility: splitting a word into letters makes screen readers spell it out. The wrapper's
role="img"+aria-labelexposes the whole word once, andaria-hiddenon each letter hides the fragments.
7. 3D Extruded Text (stepped text-shadow)
.fx-extrude {
color: var(--ink);
text-shadow:
1px 1px 0 color-mix(in srgb, var(--ink-2) 80%, #000),
2px 2px 0 color-mix(in srgb, var(--ink-2) 70%, #000),
3px 3px 0 color-mix(in srgb, var(--ink-2) 60%, #000),
4px 4px 0 color-mix(in srgb, var(--ink-2) 50%, #000),
5px 5px 0 color-mix(in srgb, var(--ink-2) 40%, #000),
6px 6px 0 color-mix(in srgb, var(--ink-2) 30%, #000),
7px 7px 12px rgba(0, 0, 0, 0.5);
animation: fx-extrude 3s ease-in-out infinite;
}
@keyframes fx-extrude {
0%,
100% {
transform: rotate(-3.5deg) translateY(2px);
}
50% {
transform: rotate(3.5deg) translateY(-4px);
}
}
Breakdown:
- Six zero-blur shadows, each offset one more pixel diagonally, stack into a solid "side" of the letter. That's the extrusion.
- Each layer mixes in progressively more black (
80% → 30%accent), so the depth darkens as it recedes, a cheap but convincing lighting cue. - The final shadow does have blur (
12px) and sits under everything as a soft drop shadow, grounding the block on the page. - A gentle
rotate+translateYrock makes it feel like a physical object.
8. Long Shadow "Sundial" (animating text-shadow offsets)
@keyframes fx-sundial {
0%,
100% {
text-shadow:
4px 0 var(--ink-2),
8px 0 color-mix(in srgb, var(--ink-2) 55%, transparent),
12px 0 color-mix(in srgb, var(--ink-2) 25%, transparent);
}
25% {
text-shadow:
0 4px var(--ink-2),
0 8px color-mix(in srgb, var(--ink-2) 55%, transparent),
0 12px color-mix(in srgb, var(--ink-2) 25%, transparent);
}
/* …50% (left), 75% (up), plus diagonal stops at every 12.5% */
}
Breakdown:
- Three shadows at
4px,8pxand12pxwith fading opacity create a short trail. - Keyframes every 12.5% move the offsets around a circle: right, down-right, down, down-left and so
on. The diagonals use
2.8px(≈4 × cos 45°) so the shadow stays the same length at every angle. - The browser interpolates between the shadow lists because each keyframe has the same number of shadows. Mismatched counts would snap instead of animating.
9. Animated SVG Outline (stroke-dasharray / stroke-dashoffset)
<svg class="fx-contour" viewBox="0 0 260 56" role="img" aria-label="OUTLINE">
<text x="50%" y="50%" dominant-baseline="central" text-anchor="middle">OUTLINE</text>
</svg>
.fx-contour text {
fill: color-mix(in srgb, var(--ink) 10%, transparent);
stroke: var(--ink-3);
stroke-width: 1.2;
stroke-dasharray: 34 66;
animation: fx-contour 3.2s linear infinite;
}
@keyframes fx-contour {
to {
stroke-dashoffset: -100;
}
}
Breakdown:
- SVG
<text>can be styled with CSSfillandstroke, which unlocks the dash properties regular HTML text doesn't have. -
stroke-dasharray: 34 66draws a 34-unit dash then a 66-unit gap: a 100-unit repeating pattern. - Animating
stroke-dashoffsetby exactly one pattern length (-100) withlineartiming makes the dashes crawl around the letterforms in a perfectly seamless loop. -
x="50%",text-anchor="middle"anddominant-baseline="central"centre the text in theviewBoxon both axes.
10. Highlighter Marker (animating background-size)
.fx-marker {
padding: 0.1em 0.2em;
background: linear-gradient(
100deg,
color-mix(in srgb, var(--ink-2) 85%, transparent),
color-mix(in srgb, var(--ink-2) 60%, transparent)
)
no-repeat 0 62%;
background-size: 0% 46%;
animation: fx-marker 3s ease-in-out infinite;
}
@keyframes fx-marker {
0% {
background-size: 0% 46%;
background-position: 0 62%;
}
38%,
62% {
background-size: 100% 46%;
background-position: 0 62%;
}
100% {
background-size: 0% 46%;
background-position: 100% 62%;
}
}
Breakdown:
- A
no-repeatgradient sized46%tall and positioned62%down sits behind the lower half of the text, just like a real highlighter stroke. - Growing
background-sizewidth from0%to100%"paints" it left to right. - The trick on the way out: the position jumps from
0to100%while the size shrinks, so the stroke retracts toward the right and appears to lift off the page instead of rewinding. - The
100deggradient with two opacities fakes uneven ink density.
11. Water Reflection (-webkit-box-reflect)
.fx-mirror {
-webkit-box-reflect: below 2px linear-gradient(transparent 45%, rgba(255, 255, 255, 0.28));
animation: fx-mirror 3s ease-in-out infinite;
}
@keyframes fx-mirror {
0%,
100% {
transform: translateY(0);
}
50% {
transform: translateY(-6px);
}
}
Breakdown:
-
-webkit-box-reflectrenders a mirrored copy of the elementbelowit,2pxaway. The optional gradient is a mask for the reflection, fading it out the further it gets from the text. - It's non-standard (Chromium and Safari only), but it degrades gracefully: Firefox simply shows the text without a reflection. That's a perfectly fine progressive enhancement.
- The float animation moves the element and its reflection together, so the word bobs over its own image.
Tip: Notice the pattern: most of these effects animate cheap properties like
transform,opacityand
background-position.transformandopacityrun on the compositor and stay smooth even on
low-end phones. Animatingtext-shadow,widthorfiltertriggers repaints, so use those
sparingly and on small amounts of text.
Respect Reduced Motion
Fun animations shouldn't make anyone feel ill. Colorion ships a global guard, and you should too:
@media (prefers-reduced-motion: reduce) {
* {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
}
}
Breakdown:
- Users who enable "Reduce motion" in their OS settings match this media query.
- Instead of
animation: none(which can leave elements stuck in their hidden0%state, like an empty typewriter), it makes every animation finish almost instantly and run once. Elements land on their final keyframe and stay readable.
Note: Browse all 123 effects, grouped into gradient, glitch, neon, reveal, 3D, retro, letter and textured
categories, at text-effects.colorion.co. There's also a
text effect generator for tweaking
colours and timing live.
I hope you had as much fun as I did walking through the amazing creativity present in our OSS community <3
It's truly never been a better time to be a developer :D
Note: Hats off to Mozilla and the OSS community for making this stuff happen
See the original here: https://hellalabs.dev/pure-css-animations/
Thanks for reading! Check out more CodePen here! Some of these were forked from other codepens!
What are your favorite Pure CSS animations? Let me know in the comments below!

Top comments (0)