Skip to content

Astro Responsive Image Quick Reference

Target Audience: Designers + Frontend Developers Goal: Quickly and correctly handle image assets in website development Based on: Device Matrix v2025.1 (2025-10-10)


1.1 Design Reference Dimensions (Best Practices)

Section titled “1.1 Design Reference Dimensions (Best Practices)”
Site TypeDesign WidthNotes
Desktop Site3840pxCorresponds to 4K display physical width, covers Tablet/Laptop/Desktop
Mobile Site1320pxCorresponds to maximum Mobile device physical width (iPhone Pro Max)

Important Principles:

  • Maximum export size of an image = Actual size of the image in the design file
  • Example: In a Desktop design file (3840px), if an image width is 1200px, then the maximum export width is 1200px

Image TypeRecommended FormatQuality SettingsNotes
PhotographyJPEGQuality 90-95, sRGB color spaceCan reduce to 88-90 if file size is abnormally large
Transparent ImagesPNGLosslessLogos, illustrations, images requiring transparent backgrounds
Logo/IconsPNG or SVGLosslessPrefer SVG (vector format)
  • Confirm the actual width (px) of the image in the design file
  • Export at actual width (do not upscale)
  • Select the correct format (Photography→JPEG, Transparent→PNG)
  • Check quality settings (JPEG: 90-95)
  • Confirm color space is sRGB
  • Clear file naming (e.g., hero-desktop.jpg, card-thumbnail.jpg)

ComponentUse CaseAdvantagesDisadvantages
<Picture>Default RecommendedSupports multi-format fallback (AVIF→WebP→JPEG)More verbose HTML
<Image>Single format requirementMore concise HTMLRequires manual format specification

Recommended Strategy:

  • Photography: Use <Picture>, format order ['avif', 'webp', 'jpeg']
  • Transparent Images: Use <Picture>, format order ['avif', 'webp', 'png']
  • WebP Only: Use <Image>, format 'webp'

Copy the utils/image_breakpoints.ts file to your project (complete code in Astro Responsive Image Specification)

Step 2: Determine Parameters Based on Design File

Section titled “Step 2: Determine Parameters Based on Design File”

Based on the image dimensions exported by the designer, determine utility function parameters:

Design ScenarioUtility Function ParametersNotes
Image fills design width (3840/1320){}Default full width, no parameters needed
Image width in design is 1200px{ maxWidth: 1200 }Specify maximum export width of the image
Image occupies 30% of design width{ ratio: 0.3 }Specify ratio of image to viewport width
Fixed size image (e.g., 100×100 avatar){ minWidth: 100, maxWidth: 100, roundStep: 1 }Fixed size range

Key Principle: maxWidth should equal the actual width of the image in the design file

Using <Picture> Component (Recommended):

---
import { Picture } from "astro:assets";
import hero from "@/assets/hero.jpg";
import { computeDesktopBreakpoints } from "@/utils/image_breakpoints";
// Set parameters based on the actual image width in the design file
// Example: Image width in design is 1200px
const { widths } = computeDesktopBreakpoints({ maxWidth: 1200 });
// Or: Image occupies 30% of design width
// const { widths } = computeDesktopBreakpoints({ ratio: 0.3 });
// Or: Full width image (fills 3840px)
// const { widths } = computeDesktopBreakpoints({});
---
<Picture
src={hero}
alt="Description text"
widths={widths}
formats={["avif", "webp", "jpeg"]}
{/* Use jpeg for photography, png for transparent images */}
sizes="100vw"
{/* Adjust based on actual layout: 100vw for full width, 30vw for fixed ratio, 25px for fixed pixels */}
/>

Using <Image> Component (Single Format):

---
import { Image } from "astro:assets";
import hero from "@/assets/hero.jpg";
import { computeDesktopBreakpoints } from "@/utils/image_breakpoints";
const { widths } = computeDesktopBreakpoints({ maxWidth: 1200 });
---
<Image
src={hero}
alt="Description text"
format="webp"
{/* Single format: webp/jpeg/png */}
widths={widths}
sizes="100vw"
/>

Utility Function Parameter Description:

// ratio: Ratio of image to viewport width (0-1), e.g., 0.3 means 30%
// maxWidth: Maximum image width (px), typically equals image width in design file
// minWidth: Minimum image width (px), used for fixed sizes or filtering small breakpoints
// roundStep: Rounding step (px), default 16, recommended 1 for fixed size scenarios
// designViewportWidth: Design viewport width (px), default Desktop 3840, Mobile 1320

Layout Typesizes AttributeNotes
Full Width"100vw"Image fills viewport width
Fixed Ratio"30vw"Image occupies 30% of viewport width
Fixed Pixels"25px"Image fixed at 25px CSS width
Responsive Mixed"(min-width: 1024px) 50vw, 100vw"Desktop 50%, Mobile 100%
Complex Responsive"(min-width: 1024px) 800px, 100vw"Desktop fixed 800px, Mobile 100%

Writing Rules:

  • Write media queries from large screen to small screen
  • Last value is the default (no media query)
  • Units: vw (viewport percentage) or px (fixed pixels)

Before Build:

  • Confirm original image dimensions meet design delivery standards
  • Confirm widths array matches the scenario
  • Confirm sizes attribute matches actual layout
  • Confirm formats order is correct (AVIF → WebP → JPEG/PNG)

After Build (Browser DevTools):

  • Network panel: Check loaded image filenames and dimensions
  • Console: Verify window.devicePixelRatio and img.currentSrc
  • Check if images are sharp (not blurry)
  • Check if file sizes are reasonable (not too large)

IssueCauseSolution
Blurry Imagessizes is too small, or widths is insufficientCheck if sizes matches actual layout; increase maxWidth
Bandwidth Wastesizes is too large, or too many widthsCheck if sizes is correct; reduce unnecessary breakpoints
Long Build TimeToo many widths array entries (>10)Use default parameters (8 breakpoints), avoid manually specifying too many widths
AVIF Not WorkingBrowser does not supportNormal, will automatically fallback to WebP/JPEG
Image DistortionWidth-height ratio error during exportCheck design file, re-export with correct aspect ratio

Document Version: v1.0 (2025-10-16) Based on: Device Matrix v2025.1, Astro Responsive Image Spec ZH Maintenance: Updated in sync with original specification documents