Using Performant Next-Gen Images in CSS with image-set()

Deliver optimal images to every user with browser-managed selection for formats, resolutions, and network conditions.

What CSS image-set() Does

The image-set() CSS functional notation allows browsers to automatically select the most appropriate image from a given set based on multiple factors:

  • Device pixel ratio to match screen resolution
  • Supported image formats available in the browser
  • Network conditions that may influence download speed preferences

Rather than requiring complex JavaScript detection, image-set() delegates selection to the browser, which knows the user's device characteristics including display density, supported MIME types, and current connection quality.

For background images specifically, image-set() provides capabilities that the HTML srcset attribute cannot offer. When working with CSS background-image properties, developers previously had limited options for responsive or format-aware image delivery. The image-set() function closes this gap, enabling sophisticated image selection directly within stylesheets without requiring markup changes or JavaScript interventions. This proves essential for hero sections, card components, and decorative elements where srcset cannot be applied.

Implementing modern CSS techniques like image-set() is a core part of our professional web development services, ensuring your websites leverage the latest standards for optimal performance.

Why Next-Gen Image Formats Matter

AVIF and WebP formats offer substantially smaller file sizes compared to traditional JPEG and PNG formats while maintaining equivalent or superior image quality:

  • AVIF: Derived from AV1 video codec, exceptional compression capabilities
  • WebP: Widespread browser support, excellent balance between compression and compatibility
  • Benefits: Faster page loads, reduced bandwidth, improved Core Web Vitals

AVIF, derived from the AV1 video codec, represents the newest generation of image formats with exceptional compression capabilities. WebP, developed by Google, has achieved widespread browser support and offers an excellent balance between compression efficiency and compatibility. Both formats enable developers to deliver visually rich experiences without the performance penalties traditionally associated with high-quality imagery. Savvy's implementation guide demonstrates how teams achieve 25-50% file size reductions compared to JPEG, making next-gen formats essential for performance-focused development.

CSS image-set() Syntax Examples
1/* Format-based selection */2.hero-background {3 background-image: image-set(4 url("image.avif") type("image/avif"),5 url("image.webp") type("image/webp"),6 url("image.jpg") type("image/jpeg")7 );8}9 10/* Resolution-based selection */11.retina-support {12 background-image: image-set(13 url("image-1x.webp") 1x,14 url("image-2x.webp") 2x,15 url("image-3x.webp") 3x16 );17}

Resolution Descriptors

The x notation indicates device pixel ratio requirements:

  • 1x: Standard resolution displays including most laptops, desktop monitors, and older mobile devices
  • 2x: High-density Retina displays found on modern MacBooks, iPhones, and premium Windows devices
  • 3x: Latest high-resolution mobile devices including flagship smartphones and professional displays

Each image must have a unique resolution descriptor to prevent ambiguous selection behavior. The browser evaluates the device pixel ratio and selects the matching option, ensuring crisp rendering without forcing all users to download unnecessarily large files. A user browsing on a standard 1x laptop will receive appropriately sized images, while an iPhone user with a 2x or 3x display will receive higher resolution variants that match their device capabilities. CSS-Tricks documents that this approach prevents over-downloading while maintaining visual quality across the device spectrum.

Format Descriptors with type()

The type() descriptor specifies MIME type for format-based selection. Browsers evaluate declarations in order, selecting the first format they can display:

  1. AVIF (most efficient, newest)
  2. WebP (widely supported, excellent compression)
  3. JPEG/PNG (universal fallback)

This ordering ensures modern browsers receive optimal images while older browsers gracefully fall back to supported alternatives. WebP enjoys nearly universal support in modern browsers, while AVIF support has expanded rapidly since its introduction. According to MDN Web Docs, this progressive enhancement approach allows developers to prioritize smaller, more efficient formats without leaving users with older browsers without a functional experience.

Combining Resolution and Format Descriptors
1/* Combined resolution and format selection */2.comprehensive-hero {3 background-image: image-set(4 url("hero-large.avif") type("image/avif") 2x,5 url("hero-large.webp") type("image/webp") 2x,6 url("hero.avif") type("image/avif") 1x,7 url("hero.webp") type("image/webp") 1x,8 url("hero.jpg") type("image/jpeg")9 );10 background-size: cover;11 background-position: center;12}

Implementation for Background Images

CSS background images require different optimization approaches than HTML img elements. The srcset attribute works exclusively with img tags, leaving CSS backgrounds without native responsive capabilities. Image-set() fills this gap for:

  • Hero sections with large background images
  • Card components and feature sections
  • Decorative elements and section dividers
  • Call-to-action backgrounds

For Next.js applications where Core Web Vitals directly influence search visibility, properly optimized background images significantly impact Largest Contentful Paint metrics. Our search engine optimization services emphasize technical performance factors like image optimization to improve search rankings and user experience. Hero sections featuring large background images that load quickly contribute to better LCP scores. Image-set() enables delivery of appropriately sized and formatted images based on the viewing device, ensuring fast initial rendering without compromising visual impact. Savvy's performance analysis shows that implementing format-aware image selection for background elements can reduce LCP times substantially compared to static image declarations.

Performance Impact of Next-Gen Images

50%

Average file size reduction with AVIF vs JPEG

30%

Bandwidth savings with WebP vs PNG

2x

Improvement in LCP with optimized images

Browser Support and Fallback Strategies

Image-set() achieved Baseline availability in September 2023, meaning full support across Chrome, Firefox, Safari, and Edge. However, fallback strategies ensure graceful degradation for older browser versions and edge cases:

Legacy browser handling: Browsers that don't recognize image-set() ignore it entirely and use the standard background-image declaration before it. This enables progressive enhancement where modern browsers receive optimized images while legacy browsers access a functional fallback.

WebKit prefix: Earlier Safari and Chrome versions required -webkit-image-set(). Including both ensures maximum compatibility across browser version variations, particularly for organizations with extended browser update cycles.

Fallback Strategy for Maximum Compatibility
1/* Maximum browser compatibility */2.compatible-background {3 background-image: url("fallback.jpg"); /* Legacy fallback */4 background-image: -webkit-image-set(5 url("image.webp") type("image/webp"),6 url("image.jpg") type("image/jpeg")7 );8 background-image: image-set(9 url("image.avif") type("image/avif"),10 url("image.webp") type("image/webp"),11 url("image.jpg") type("image/jpeg")12 );13}

Performance Optimization Benefits

Implementing image-set() delivers measurable improvements through multiple mechanisms:

Core Web Vitals Impact:

  • Largest Contentful Paint (LCP) improves with faster image loading as smaller, more efficient formats reach users sooner
  • Cumulative Layout Shift (CLS) prevented by properly sized images that load before layout calculation completes
  • Browser considers network conditions for adaptive behavior, potentially serving lower-resolution images on slower connections

Bandwidth Efficiency:

  • 25-50% file size reductions with AVIF/WebP vs JPEG translate directly to faster page loads
  • Resolution-aware selection prevents over-downloading on standard resolution displays
  • Cumulative savings across all images and page views reduce hosting costs while improving access speeds

As CSS-Tricks confirms, these optimizations compound across all images on a page, creating significant cumulative benefits for overall page performance and user experience.

Key Benefits of CSS image-set()

Format Flexibility

Serve AVIF, WebP, and traditional formats with automatic browser selection based on support.

Resolution Adaptation

Deliver 1x, 2x, or 3x images matching device pixel ratio without JavaScript detection.

Network Awareness

Browsers consider connection quality when selecting images, improving experience on slow networks.

Background Coverage

Enables format and resolution selection for CSS backgrounds where srcset cannot be used.

Best Practices and Recommendations

Image Preparation Workflow:

  • Establish consistent naming conventions and directory structures for image variants
  • Use automated build processes with tools like sharp or ImageMagick to generate AVIF, WebP, and resolution variants from source images
  • Integrate image optimization into CI/CD pipelines to ensure all deployments include properly prepared assets

Testing Strategies:

  • Use browser developer tools to inspect network requests and verify which images load in different contexts
  • Test with network throttling enabled to verify adaptive behavior on slower connections
  • Validate across target browsers and device pixel ratios using both manual testing and automated tools

Tools like squoosh.app and build plugins for webpack or Vite simplify the image preparation workflow, enabling teams to generate all required variants efficiently. Combining these tools with consistent naming conventions ensures maintainable implementations that scale across large projects.

For comprehensive guidance on optimizing your website's performance, explore our guide on tools for auditing CSS to identify additional optimization opportunities.

Frequently Asked Questions

Ready to Optimize Your Website Images?

Our web development team specializes in performance optimization, implementing modern techniques like CSS image-set() to deliver exceptional user experiences.

Sources

  1. CSS-Tricks - image-set() - Comprehensive CSS function reference with syntax, examples, and browser support details
  2. MDN Web Docs - image-set() - Authoritative documentation on CSS image-set functional notation
  3. Savvy - CSS image-set for Better Images and Fallback Support - Practical implementation guide with AVIF/WebP focus