Blur Image
stableDisplays a blurred preview while an image loads, then brings the full image into focus.

Features
- Generates a blurred preview from the source image.
- Sharpens the full image once it has loaded and scrolled into view.
- Lets you disable the opacity fade while preserving the sharpening effect. See When to disable the opacity fade
- Respects reduced-motion preferences and displays the full image without JavaScript.
Basic usage
Wrap one Astro Image or Picture in BlurImage. Pass the same source to both: the wrapper generates the placeholder, and the inner component renders the full image.
---
import { Image } from "astro:assets";
import { BlurImage } from "@ulubit/ui";
import pattern from "./resolution-pattern.png";
---
<BlurImage src={pattern}>
<Image
src={pattern}
alt="Orange circle over vertical stripes of decreasing width"
/>
</BlurImage>API Reference
| Prop | Type | Default | Description |
|---|---|---|---|
src |
ImageMetadata |
Required | Imported local image used to generate the placeholder. |
fade |
boolean |
true |
Controls opacity. Defaults to opacity: 0. |
Standard div attributes apply to the wrapper. Set alt and any responsive sizing props on the inner Image or Picture. To choose widths and sizes for your layout, see Responsive image sizing for the web.
When to use the fade prop
By default, BlurImage starts with opacity: 0, . Even after the image loads, it won’t count toward Largest Contentful Paint (LCP) until it’s fully visible. w3.org
For an image likely to be considered the page’s LCP, such as a hero image, consider setting fade={false}. This sets the image opacity: 1 while retaining the blur effect transition, reducing the time of the LCP.
In our test, that change reduced LCP from 2.1 seconds to 0.4 seconds.
Source code
BlurImage.astro
---
import type { ImageMetadata } from 'astro';
import type { HTMLAttributes } from 'astro/types';
import { getImage } from 'astro:assets'
import "./blur-image.css"
interface Props extends HTMLAttributes<"div"> {
src: ImageMetadata
fade?: boolean
}
const {
src,
class: className,
style,
fade = true,
...rest
} = Astro.props
const blurImage = await getImage({
src,
width: 24,
quality: 30,
})
const blurPlaceholderImage = `url(${blurImage.src})`
// Preserve consumer styles alongside the generated blur placeholder.
// Astro accepts styles as strings or objects, so both must be handled.
const mergedStyle = typeof style === "string"
? `${style}; --blur-placeholder-image: ${blurPlaceholderImage}`
: {...style, "--blur-placeholder-image": blurPlaceholderImage}
---
<div
class:list={["blur-image", className, !fade && "fade-off"]}
style={mergedStyle}
data-blur-image
{...rest}
>
<slot />
</div>
<script>
import "./blur-image"
</script>Collapse code
blur-image.css
.blur-image {
position: relative;
isolation: isolate;
overflow: hidden;
height: 100%;
width: 100%;
}
.blur-image::before {
content: "";
position: absolute;
inset: 0;
z-index: 0;
background-image: var(--blur-placeholder-image);
background-position: center;
background-size: cover;
filter: blur(6px);
pointer-events: none;
}
.blur-image.error::before {
display: none;
}
/* :where() for zero specificity */
.blur-image :where(img) {
position: relative;
z-index: 1;
display: block;
width: 100%;
height: 100%;
object-fit: cover;
opacity: 0;
filter: blur(6px);
transition:
opacity var(--transition-duration-slow),
filter var(--transition-duration-slow);
}
.blur-image.loaded :where(img) {
opacity: 1;
filter: none;
}
.blur-image.fade-off :where(img) {
opacity: 1;
}
.blur-image.error :where(img) {
opacity: 1;
filter: none;
transition: none;
}
@media (prefers-reduced-motion: reduce) {
.blur-image :where(img) {
transition: none;
}
}
/* for when js is disabled */
@media (scripting: none) {
.blur-image :where(img) {
opacity: 1;
filter: none;
}
}Collapse code
blur-image.ts
let imageObserver: IntersectionObserver | undefined
function initBlurImages() {
imageObserver?.disconnect()
const wrappers = document.querySelectorAll<HTMLElement>('[data-blur-image]')
imageObserver = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (!entry.isIntersecting) return;
const wrapper = entry.target as HTMLElement
const img = wrapper.querySelector('img')
observer.unobserve(wrapper)
if (!img) return
const cleanup = () => {
img.removeEventListener('load', reveal)
img.removeEventListener('error', fail)
}
const reveal = () => {
cleanup()
wrapper.classList.add('loaded')
}
const fail = () => {
cleanup()
wrapper.classList.add('error')
}
img.addEventListener('load', reveal, {
once: true
})
img.addEventListener('error', fail, {
once: true
})
if (img.complete) {
if (img.naturalWidth > 0) reveal()
else fail()
}
})
}, { threshold: 0.1 })
wrappers.forEach(wrapper => imageObserver?.observe(wrapper))
}
initBlurImages()
document.addEventListener("astro:after-swap", initBlurImages)