Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/vtbag/website. The @vtbag website. ⭐️ Please star to support this work! ⭐️
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657---layout: "@/layouts/ExplainerLayout.astro"demoTitle: "View Transition Example: Clipped Overflow"demoText: "This example shows how to prevent morphing view transitions between two different images from growing beyond the original size of the old and new images."title: "How does it work?"---
The [previous example](/demo/BasicIM2/) demonstrated that the default styling for view transitions can force images to grow larger than the original old or new image dimensions before they fade out.
### Preventing Image OverflowPreventing the old and new images from overflowing the group outline is straightforward. The group pseudo-element is the parent of the image pair, which in turn is the parent of the image pseudo-elements. Simply setting the overflow property on the group pseudo-element will take care of it.
```css ins={1-3} ::view-transition-group(img) { overflow: clip; }```
Now the images will be cut off when they extend beyond the boundaries of the transition group. Admittedly, cutting the face in half isn't visually appealing.
### Further Improvement: Prefer Height Over WidthIn this example it looks better when we also change [the default behavior of morph transitions](/basics/default-animations/#how-the-boxes-morph) from all images being _full group width_ to _full group height_. Now, the landscape image grows in from the left, creating a nice reveal effect. Adding `right: 0` keeps the face aligned with the right edge as it fades.
```css ins={1-6} ::view-transition-old(img), ::view-transition-new(img) { block-size: 100%; inline-size: auto; right: 0; }```
### More OptionsThere are more options if you want to tweak the standard morph behavior. Here are some of the knobs you might want to turn:* inline-size and block-size, width and height: see above* z-index: The view transition API places the new image above the old one. You can change this by assigning the old image an appropriate z-index value.* opacity: As an alternative to the default cross-fade, you could only fade-out the upper image.* animation-name: if you don't need the fade, or want to define your own `mix-blend-mode` value you can set the `animation-name` to `none`* animation: if you want a different fade or even a different effect like shrinking or rotating* mix-blend-mode: when you canceled the default animation* object-fit: see [next example](/demo/BasicIM4/)* padding, borders, transforms,…
## Inspection Chamber Insights
Here are the detailed pictures as seen with the inspection chamber.
<div style="display:flex"><span id="mix1"></span><span id="mix2"></span></div>
The left one shows the effect of just setting `overflow: clip`. The right one also has the `height` (`block-size`) set to 100% and the `width` (`inline-size`) to `auto` to keep the aspect ratio.
While this is in my opinion already better than the last one, I'll show you my favorite in the [next example](/demo/BasicIM4/).