Getting Started
Enable view transitions by adding the<ViewTransitions /> component to your layout’s <head>:
src/layouts/Layout.astro
With just this one component, Astro handles all the complexity of page transitions, including routing, animation, and state management.
How It Works
When a user clicks a link:1
Intercept Navigation
Astro intercepts the navigation and prevents the default page load
2
Fetch New Page
The new page is fetched in the background
3
Animate Transition
Elements fade out, move, or morph based on their transition names
4
Update Content
The DOM is updated with the new page content
5
Complete
Elements animate in and the transition completes
Basic Transitions
By default, all pages get a cross-fade transition. Customize transitions using thetransition:* directives:
src/pages/index.astro
Transition Directives
transition:name
Persist or morph elements across pages by giving them the same name:src/pages/products/[id].astro
transition:name will smoothly morph between pages:
src/pages/products/index.astro
transition:persist
Keep elements in the DOM across page transitions:transition:animate
Customize animation styles:Built-in Animations
- Fade
- Slide
- Custom
Directional Animations
Create different animations for forward and backward navigation:slide animation automatically:
- Slides left when going forward
- Slides right when going back
Lifecycle Events
Listen to transition events to run code during transitions:Event Reference
Event
Fires before fetching the new page
Event
Fires after fetching and parsing the new page
Event
Fires before updating the DOM
Event
Fires after updating the DOM
Event
Fires when the page is fully loaded and interactive
Preventing Transitions
Disable transitions for specific links:Fallback Behavior
View Transitions gracefully degrade in browsers that don’t support the View Transitions API:- Modern browsers: Smooth animated transitions
- Older browsers: Standard page navigation
- JavaScript disabled: Normal links work as expected
The View Transitions API is supported in Chrome, Edge, and other Chromium browsers. Safari and Firefox users get standard navigation.
Preserving State
Form Inputs
Persist form state across navigation:Media Playback
Keep videos and audio playing:Third-Party Scripts
Persist widgets that shouldn’t reinitialize:Advanced Patterns
Loading Indicators
Show loading state during transitions:src/components/LoadingBar.astro
Scroll Restoration
Customize scroll behavior:Page-Specific Transitions
Different animations for different pages:src/pages/index.astro
src/pages/gallery.astro
View Transition Scope
Create isolated transition contexts:Performance Tips
1
Use transition:name sparingly
Only add
transition:name to elements that truly need morphing. Too many can impact performance.2
Keep animations short
Transitions under 300ms feel snappy. Avoid durations over 500ms.
3
Optimize images
Use optimized images for smoother transitions, especially for morphing elements.
4
Test on slow connections
View transitions wait for the new page to load. Test on throttled connections.
Accessibility
View Transitions respect user preferences:- Announces page changes to screen readers
- Updates the document title
- Manages focus appropriately
- Respects
prefers-reduced-motion
Troubleshooting
Transitions not working
Transitions not working
- Ensure
<ViewTransitions />is in your layout’s<head> - Check that you’re using the same layout across pages
- Verify the View Transitions API is supported in your browser
Elements flashing
Elements flashing
- Use
transition:persistfor elements that should maintain state - Ensure CSS is loaded before the transition starts
- Check for conflicting animations
Scripts not running
Scripts not running
- Use the
astro:page-loadevent instead ofDOMContentLoaded - Wrap script logic in event listeners that re-run on navigation
Related Resources
Islands
Client-side interactivity
Routing
Page routing and navigation
Layouts
Shared page layouts
Performance
Optimization techniques