---
layout: "@/layouts/ExplainerLayout.astro"
demoTitle: "View Transition Example: Basic Image Morph"
demoText: "This example demonstrates the default morph animation between a small and a large version of the same image."
title: "How does it work?"
---
This example resembles he typical teaser & details pattern. The morphing image guides your eyes from the teaser to the details.
### HTML
Similar to the [Basic Same-Document example](/demo/BasicS/), this setup involves two DOM trees that are toggled via the `startViewTransition()` callback. The first DOM tree, the teaser, displays a card containing a small version of an image, while the second DOM tree, the details, represents a detailed view with a heading, descriptive text, and a larger version of the image.
The approach shown here works consistently for both same-document and cross-document view transitions. However, using the same-document variant simplifies a bit the scaffolding required for these examples.
```html
Teaser Heading
This is a teaser text for the card.
Details
Some text that describes the details of what the card teases in more
detail. Click >here< to return to teaser view.
```
For the styling of the elements see the source of this page.
### View Transition specific CSS
To define a morph transition from one element to another, you have to assign the same view transition name to both elements. In this example we morph between two images, the card's image and the article's image.
```css
.card img,
article img {
view-transition-name: img;
}
```
It is not important that the two elements are similar or even have the same tag. Technically, you can morph between any arbitrary elements, e.g. transforming a form into a table. However, achieving a visually appealing result may require some CSS adjustments.
### Script Part
The script attaches click event listeners to both DOM trees, enabling interaction. When a click occurs, the listener toggles the active class between the two DOM trees. The CSS ensures that only the active DOM tree is visible, while the other is hidden using `display: none`. The View Transition API effectively ignores hidden parts of the DOM, which is why it's possible to assign the same view transition name, img, to both images. Although view transition names need to be unique, the uniqueness is preserved because only the visible part of the DOM is considered.
```html
````
If you want to learn more about why `mayViewTransition()` is used here, see the description in the [Basic Same-Document example](/demo/BasicS/).
## Inspection Chamber Insights
With the inspection chamber, you can examine the details of the default morph animation. This built-in effect works as follows:
- At the beginning of the view transition, the group pseudo-element is transformed to match the position and size of the old element. Both the old and new images are rendered on the group pseudo-element.
- As the transition progresses, the group pseudo-element gradually shifts in position and size until it aligns with the new element's dimensions. Throughout this movement, the old and new images continue to be displayed on the group element. While the group's size is interpolated between the start and end states, the specifics of how the old and new images are resized during the transition might involve more complexity. We will explore this further in upcoming examples. In this example where old and new images have the same aspect ratio, the size of the images are also just interpolated.
- During the transition, the new image, which is drawn above the old image, gradually fades in while the old image fades out.
The first two bullet points are implemented by the browser defined `-ua-view-transition-group-anim-img` animation. The last bullet point is implemented by the default fades `-ua-view-transition-fade-out` and `-ua-view-transition-fade-in` of the old and new image.


The panel displays the details of the `-ua-view-transition-group-anim-img` animation. The live values show how the `width` and `height` grow as the image is getting larger and the `transform` value changes with the position of the image.
In the panel you can also see an additional animation for the old and new images: `-ua-mix-blend-mode-plus-lighter` ensures that the colors of the semi-transparent images add up to the original color values and that the sum of those images is never looks under or over exposed.
Everything that is outside of the images where we assigned the view transition name `img` is controlled by the `root` group. As we did not specify something else, this has the default animation: it fades out the card and fades in the detailed view.