Blog · Development

Motion without 'unsafe-inline': animations with a strict CSP in Astro

· 4 min read · by

Originally written in Portuguese. Português

Dark N3XUS home page with the headline Secure software, run by the people who built it, in large letters

4 min read

The N3XUS website is served with a strict CSP: style-src 'self' and script-src 'self'. No style="" in the HTML, no embedded <style>, no third-party scripts. We have already explained how we kept it that way in Astro.

This month we redid the entire visual design and wanted more motion. Most animation recipes out there rely on inline styles: a per-element delay in style="--i: 3", the cursor position in style="left: 120px". With our CSP, the browser blocks all of that. This post shows what we did instead.

In short

  • Position-based delay comes from CSS selectors (:nth-of-type), not from a style attribute.
  • Scroll-driven animation with animation-timeline: view(), inside @supports and prefers-reduced-motion.
  • Cursor position through the CSSOM (el.style.setProperty), which the CSP does not block, unlike a style attribute written in the HTML.
  • Measured result: Lighthouse mobile 99 for performance and 100 for accessibility, best practices and SEO on 5 pages, CLS 0.

A headline that comes in word by word

An Astro component splits the headline into words at build time. Each word sits inside a mask (overflow: hidden) and rises from within it:

.split .w { display: inline-block; overflow: hidden; }
.split .w > span { display: inline-block; animation: rise 0.9s cubic-bezier(0.2, 0.7, 0.1, 1) both; }
.split .w:nth-of-type(2) > span { animation-delay: 0.05s; }
.split .w:nth-of-type(3) > span { animation-delay: 0.1s; }
@keyframes rise { from { transform: translateY(105%); } }

The delay that would normally go in style="--i: 2" comes from the word’s position. That is six rules in the CSS and no attribute in the HTML. Since the animation only touches transform, the text is already in place from the first frame: CLS stayed at 0.

Scrolling that tells a story

The hero screenshot arrives tilted and straightens out as you scroll; on posts, a bar at the top shows how much has been read. Both are pure CSS, using scroll timelines:

@supports (animation-timeline: view()) {
  @media (prefers-reduced-motion: no-preference) {
    .h-win { animation: untilt linear both; animation-timeline: view(); animation-range: entry 0% cover 45%; }
    .read-bar { animation: grow linear both; animation-timeline: scroll(root); }
  }
}

Outside @supports, a browser that does not know animation-timeline shows the page without motion, and the content is the same. We chose not to use “fade-up on every block”, which is one of the most common signs of a template-generated page. Only large media gets an entrance on scroll.

Cursor without a style attribute

In the blog lists, a cover preview follows the mouse. The CSS positions the image with variables, and a small script updates the variables:

lista.addEventListener('pointermove', (e) => {
  lista.style.setProperty('--px', `${e.clientX}px`);
  lista.style.setProperty('--py', `${e.clientY}px`);
});

MDN explains the difference: style-src blocks the style attribute in the HTML, setAttribute('style', ...) and style.cssText, but not properties set one by one through the element.style API. The script only runs with a mouse (hover: hover and pointer: fine) and when no reduced motion is requested.

Tabs and pause without JavaScript

The home page tabs are radio + :has(): the arrow keys switch tabs, and the panel is shown by CSS. A browser without :has() shows all panels stacked one below the other.

The project screenshot wall moves on its own. Under WCAG success criterion 2.2.2, automatic motion lasting more than 5 seconds needs a pause. It stops on mouse hover, when keyboard focus is inside it, and with a “Pause motion” checkbox.

What went wrong along the way

  • Entire headline gray. An old rule, .h-hero h1 span, colored the secondary part of the headline. When every word became a span, the whole headline turned gray. We caught it in the first screenshot.
  • Panel overflowing on mobile. A fieldset has a minimum width equal to its content by default; the tabs pushed the panel off the screen. Fixed with min-inline-size: 0.
  • Scrollable regions without focus. axe flagged that the code block and the diagram scrolled horizontally but did not receive keyboard focus. The diagram got tabindex="0", and the code blocks now wrap lines on mobile.
  • The template-looking closing section. A centered card with a badge, a large heading and two buttons. We replaced it with a block that makes the email address the main element, along with who answers it, with a name and a face.

How we verified

  • Lighthouse mobile through the repository script: 99/100/100/100 on 5 pages, TBT 0 ms, CLS 0.
  • axe (WCAG 2.2 AA): no violations on 12 pages, on desktop and mobile.
  • Reduced motion: with prefers-reduced-motion: reduce, document.getAnimations() lists no running animation on any page.
  • Real CSP: the same Docker image as the server, with the nginx headers, and no errors in the console.

What we learned

  • Almost all motion fits in CSS. JavaScript only for what depends on the user (cursor, an index that follows the scroll).
  • Motion tied to an action or to scrolling reads as intentional. Motion on everything reads as a template.
  • Writing the rules down in a file (ours is docs/DESIGN.md) keeps every new page from drifting back to the default.

References

Want a website with motion and strict security headers at the same time? Talk to us.