Skip to content

[Image Preview for HTML Images] Define swapping and transitions in CSS #1405

Open

Description

We could define pseudo elements within <img>:

  • ::image - the main image - displays the main image
  • ::preview - displays the preview
  • ::wrapper - contains both

Then have a series of pseudo-classes on the <img> that communicate the state of the image and preview:

  • :complete - a pseudo-class that's active when the image has fully loaded
  • :scan-complete - a pseudo-class that's active when the image has loaded an initial complete scan

This would allow defining things like cross-fades between the preview and image. You could even apply a blur to the wrapper, which animates away once the image is ready, meaning the image appears to come into focus when it's ready.

Activity

  1. jakearchibald commented on Sep 23, 2026

    @jakearchibald
    Author

    It seems like the proposal is strongly leaning towards view transitions for this currently, but I don't see the benefit, and it means a capturing cycle for things we already have rasterised.

  2. added
    Image Preview
    on Sep 23, 2026
  3. ogerchikov commented on Oct 3, 2026

    @ogerchikov
    Member

    Thanks for the suggestion! I’m open to other ways to animate the handoff between the preview and final image. My main goal is to understand whether customizable animations are part of the minimum solution developers would actually use, or whether they can be added later.

    I’m also curious how this would support CSS gradients, as you mentioned in this comment. Are you thinking that the preview source itself would be specified as a CSS property that accepts an <image> value? Right now, I understand how the pseudo-elements would let us style the preview and final image, but I’m not sure how the gradient becomes the preview.

  4. jakearchibald commented on Oct 5, 2026

    @jakearchibald
    Author

    So, with the idea in my OP, the UA stylesheet would be roughly like:

    img {
      &::preview {
        display: none;
      }
    
      &[previewsrc]:not(:complete) {
        &::preview {
          display: block;
        }
        &::image {
          display: none;
        }
      }
    }

    This doesn't fully capture the loading sequence in the explainer, but hopefully it's enough to get the idea across.

    Then, a developer could choose to do:

    <img src="…" alt="…" style="--image-preview: linear-gradient(to bottom, #FF8A5B, #FF3D77)" />

    And:

    @property --image-preview {
      syntax: "<image> | <color>";
      inherits: true;
      initial-value: transparent;
    }
    
    @container not style(--image-preview: transparent) {
      img:not(:complete) {
        &::preview {
          display: block;
          background: var(--image-preview);
          background-size: cover;
        }
        &::image {
          display: none;
        }    
      }
    }

    Hopefully it's clear that you could easily animate/transition between these states.

  5. bramus commented on Oct 5, 2026

    @bramus

    I think you could do this without introducing ::image or ::wrapper, by layering the ::preview on top of the image when the browser deems it necessary to show it.

    Advantage of not using ::image is that it remains clear to what part the styles declared on img itself apply to. Say you have a display: block declared on img … does that apply to the ::image, ::preview, ::wrapper, or all? And what does getComputedStyle on img return when you’ve manipulated the ::wrapper? There have been similar discussions for ::interest-button and interest invokers — w3c/csswg-drafts#13981 (comment)

    The downside of using only ::preview is that it would require some form or @exit-style though, to animate-out the ::preview when it’s no longer necessary to show — w3c/csswg-drafts#9500

  6. jakearchibald commented on Oct 5, 2026

    @jakearchibald
    Author

    I think you could do this without introducing ::image or ::wrapper, by layering the ::preview on top of the image when the browser deems it necessary to show it.

    I think those pieces are necessary for three reasons:

    • Alpha transparency - Both the image and its preview may contain an alpha channel, so simply layering the preview over the top would be additive rather than replacing. Better to do proper replacing.
    • Animated images - If the main image is display:none whilst the preview is showing, it isn't trying to play animations. This means, when the swap occurs, the animation starts.
    • Cross-fading - Same as with view transitions, the wrapper allows you to apply the isolation to produce a true cross-fading animation from one to the other. I guess the image itself could act as that wrapper, but it depends if you'd want things like blurs to impact borders on the image or not.
  7. jakearchibald commented on Oct 5, 2026

    @jakearchibald
    Author

    And what does getComputedStyle on img return when you’ve manipulated the ::wrapper?

    I don't understand the issue here. To be clear, the ::wrapper is still inside the img, not outside it. Similar to how the ::view-transition-image-pair is inside the ::view-transition-group. Maybe instead of ::wrapper it should be ::image-pair.

  8. ogerchikov commented on Oct 7, 2026

    @ogerchikov
    Member

    Thanks, this helps clarify how the CSS approach could define and customize the handoff between preview and final image states.

    However, I have a more fundamental question. My understanding was that this issue was primarily proposing an alternative to using View Transitions for the preview-to-final-image handoff. But in your example, CSS can also be used to specify the preview itself (for example via --image-preview and ::preview).

    Should I think of this proposal as:

    1. An alternative to View Transitions for defining the handoff and animations between image states, while still relying on something like Image.previewSrc / previewsrc to provide the preview image?
      or
    2. A broader alternative to the Image.previewSrc approach itself, where the preview is specified entirely through CSS rather than an HTML attribute/API?

    I'm trying to understand whether the proposal is focused on transitions only, or whether it is also proposing a different mechanism for supplying preview content.

  9. jakearchibald commented on Oct 8, 2026

    @jakearchibald
    Author

    Advantages of the system I'm proposing over the current one:

    • It avoids all the screen capturing & position animation stuff that comes with view transitions, which is unnecessary because we already have the before and after states as decoded images.
    • It allows both states to be animated images during a transition.
    • It allows blurring the image and/or preview, without blurring the <img> itself (which would also blur its borders etc).
    • It allows CSS to fully define the appearance of the preview, allowing things like gradients.

    If we didn't have previewsrc and just relied on CSS, then the preview download wouldn't start until styles resolve, which seems far too late. It might also make other things tricky like different previews for different <source>s, but I haven't thought too much about it.

    So, this proposal is more than just 1, as a key benefit is that it does allow pure CSS representations of the preview. But, I didn't intend this to replace the previewsrc attribute. In my proposal, previewsrc would be used to set the replaced content of ::preview.

  10. jakearchibald commented on Oct 29, 2026

    @jakearchibald
    Author

    Something that came up at TPAC: We might have an opportunity here to give developers control of failed-to-load images, and the display of alt text during & after loading.

  11. ogerchikov commented on Oct 29, 2026

    @ogerchikov
    Member

    Something that came up at TPAC: We might have an opportunity here to give developers control of failed-to-load images, and the display of alt text during & after loading.

    Do you have more context for this?

  12. jakearchibald commented on Oct 29, 2026

    @jakearchibald
    Author

    It was just a hallway chat. They're states of an image that are currently difficult to customise.

  13. ogerchikov commented on Oct 29, 2026

    @ogerchikov
    Member

    I think we could build on your image/preview/group idea and make it useful beyond previews:

    <img>
    └── ::image-group
        ├── ::image-placeholder
        ├── ::preview
        └── ::image
    

    A separate placeholder part would let authors use a color, gradient, or shimmer while waiting for a preview or when there isn’t one. The group would handle shared clipping and isolation.

    The rough idea would be to:

    • Style each part independently, without also affecting the image’s border or padding.
    • Expose the current presentation through something like :image-phase(none | waiting | preview | image), rather than treating it as a network-completion signal.
    • Use normal CSS transitions, with display transitions keeping outgoing placeholder/preview content around while it animates out.

    For example, using the current previewsrc name and the proposed CSS hooks:

    <img class="photo"
         src="photo.avif"
         previewsrc="thumb.avif"
         width="800" height="600"
         alt="A mountain reflected in a lake">
    .photo::image-placeholder {
      background: linear-gradient(135deg, #e2e8f0, #cbd5e1);
    }
    
    .photo::preview {
      filter: blur(4px);
    }
    
    .photo::image-placeholder,
    .photo::preview {
      mix-blend-mode: plus-lighter;
      transition:
        opacity 180ms,
        display 180ms allow-discrete;
    }
    
    .photo::image {
      mix-blend-mode: plus-lighter;
      transition: opacity 180ms;
    }
    
    .photo:not(:image-phase(image))::image {
      opacity: 0;
    }
    
    @media (prefers-reduced-motion: reduce) {
      .photo::image-placeholder,
      .photo::preview,
      .photo::image {
        transition: none;
      }
    }

    With UA-provided active/inactive and entry styles, this would show a gradient placeholder, fade into a blurred preview, then crossfade into the main image.

    Separately, authors could choose native/progressive reveal timing or wait for a complete, decoded first frame, using the same policy for placeholder and preview handoffs. The exact mechanism needs definition (for example image-reveal: normal | complete-frame CSS property).

    We could start with just the placeholder/image parts and add preview support later. That would let the CSS work move independently of the HTML preview API and partially address the styling use cases in w3c/csswg-drafts#7467.

    This also leaves room for failed-image and alt-text styling through resource-state selectors or additional parts, rather than trying to make ::preview cover everything.

    Please let me know what you think about this.

  14. jakearchibald commented on Oct 30, 2026

    @jakearchibald
    Author

    I'm not sure about image-reveal. It sounds like we could achieve that with more pseudo-classes. But the general direction seems good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Image Preview

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions