🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Styling••By Komal Nakrani

Astro Animation: CSS, Motion, GSAP and More

Astro animation options compared: CSS transitions and scroll-driven animations, view transitions, Motion, GSAP, AOS, Lenis and auto-animate, with code for each.

#Astro#Astro Animation#CSS#GSAP#Motion

For Astro animation, start with CSS: transitions, keyframes and scroll-driven animations cover hover effects, entrances and parallax with no JavaScript. Add view transitions for page-to-page motion. When you need timelines, scroll-linked sequences or physics, load a library in a <script> tag: Motion for small, modern animations, GSAP for complex timelines and ScrollTrigger effects, AOS for simple scroll reveals, and Lenis for smooth scrolling. None of them needs a UI framework or an island.

This guide covers each option with Astro code, what it costs, and how to keep animation from hurting performance and accessibility.

Key Takeaways

  • CSS handles most website animation in Astro with zero JavaScript.
  • Animation libraries load in a plain <script> in an .astro component; Astro bundles them as modules.
  • Motion and GSAP are the two main JavaScript options; GSAP is the heavier, more capable one.
  • With <ClientRouter />, re-run animation setup on the astro:page-load event.
  • Always respect prefers-reduced-motion.

Astro animation options at a glance

Option JavaScript Good for Notes
CSS transitions and keyframes None Hover, focus, entrances, loaders Use transform and opacity
CSS scroll-driven animations None Reveal on scroll, progress bars, parallax Check current browser support
View transitions None (native) or router script Page-to-page motion Built into Astro
Motion (motion) Small, tree-shakeable Entrances, scroll triggers, springs Vanilla API, no React needed
GSAP Larger Timelines, ScrollTrigger, SVG morphing Free, including its plugins
AOS Small Simple fade/slide reveals with attributes Less control
Lenis Small Smooth scrolling Often paired with GSAP
auto-animate Tiny Animating list changes One line per container
Lottie Large player Designer-made vector animations Use sparingly

CSS: the default for Astro animation

Astro’s scoped <style> tags make CSS animation the natural first choice. A card hover and a fade-in entrance:

---
// src/components/FeatureCard.astro
const { title, body } = Astro.props;
---
<article class="card">
  <h3>{title}</h3>
  <p>{body}</p>
</article>

<style>
  .card {
    transition: transform 200ms ease, box-shadow 200ms ease;
    animation: rise 500ms ease-out both;
  }
  .card:hover {
    transform: translateY(-4px);
    box-shadow: 0 12px 24px rgb(0 0 0 / 0.08);
  }
  @keyframes rise {
    from { opacity: 0; transform: translateY(12px); }
    to { opacity: 1; transform: none; }
  }
  @media (prefers-reduced-motion: reduce) {
    .card { animation: none; transition: none; }
  }
</style>

Animate transform and opacity where you can. They run on the compositor and don’t trigger layout, which keeps animations smooth and avoids layout shift.

Scroll-driven animations in CSS

CSS can now tie an animation to scroll position with animation-timeline:

/* src/styles/global.css */
@supports (animation-timeline: view()) {
  .reveal {
    animation: rise linear both;
    animation-timeline: view();
    animation-range: entry 0% cover 30%;
  }
}

@keyframes rise {
  from { opacity: 0; transform: translateY(24px); }
  to { opacity: 1; transform: none; }
}

Wrapping it in @supports means browsers without support simply show the content. Browser support has grown but isn’t universal yet, so check caniuse before relying on it for anything essential.

View transitions for page changes

For motion between pages, Astro supports native cross-document view transitions with one CSS rule, or <ClientRouter /> for client-side navigation with transition:name and transition:animate directives. This covers most “app-like” page motion without a library. Our guide to Astro view transitions has the full setup.

Motion

Motion (the motion package, from the team behind Framer Motion) has a vanilla JavaScript API that works directly in Astro scripts:

# terminal
npm install motion
---
// src/components/Stats.astro
---
<section class="stats">
  <div class="stat">Fast pages</div>
  <div class="stat">Static output</div>
  <div class="stat">Deploy anywhere</div>
</section>

<script>
  import { animate, inView, stagger } from 'motion';

  if (!window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
    inView('.stats', (element) => {
      animate(
        element.querySelectorAll('.stat'),
        { opacity: [0, 1], y: [16, 0] },
        { delay: stagger(0.1), duration: 0.5 }
      );
    });
  }
</script>

Astro bundles the script as a module and includes it once, however many times the component appears. Motion is tree-shakeable, so importing animate and inView keeps the bundle small. It also has scroll() for scroll-linked animation and spring easing.

If you’re inside a React island, motion/react gives you the <motion.div> component API, but that brings React with it. For static Astro components, the vanilla API is enough.

GSAP

GSAP is the most capable option: timelines, ScrollTrigger, pinning, SVG morphing, text splitting and more. Since 2025 it’s free to use, including the plugins that used to require a paid membership.

# terminal
npm install gsap
---
// src/components/Hero.astro
---
<section class="hero">
  <h1 class="hero-title">Build faster websites</h1>
  <p class="hero-sub">Static pages, zero JavaScript by default.</p>
</section>

<script>
  import { gsap } from 'gsap';
  import { ScrollTrigger } from 'gsap/ScrollTrigger';

  gsap.registerPlugin(ScrollTrigger);

  const mm = gsap.matchMedia();
  mm.add('(prefers-reduced-motion: no-preference)', () => {
    const tl = gsap.timeline();
    tl.from('.hero-title', { y: 40, opacity: 0, duration: 0.6 })
      .from('.hero-sub', { y: 20, opacity: 0, duration: 0.4 }, '-=0.3');

    gsap.to('.hero', {
      backgroundPositionY: '30%',
      scrollTrigger: { trigger: '.hero', start: 'top top', scrub: true },
    });
  });
</script>

gsap.matchMedia() is the cleanest way to respect reduced motion: animations only run when the query matches, and GSAP reverts them if it stops matching. GSAP is larger than Motion, so use it when you need its timeline and scroll features, not for a single fade.

AOS, Lenis and auto-animate

AOS (Animate On Scroll) uses data attributes: <div data-aos="fade-up">. Call AOS.init() once in a layout script. It’s easy for simple reveals but less flexible, and CSS scroll-driven animations now cover the same ground in supporting browsers.

Lenis adds smooth, momentum-style scrolling. It’s often combined with GSAP ScrollTrigger on agency and portfolio sites. Smooth scrolling changes how the page feels to scroll, so test it with keyboards, trackpads and assistive tech, and disable it for reduced motion.

auto-animate (@formkit/auto-animate) animates elements being added, removed or reordered inside a container with one call: autoAnimate(listElement). It’s useful for filterable lists and FAQ toggles.

Lottie plays After Effects animations exported as JSON. The player is large relative to everything else on an Astro page, so load it with a dynamic import() only when the animation scrolls into view.

Animations and the ClientRouter

If you use <ClientRouter />, bundled scripts run once, on the first page. After navigation, the new page’s elements have no animations attached. Wrap setup in the astro:page-load event:

<script>
  import { animate, inView } from 'motion';

  document.addEventListener('astro:page-load', () => {
    inView('.reveal', (el) => {
      animate(el, { opacity: [0, 1], y: [16, 0] }, { duration: 0.4 });
    });
  });
</script>

For GSAP, kill ScrollTriggers from the previous page before creating new ones (ScrollTrigger.getAll().forEach((t) => t.kill()) in astro:before-swap), or they keep running against removed elements.

Performance and accessibility rules

  • Don’t hide content until JavaScript runs. If a reveal animation sets opacity: 0 in CSS and the script fails, the content stays invisible. Set the hidden state from JavaScript, or use @supports with CSS-only reveals.
  • Avoid animating layout properties (width, height, top, margin). They cause layout work and can register as layout shift.
  • Load heavy libraries only where used. A <script> in a component is included only on pages that render that component.
  • Respect prefers-reduced-motion in CSS and in JavaScript, as in the examples above.
  • Keep entrances short. 200 to 500 ms feels responsive; long entrances delay reading.

Our Astro performance guide covers measuring the effect on Core Web Vitals.

Many Komal portfolio and agency themes use CSS and small scripts for scroll reveals rather than heavy libraries; you can browse portfolio and agency themes or read the styling docs. For script bundling details, see the official Astro client-side scripts guide.

FAQ

How do I add animations to an Astro site?

Use CSS transitions and keyframes in a component’s <style> tag for most effects. For scroll-triggered or timeline animations, install a library such as Motion or GSAP and import it in a <script> tag inside an .astro component.

Can I use GSAP with Astro?

Yes. Install gsap, import it and any plugins like ScrollTrigger in a <script> tag, and register the plugins. No integration or UI framework is needed. With <ClientRouter />, run your setup on astro:page-load.

Can I use Framer Motion in Astro?

Framer Motion is now Motion. Its vanilla motion package works in plain Astro scripts. The React component API (motion/react) works inside React islands, which requires @astrojs/react and a client directive.

Do animations slow down Astro sites?

CSS animations on transform and opacity have little cost. JavaScript libraries add download and execution time, and animations that move layout can cause layout shift. Load libraries only on pages that use them and keep effects short.

How do I animate elements on scroll in Astro?

Use CSS scroll-driven animations with animation-timeline: view() inside an @supports block, or an IntersectionObserver-based helper such as Motion’s inView(). Both reveal content as it enters the viewport.