view transitions – CSS-Tricks https://css-tricks.com Tips, Tricks, and Techniques on using Cascading Style Sheets. Mon, 17 Jun 2024 19:49:56 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://i0.wp.com/css-tricks.com/wp-content/uploads/2021/07/star.png?fit=32%2C32&ssl=1 view transitions – CSS-Tricks https://css-tricks.com 32 32 45537868 ::view-transition-image-pair https://css-tricks.com/almanac/selectors/v/view-transition-image-pair/ Fri, 14 Jun 2024 16:50:48 +0000 https://css-tricks.com/?page_id=378681 The CSS ::view-transition-image-pair pseudo-element is part of the View Transitions API that lets us select the “before-and-after” snapshots in a view transition.

::view-transition-image-pair(root) {
  animation-duration: 1s;
}

For context’s sake, ::view-transition-image-pair is one pseudo-element in a tree of pseudo-elements the …


::view-transition-image-pair originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
The CSS ::view-transition-image-pair pseudo-element is part of the View Transitions API that lets us select the “before-and-after” snapshots in a view transition.

::view-transition-image-pair(root) {
  animation-duration: 1s;
}

For context’s sake, ::view-transition-image-pair is one pseudo-element in a tree of pseudo-elements the browser establishes when a view transition is registered on the page. The ::view-transition pseudo-element is the tree’s “root” and ::view-transition-image-pair is two levels deep, contained in the ::view-transition-group pseudo-element.

::view-transition
├─ ::view-transition-group(name-1)
│  └─ ::view-transition-image-pair(name-1)
│     ├─ ::view-transition-old(name-1)
│     └─ ::view-transition-new(name-1)
├─ ::view-transition-group(name-2)
│  └─ ::view-transition-image-pair(name-2)
│     ├─ ::view-transition-old(name-2)
│     └─ ::view-transition-new(name-2)
│ /* and so one... */

This makes ::view-transition-image-pair an ancestor of the root ::view-transition and a direct child of the ::view-transition-group. In turn, ::view-transition-image-pair contains two child pseudo-elements of its own:

These two pseudos represent the “old” state before the view transition begins and the “new” state it transitions to, respectively. So, ::view-transition-image-pair becomes a handy way to select and style both of those states together.

The specification calls this the pseudo “View Transition Image Pair Isolation” which means that it adds isolation to a view transition’s old and new states. In other words, ::view-transition-image-pair is set to isolation: isloate by default. This establishes a stacking context that allows the view transition’s old and new states to sit on top of everything else to prevent it from being obscured by other elements.

Syntax

::view-transition-image-pair(<pt-name-selector>) {
  /* Styles */
}

The pseudo-element accepts a <pt-name-selector> in its argument, which is equal to one of the following:

  • <custom-ident>: Use this to select a specific transition image pair in the ::view-transition pseudo tree. For example, if a particular element has a view-transition-name of gallery, then you would use ::view-transition-image-pair(gallery) to select that transition group.
  • root: This value matches html::view-transition-image-pair(*) which is a selector set up by the browser to match any view transition image pair that is not assigned to a specific view transition via the CSS view-transition-name property.
  • Universal selector (*): Use this to select all view transition image pairs on a page.

Default styles

The specification defines the default styles for browsers to set on ::view-transition-image-pair like this:

:root::view-transition-image-pair(*) {
  position: absolute;
  inset: 0;

  animation-duration: inherit;
  animation-fill-mode: inherit;
}

Notice that the default styles target all view transitions with the Universal Selector (*). We can override these by selecting a specific view-transition-name:

::view-transition-pair(gallery) {
  animation-duration: 500ms;
}

Specification

The CSS ::view-transition-image-pair pseudo-element is defined in the CSS View Transitions Module Level 1 specification. The specification is labeled a Candidate Recommendation Snapshot, meaning it’s been widely reviewed and intended to become an official W3C Recommendation, but it is still being tested in the wild.

The specification further states:

This document is intended to become a W3C Recommendation; it will remain a Candidate Recommendation at least until 5 December 2023 to gather additional feedback.

That date has passed as of this writing, so keep an eye on the document’s status as it becomes a recommended feature.

Browser support

Data on support for the view-transitions feature across the major browsers from caniuse.com


::view-transition-image-pair originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
378681
::view-transition-image-new https://css-tricks.com/almanac/selectors/v/view-transition-image-new/ Fri, 14 Jun 2024 16:50:16 +0000 https://css-tricks.com/?page_id=378713 The CSS ::view-transition-image-new pseudo-element is part of the View Transitions API that lets us select the “new” snapshot that is transitioned to in a view transition.

::view-transition-image-new(*) {
  animation-duration: 700ms;
}

For context’s sake, ::view-transition-image-new is one pseudo-element in a …


::view-transition-image-new originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
The CSS ::view-transition-image-new pseudo-element is part of the View Transitions API that lets us select the “new” snapshot that is transitioned to in a view transition.

::view-transition-image-new(*) {
  animation-duration: 700ms;
}

For context’s sake, ::view-transition-image-new is one pseudo-element in a tree of pseudo-elements the browser establishes when a view transition is registered on the page. The ::view-transition pseudo-element is the tree’s “root” and ::view-transition-image-pair is two levels deep, contained in the ::view-transition-group pseudo-element.

And inside the ::view-transition-image-pair is where we’ll find ::view-transition-image-new:

::view-transition
├─ ::view-transition-group(name-1)
│  └─ ::view-transition-image-pair(name-1)
│     ├─ ::view-transition-old(name-1)
│     └─ ::view-transition-new(name-1)
├─ ::view-transition-group(name-2)
│  └─ ::view-transition-image-pair(name-2)
│     ├─ ::view-transition-old(name-2)
│     └─ ::view-transition-new(name-2)
│ /* and so one... */

This makes ::view-transition-image-new a direct child of the ::view-transition-image-pair. The image pair also contains the view traditions “old” snapshot, ::view-transition-image-old.

These two pseudos represent the “old” state before the view transition begins and the “new” state it transitions to, respectively. So, ::view-transition-image-pair becomes a handy way to select and style both of those states together.

The specification calls this the pseudo “View Transition New State Image” which is pretty on the nose considering that it is indeed an image of the view transition that the browser captures before the transition runs and then transitions to the new state.

Syntax

::view-transition-image-new(<pt-name-selector>) {
  /* Styles */
}

The pseudo-element accepts a <pt-name-selector> in its argument, which is equal to one of the following:

  • <custom-ident>: Use this to select a specific transition “new” image in the ::view-transition pseudo tree. For example, if a particular element has a view-transition-name of gallery, then you would use ::view-transition-image-new(gallery) to select that transition group.
  • root: This value matches :root::view-transition-new(*) which is a selector set up by the browser to match any view transition “new” image that is not assigned to a specific view transition via the CSS view-transition-name property.
  • Universal selector (*): Use this to select all “new” states of a view transition on a page.

Default styles

The specification defines the default styles for browsers to set on ::view-transition-image-new like this:

:root::view-transition-old(*),
:root::view-transition-new(*) {
  position: absolute;
  inset-block-start: 0;
  inline-size: 100%;
  block-size: auto;

  animation-duration: inherit;
  animation-fill-mode: inherit;
  animation-delay: inherit;
}

Notice that the default styles target all view transitions with the Universal Selector (*). We can override these by selecting a specific view-transition-name:

::view-transition-new(gallery) {
  animation-duration: 500ms;
}

Specification

The CSS ::view-transition-image-new pseudo-element is defined in the CSS View Transitions Module Level 1 specification. The specification is labeled a Candidate Recommendation Snapshot, meaning it’s been widely reviewed and intended to become an official W3C Recommendation, but it is still being tested in the wild.

The specification further states:

This document is intended to become a W3C Recommendation; it will remain a Candidate Recommendation at least until 5 December 2023 to gather additional feedback.

That date has passed as of this writing, so keep an eye on the document’s status as it becomes a recommended feature.

Browser support

Data on support for the view-transitions feature across the major browsers from caniuse.com


::view-transition-image-new originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
378713
::view-transition-image-old https://css-tricks.com/almanac/selectors/v/view-transition-image-old/ Fri, 14 Jun 2024 16:49:48 +0000 https://css-tricks.com/?page_id=378707 The CSS ::view-transition-image-old pseudo-element is part of the View Transitions API that lets us select the “before” snapshot in a view transition.

::view-transition-image-old(*) {
  animation-duration: 700ms;
}

For context’s sake, ::view-transition-image-old is one pseudo-element in a tree of pseudo-elements the …


::view-transition-image-old originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
The CSS ::view-transition-image-old pseudo-element is part of the View Transitions API that lets us select the “before” snapshot in a view transition.

::view-transition-image-old(*) {
  animation-duration: 700ms;
}

For context’s sake, ::view-transition-image-old is one pseudo-element in a tree of pseudo-elements the browser establishes when a view transition is registered on the page. The ::view-transition pseudo-element is the tree’s “root” and ::view-transition-image-pair is two levels deep, contained in the ::view-transition-group pseudo-element.

And inside the ::view-transition-image-pair is where we’ll find ::view-transition-image-old:

::view-transition
├─ ::view-transition-group(name-1)
│  └─ ::view-transition-image-pair(name-1)
│     ├─ ::view-transition-old(name-1)
│     └─ ::view-transition-new(name-1)
├─ ::view-transition-group(name-2)
│  └─ ::view-transition-image-pair(name-2)
│     ├─ ::view-transition-old(name-2)
│     └─ ::view-transition-new(name-2)
│ /* and so one... */

This makes ::view-transition-image-old a direct child of the ::view-transition-image-pair. The image pair also contains the view traditions “new” snapshot, ::view-transition-image-new.

These two pseudos represent the “old” state before the view transition begins and the “new” state it transitions to, respectively. So, ::view-transition-image-pair becomes a handy way to select and style both of those states together.

The specification calls this the pseudo “View Transition Old State Image” which is pretty on the nose considering that it is indeed an image of the view transition that the browser captures before the transition runs and then transitions to the new state.

Syntax

::view-transition-image-old(<pt-name-selector>) {
  /* Styles */
}

The pseudo-element accepts a <pt-name-selector> in its argument, which is equal to one of the following:

  • <custom-ident>: Use this to select a specific transition “old” image in the ::view-transition pseudo tree. For example, if a particular element has a view-transition-name of gallery, then you would use ::view-transition-image-old(gallery) to select that transition group.
  • root: This value matches :root::view-transition-old(*) which is a selector set up by the browser to match any view transition “old” image that is not assigned to a specific view transition via the CSS view-transition-name property.
  • Universal selector (*): Use this to select all “old” states of a view transition on a page.

Default styles

The specification defines the default styles for browsers to set on ::view-transition-image-old like this:

:root::view-transition-old(*),
:root::view-transition-new(*) {
  position: absolute;
  inset-block-start: 0;
  inline-size: 100%;
  block-size: auto;

  animation-duration: inherit;
  animation-fill-mode: inherit;
  animation-delay: inherit;
}

Notice that the default styles target all view transitions with the Universal Selector (*). We can override these by selecting a specific view-transition-name:

::view-transition-old(gallery) {
  animation-duration: 500ms;
}

Specification

The CSS ::view-transition-image-old pseudo-element is defined in the CSS View Transitions Module Level 1 specification. The specification is labeled a Candidate Recommendation Snapshot, meaning it’s been widely reviewed and intended to become an official W3C Recommendation, but it is still being tested in the wild.

The specification further states:

This document is intended to become a W3C Recommendation; it will remain a Candidate Recommendation at least until 5 December 2023 to gather additional feedback.

That date has passed as of this writing, so keep an eye on the document’s status as it becomes a recommended feature.

Browser support

Data on support for the view-transitions feature across the major browsers from caniuse.com


::view-transition-image-old originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
378707
::view-transition-group https://css-tricks.com/almanac/selectors/v/view-transition-group/ Wed, 12 Jun 2024 12:58:14 +0000 https://css-tricks.com/?page_id=378612 The ::view-transition-group pseudo-element selects one or more containers that group the pieces of an individual view transition.

::view-transition-group(transition-name) {
  animation-timing-function: ease-in-out;
}

See that transition-name identifier in the pseudo-element’s argument? That’s how we can select a particular view transition. View …


::view-transition-group originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
The ::view-transition-group pseudo-element selects one or more containers that group the pieces of an individual view transition.

::view-transition-group(transition-name) {
  animation-timing-function: ease-in-out;
}

See that transition-name identifier in the pseudo-element’s argument? That’s how we can select a particular view transition. View transitions are grouped together in a single ::view-transition pseudo-element, where each transition is contained in a ::view-transition-group:

::view-transition
├─ ::view-transition-group(name-1)
├─ ::view-transition-group(name-2)
│ /* and so one... */

And inside each ::view-transition-group is another pseudo-element that holds the “before-and-after” snapshots that the view transitions between.

::view-transition
├─ ::view-transition-group(name-1)
│  └─ ::view-transition-image-pair(name-1)
│     ├─ ::view-transition-old(name-1)
│     └─ ::view-transition-new(name-1)
├─ ::view-transition-group(name-2)
│  └─ ::view-transition-image-pair(name-2)
│     ├─ ::view-transition-old(name-2)
│     └─ ::view-transition-new(name-2)
│ /* and so one... */

That essentially makes ::view-transition-group the “root” element of an individual view transition. It’s a wrapper for selecting the ::view-transition-image-pair, typically for positioning that particular view transition or defining its animation properties. In fact, the specification calls it the “View Transition Named Subtree Root.”

Syntax

::view-transition-group(<pt-name-selector>) {
  /* Styles */
}

The pseudo-element accepts a <pt-name-selector> in its argument, which is equal to one of the following:

  • <custom-ident>: Use this to select a specific transition group in the ::view-transition pseudo tree. For example, if a particular element has a view-transition-name of gallery, then you would use ::view-transition-group(gallery) to select that transition group.
  • root: This value matches html::view-transition-group(*) which is a selector set up by the browser (see “Default styling” below) to match any view transition that is not assigned to a specific view transition group via the CSS view-transition-name property.
  • Universal selector (*): Use this to select all view transition groups on a page.

Default styling

Like many semantic HTML elements and pseudo-elements, browsers apply a set of default styles to ::view-transition-group.

html::view-transition-group(*) {
  position: absolute;
  top: 0;
  left: 0;

  animation-duration: 0.25s;
  animation-fill-mode: both;
}

So, if we want to position and configure the transition’s animation behavior, these are the styles we have to override in our own stylesheet.

Accessibility

It’s always a good idea to consider users with motion sensitivities any time we’re working with animations and transitions. We can use ::view-transition-group to reduce the amount of motion using the prefers-reduced-motion media query.

According to the default styles we learned about above, the browser applies an animation-duration of 0.25s. We can override that by selecting all of the view transition groups on the page by passing the universal selector in the pseudo-element’s argument and either removing or reducing that duration to accommodate motion sensitivities.

@media (prefers-reduced-motion: reduce) {
  ::view-transition-group(*): {
    animation-duration: 0;
  }
}

Specification

The ::view-transition-group pseudo-element is defined in the View Transitions Module Level 1 specification. It’s labeled a Candidate Recommendation Snapshot, meaning it’s been widely reviewed and intended to become an official W3C Recommendation, but it is still being tested in the wild.

The specification further states:

This document is intended to become a W3C Recommendation; it will remain a Candidate Recommendation at least until 5 December 2023 to gather additional feedback.

That date has passed as of this writing, so keep an eye on the document’s status as it becomes a recommended feature.

Browser support

Data on support for the view-transitions feature across the major browsers from caniuse.com

If you need to support older browsers that do not support ::view-transition, try using @supports to check whether or not the browser recognizes the view-transition-name property:

/* Apply these styles only if View Transitions are *NOT* supported */
@supports not (view-transition-name: none) {
  /* Fallback Styles */
}

More information


::view-transition-group originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
378612
::view-transition https://css-tricks.com/almanac/selectors/v/view-transition/ Fri, 07 Jun 2024 17:53:32 +0000 https://css-tricks.com/?page_id=378574 The CSS ::view-transition pseudo-element is the top-level — or “root” — containing all animated transitions defined as CSS view transitions on the page.

::view-transition {
  position: fixed;
  inset: 0;
}

Syntax

::view-transition {
  /* Styles */
}

It’s worth noting …


::view-transition originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
The CSS ::view-transition pseudo-element is the top-level — or “root” — containing all animated transitions defined as CSS view transitions on the page.

::view-transition {
  position: fixed;
  inset: 0;
}

Syntax

::view-transition {
  /* Styles */
}

It’s worth noting that ::view-transition holds all view transitions on the page (we’ll get to that in a bit), so you’re likely to see it used most often used unchained to a selector like the example above rather than, say, this:

/* 👎 */
blockquote::view-transition {
  /* Styles */
}

Left as-is, ::view-transition is scoped to the <html> element:

html::view-transition {
  /* Styles */
}

Default styles

Like many semantic HTML elements and pseudo-elements, browsers apply a set of default styles to ::view-transition.

::view-transition {
  position: fixed;
  inset: 0;
}

So, if we want to position ::view-transition ourselves, these are the styles we have to override in our own stylesheet.

It’s a “tree-abiding” pseudo-element

In other words, it’s effectively a wrapper that serves as the parent pseudo-element for others below it. The spec calls this a “tree-abiding pseudo-element” which means it contains other pseudos that can be used to style view transitions directly in CSS.

In this case, ::view-transition contains all of the view transitions on the page, organized in groups.

::view-transition
├─ ::view-transition-group(name)
│  └─ ::view-transition-image-pair(name)
│     ├─ ::view-transition-old(name)
│     └─ ::view-transition-new(name)
├─ ::view-transition-group(name)
│  └─ ::view-transition-image-pair(name)
│     ├─ ::view-transition-old(name)
│     └─ ::view-transition-new(name)
│ /* and so one... */

This way, we can define styles that apply to all view transitions together with ::view-transition and individually with ::view-transition-group.

How it fits into the view transitions process

It’s a good idea to be familiar with the steps that a view transition takes for context on where this ::view-transition tree fits in with everything.

It’s right there in Step 4 that we’re talking about.

It introduces a new stacking layer!

You know how we can use the CSS z-index property to layer elements one on top of another, like in a three-dimensional space?

We call this a “stacking context” and it happens when we declare a position on an element. In this case, both .child elements are in a stacking context where we’re using z-index to determine which element is above the other in the stack. CSS uses this information to determine in what order it applies styles:

  1. Elements with a negative stack level, typically elements with z-index: -1
  2. Elements with a position value of static.
  3. Elements with a stack level of 0, typically positioned elements with a z-index value of auto.
  4. Elements with positive stack levels, e.g. a positioned element with a z-index value of 1 or higher.

That third layer is called the “top layer” since it’s right smack dab in between layers positive and negative layers. This is where the <html> and <body> sit.

Well, it turns out that ::view-transition introduces a new stacking layer of its own, that is applied after everything else, i.e., it comes after the layer with the highest number:

  1. Elements with a negative stack level, typically elements with z-index: -1
  2. Elements with a position value of static.
  3. Elements with a stack level of 0, typically positioned elements with a z-index value of auto.
  4. Elements with positive stack levels, e.g. a positioned element with a z-index value of 1 or higher.
  5. View transitions

Here’s exactly how the specification describes it:

The ::view-transition pseudo-element generates a new stacking context, called the view transition layer, which paints after all other content of the document (including any content rendered in the top layer), after any filters and effects that are applied to such content. (It is not subject to such filters or effects, except insofar as they affect the rendered contents of the ::view-transition-old() and ::view-transition-new() pseudo-elements.)

CSS View Transitions Module Level 1, 4.2. View Transition Painting Order

What’s the big deal, you ask? This means that a view transition’s styles are painted after everything else, ensuring that they are never buried beneath other layers. This way, we never have to juggle between view transition styles and the styles of the elements on a page with z-index to make sure they’re visible. But this only takes effect if (1) there are view transitions on the page and (2) when the view transition is running. Once the transition ends, its stacking context is gone.

Specification

The CSS ::view-transition pseudo-element is defined in the View Transitions Module Level 1 specification. It’s labeled a Candidate Recommendation Snapshot which means it’s been widely reviewed and is intended to become an official W3C Recommendation, but is still being tested in the wild.

The specification further states:

This document is intended to become a W3C Recommendation; it will remain a Candidate Recommendation at least until 5 December 2023 to gather additional feedback.

That date has passed as of this writing, so keep an eye on the document’s status as it makes its way toward becoming a recommended feature.

Browser support

Data on support for the view-transitions feature across the major browsers from caniuse.com

If you need to support older browsers that do not support ::view-transition, try using @supports to check whether or not the browser recognizes the view-transition-name property:

/* Apply these styles only if View Transitions are *NOT* supported */
@supports not (view-transition-name: none) {
  /* Fallback Styles */
}

More information


::view-transition originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
378574
view-timeline-name https://css-tricks.com/almanac/properties/v/view-timeline-name/ Thu, 30 May 2024 21:21:07 +0000 https://css-tricks.com/?page_id=378389 The CSS view-timeline-name property allows us to identify and reference an element we want to animate based on its scroll position when it enters the visible area (i.e., scrollport) of a scroll container (also called the “source”).

.element {
  view-timeline-name: 


view-timeline-name originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
The CSS view-timeline-name property allows us to identify and reference an element we want to animate based on its scroll position when it enters the visible area (i.e., scrollport) of a scroll container (also called the “source”).

.element {
  view-timeline-name: --image-zoom;
}

It’s a declarative way to trigger a scroll animation; otherwise, the element (also called the “subject”) will begin its scroll-based animation when it enters the scrollport of its nearest ancestor that has one.

The view-timeline-name property can also be declared using the view-timeline property, which is a shorthand that combines view-timeline-name and view-timeline-axis in a single declaration.

.element {
  view-timeline: --image-zoom y;

  /* Equivalent to: */
  view-timeline-name: --image-zoom;
  view-timeline-axis: y;
}

Syntax

view-timeline-name: [ none | <dashed-ident> ]#
  • Initial value: none
  • Applies to: All elements
  • Inherited: No
  • Percentages: N/A
  • Computed value: none or a list of CSS identifiers
  • Canonical order: By grammar
  • Animation: Not animatable

Property Values & Descriptions

/* Keyword value: none */
view-timeline-name: none;

/* <dashed-ident> examples */
view-timeline-name: --my-timeline;
view-timeline-name: --myTimeline;
view-timeline-name: --my_timeline;
  • none: The timeline does not have a name.
  • <dashed-ident>: A custom name given to a view progress timeline, like a <custom-ident>, that declaratively identifies it and distinguishes it from other timelines. The value is called a “dashed” ident because it must be prefixed with two dashes (--) exactly like we do for CSS custom properties, e.g. --image-zoom.

Scroll progress timelines vs. View progress timelines

The difference may seem small, but a scroll progress timeline is an animation we want to run on an element visible in the current scrollport, i.e. its scroll position whether scrolling vertically (the y-axis) or horizontally (the x-axis). When the element reaches a scroll position, the animation triggers and completes at the end of the scroll container. In other words, it’s an animation timeline that progresses from 0% to 100% as the user scrolls, whereas a typical animation timeline often happens when the page has been rendered.

That’s different from a view progress timeline which is a segment or part of a scroll progress timeline. In other words, a view progress timeline is sort of like the starting ignition of a scroll progress timeline, triggering the scroll progress timeline once the element (i.e., the “subject”) is currently in view.

Illustrating the pieces of an animation timeline, scroll progress timeline, and view progress timeline.

So, what we’re talking about is a scroll progress timeline that’s scoped to the element’s visibility in the current view. Once the element’s starting edge intersects with its nearest ancestor’s scrollport — which is a fancy way of saying “the visible part of a scroll container” — the animation is handed off to the scroll progress timeline and progresses by the user’s scrolling along whichever axis — x or y — is set.

Illustration of a scroll container on a webpage, including the visible scroll port and the element waiting to animate on scroll when entering the scroll port.

The <body> element can be considered a scroll container as long as it overflows what is currently in view. That means we can trigger or start an animation when the element we want to animate enters the visible area of the <body> after which point, the animation progresses based on the element’s scroll position.

A little terminology

It’s worth noting that there are some specific words that the specification uses to describe the various pieces of a scroll progress timeline.

  • Subject: The element we want to animate.
  • Source: The subject’s nearest ancestor (such as a parent or grandparent) with a scroll container that triggers and contains the animation up until the subject’s starting edge intersects with the source’s ending edge.

It’s cool that we have this sort of context, so we’ll continue using these terms along the way.

It’s a declarative way to define a view progress timeline

We don’t have to give a name to a view progress timeline. If we don’t it’s called an anonymous timeline and the subject we want to animate enters the progress timeline when it intersects with its nearest source. In other words, the animation is triggered when the element enters the visible part of the first scroll container it encounters.

Conversely, we can explicitly give the view progress timeline an identifying name with the view-timeline-name property to establish a declarative timeline that can be called and referenced by name. It’s what we reach for when we want the subject’s animation to kick off when it enters a super-specific source’s visible area.

Anonymous view progress timelines with view()

Even if we decide not to name a view progress timeline explicitly with scroll-timeline-name, it’s still possible to configure the view timeline behavior with the view() function. The function doesn’t take a scroll-timeline-name parameter, but it does instruct the browser what a subject should do when it intersects with its source.

.subject {
  animation-timeline: view(y 10dvh);
}

So, even without an explicit view-transition-name set on the subject element, we can still direct which view-timeline-axis the animation runs on and fudging exactly when it starts it starts with a view-timeline-inset parameter.

Specification

The view-timeline-name property is defined in the Scroll-driven Animations Module Level 1 specification, which is currently in Editor’s Draft status at the time of this writing. That means the work is under discussion and could change between now and when the proposal becomes a formal Candidate Recommendation.

Browser support

IEChromeEdgeFirefoxSafariOpera
No115115111No101
iOS SafariAndroid ChromeAndroid FirefoxAndroid BrowserOpera Mobile
No125No12580

Source: caniuse (retrieved on May 30, 2024)


view-timeline-name originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
378389
view-transition-name https://css-tricks.com/almanac/properties/v/view-transition-name/ Wed, 29 May 2024 21:46:06 +0000 https://css-tricks.com/?page_id=378365 The CSS view-transition-name property uniquely identifies an element that can be used in a view transition.

card {
  view-transition-name: card-transition;
}

We give the element a name (i.e., <custom-ident) that we reference or set the property to none to …


view-transition-name originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
The CSS view-transition-name property uniquely identifies an element that can be used in a view transition.

card {
  view-transition-name: card-transition;
}

We give the element a name (i.e., <custom-ident>) that we reference or set the property to none to exclude the element from view transitions.

The specification is not a standard just yet but describes view-transition-name as a way of “tagging individually transitioning subtrees” which is a super fancy way of saying we want a certain element to be captured in a view transition. When we give an element a view-transition-name, we’re effectively giving it access to a set of pseudo-elements for creating a view transition.

Syntax

view-transition-name: none | <custom-ident>;
  • Initial value: none
  • Applies to: All elements
  • Inherited: No
  • Percentages: N/A
  • Computed value: As specified
  • Canonical order: By grammar
  • Animation type: discreet

Property Values & Descriptions

/* <custom-ident> */
view-transition-name: banner;
view-transition-name: banner-transition;
view-transition-name: bannerTransition;

/* Keyword */
view-transition-name: none;
  • <custom-ident>: A unique identifier used to define an element that can participate in a view transition, whether it is the “old” element or the “new” one that is transitioned to. We can’t use the words none or auto for the value, as they are reserved global keyword values.
  • none: Removes the element from taking part in a view transition.

What the heck is a “captured element”?

It’s worth knowing because that’s exactly how the specification describes an element with a view-transition-name declared on it. The basic idea of view transitions is that we “capture” an existing state as an image and transition between that snapshot and a new element.

When we “capture” an element, we’re sort of tracking it during the lifetime of the view transition, during which the element takes multiple forms. Here’s the process that a captured element follows, as provided by the spec, which goes further by calling a captured element a “struct” or a set of items:

  • old image: A 2D bitmap or null. Initially null.
  • old width / old height: an unrestricted double, initially zero.
  • old transform: a <transform-function>, initially the identity transform function.
  • old writing-mode: Null or a writing-mode, initially null.
  • old direction: Null or a direction, initially null.
  • old text-orientation: Null or a text-orientation, initially null.
  • old mix-blend-mode: Null or a mix-blend-mode, initially null.
  • old backdrop-filter: Null or a backdrop-filter, initially null.
  • old color-scheme: Null or a color-scheme, initially null.
  • new element: An element or null. Initially null.

Those are all the pieces of a captured element used in a view transition. We capture a set of information about an element’s current (or “old”) state and transition it into a new element.

The name has to be unique

We can name a view transition anything we want (none and auto being the only exceptions) but it has to be unique in the sense that it is only applied to one rendered element on the current page.

The key word there is “rendered” because we can technically repurpose the same view-transition-name on multiple elements. For example, you might want the same transition on a set of images but wouldn’t want to go through the work of naming each and every one. We can instead declare view-transition-name on a certain class:

.image-transition {
  view-transition-name: image-zoom;
}

Then we can set up JavaScript that that sets a variable for each image in the set, adds the .image-transition class to the images, then triggers the transition with some action — like a click handler — on the image. From there, the script waits for the transition to wrap up, then removes the .image-transition class so it can be applied to another clicked image.

// Create a variable for img elements in a .gallery
const image = document.querySelectorAll(".gallery img");

// When an image is clicked...
image.onclick = () => {
  // If view transitions are *not* supported
  if (!document.startViewTransition) {
    // Run a fallback function
    doSomething(image);
  // If view transitions are supported
  } else {
    // Add the .image-transition class to the clicked image
    image.classList.add("image-transition");

    // Start the transition and save its instance in a variable
    const transition = document.startViewTransition(() => doSomething(image));

    // Wait for the transition to finish.
    await transition.finished;

    // Remove the class when finished
    image.classList.remove("image-transition");
  }
};

Another approach is to set view-transition-name to a <custom-ident> on the clicked image, wait for the transition to run, then swap out the <custom-ident> with the none value instead.

// Create a variable for img elements in a .gallery
const image = document.querySelectorAll(".gallery img");

// When an image is clicked...
image.onclick = () => {
  // First, check if view transitions are supported
  if (!document.startViewTransition) {
    // Fallback if View Transitions API is not supported.
    doSomething(image);
  } else {
    // Set `view-transition-name` with a <custom-ident>
    image.style.viewTransitionName = "image-zoom";

    // Start the transition and save its instance in a variable
    const transition = document.startViewTransition(() => doSomething(image));

    // Wait for the transition to finish.
    await transition.finished;

    // Set the transition name to `none` when finished
    image.style.viewTransitionName = "none";
  }
};

Specification

The view-transition-name property is just one of several view-* properties that are used together to register and manage view transitions that are part of the View Transitions Module Level 1 specification, which is currently a Candidate Recommendation Snapshot meaning it can be used where available, but is in the process of collecting feedback before formal recommendation. In other words, view-transition-name and other related properties are experimental and not part of the standards just yet.

Browser support

It’s only Chrome and other Chromium-powered browsers at the moment. Keep an eye on the Caniuse pages for the most current support information for the view-transition-name property and support for the none value.

Data on support for the view-transitions feature across the major browsers from caniuse.com

As far as browser support goes, we can use the view-transition-name property to check whether or not the user’s browser supports the View Transitions API and provide a fallback for browsers that lack support. We could use a <custom-ident> value to check support, but the none value is probably a better bet since it is a consistent keyword and we definitely want that value supported if we’re working with view trasnsitions anyway.

/* If the View Transitions API is supported */
@supports (view-transition-name: none) {

}

/* If the View Transitions API is *not* supported */
@supports not (view-transition-name: none) {
  /* Use a simple CSS animation if possible */
}

Similarly, we can check for support via JavaScript as well:

// If the View Transitions API is *not* supported.
if (!document.startViewTransition) {
  doSomething());

// Otherwise, start the view transition 
} else {
  document.startViewTransition(() => doSomething());
}

view-transition-name originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
378365
Basic Multi-Page View Transition https://css-tricks.com/snippets/css/basic-view-transition/ Mon, 15 Jan 2024 19:20:08 +0000 https://css-tricks.com/?page_id=378042 @view-transition { navigation: auto; }

That goes right in your stylesheet and it’s the most basic of basic view transitions. The effect is a cross-fade between two pages.

The Chrome team had initially released a meta tag we could …


Basic Multi-Page View Transition originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
@view-transition { navigation: auto; }

That goes right in your stylesheet and it’s the most basic of basic view transitions. The effect is a cross-fade between two pages.

The Chrome team had initially released a meta tag we could plop into the <head> to opt into view transitions:

<meta name="view-transition" content="same-origin" />

That was a temporary way to access view transitions, but then the Chrome team shipped an update in May 2024 that enables multi-page view transitions by default and only requires us to opt into them with the CSS snippet above.


Basic Multi-Page View Transition originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
https://css-tricks.com/wp-content/uploads/2024/05/simple-view-transition.mp4 View Transitions Archives - CSS-Tricks nonadult 378042