The image-set function gives the browser multiple CSS image candidates with resolution descriptors. It is useful for decorative backgrounds on devices with different pixel densities. It does not provide alternative text or turn a CSS background into a content image.
CSS image-set: offer resolution choices for decoration
When to use it
A case-map card has a purely decorative contour behind its readable heading. Supply an ordinary 1x URL first, then a candidate set with 1x and 2x files for browsers that support image-set. Keep a solid background color beneath the image so failed requests leave a usable card. The two paths in the sample are deployment assets, not files bundled by this content draft. Confirm their real dimensions, MIME types, cache headers, and response codes before reusing the rule. Test both display densities, offline loading, slow networks, reduced data modes where available, and print. Do not put evidence, a route, or a status label only in the background image; users who disable backgrounds, print in grayscale, or use assistive technology must still receive the information from HTML. A high-density screen does not guarantee the browser chooses the 2x file every time.
.case-map { background-color: #e8f1f4; background-image: url('/assets/case-map-1x.webp'); background-size: cover; }
@supports (background-image: image-set(url('/assets/case-map-1x.webp') 1x)) { .case-map { background-image: image-set(url('/assets/case-map-1x.webp') 1x, url('/assets/case-map-2x.webp') 2x); } }Cost and verification
Resolution choice affects transfer size, decoding, and memory. Measure actual bytes and rendering on a slow device instead of assuming a 2x file is always worthwhile. image-set selects among candidates, but an individual failed request is not a dependable signal to retry the previous CSS declaration; the solid color is the resilient visual fallback. The background should be compressed and appropriately sized for its displayed area. If an image carries meaning, use HTML image markup with an appropriate text alternative and responsive source selection rather than hiding it in CSS. The card's title and actions must retain contrast over the chosen asset as well as over the plain color.
Common Mistakes
- Do not use a CSS background for essential evidence or labels.
- Do not assume the browser will always choose the largest candidate.
- Do not ship illustrative asset paths without creating and checking the files.
