---
layout: "@/layouts/ExplainerLayout.astro"
demoTitle: "View Transition Example: Basic Same-Document"
demoText: "This example demonstrates the default cross-over effect of view transitions. Press the brownish tab header to start the transition."
title: "How does it work?"
---
This example features a display with two tabs. Selecting a tab switches the content.
### HTML
The HTML part for this example just defines two tab headings and two alternative tab contents. The CSS class `active` is used to highlight the current heading and to select the tab-content to be shown.
```html
Why don't skeletons fight each other?
They don't have the guts.
I'm reading a book on anti-gravity.
It's impossible to put down!
I'm on a seafood diet.
I see food and I eat it.
Why don't some couples go to the gym?
Because some relationships don't work out.
```
For the styling see the source of this page.
### Script Part
The script part adds listeners for the click event to the tab headings.
```html
```
When clicked, the listener first removes all `active` classes from headings and content areas and then sets the clicked heading to active as well as the corresponding content area. CSS does the rest and highlights and hides the elements accordingly.
To get a smooth transition effect, we run those DOM updates inside `document.startViewTransition()`.
To ensure compatibility across different browsers, the script uses a wrapper function called `mayViewTransition`. This wrapper checks if the native view transition API is supported. If it is, `document.startViewTransition` is called; if not, the DOM-changing code is called directly. This approach is a best practice for using `document.startViewTransition`, as it prevents errors in browsers that lack native support for view transitions.
The [Utensil-Drawer package](/tools/utensil-drawer/#maystartviewtransition) provides the `mayViewTransition` function as a convenient utility for this purpose. It is way more elaborated and supports various real-world key challenges.
### View Transition specific CSS
In this example, no specific view transition names are defined. Instead, the example relies on the default view transition name, `root`, which the browser automatically applies to the `` element. This means that the old and new images are those of the viewport before and after the transition and the default effect is a cross-fade between the two images.