image-set() CSS function notation is a method of letting the browser pick the most appropriate CSS image from a given set, primarily for high pixel density screens.

Resolution and bandwidth differ by device and network access. The image-set() function delivers the most appropriate image resolution for a user’s device, providing a set of image options — each with an associated resolution declaration — from which the browser picks the most appropriate for the device and settings. Resolution can be used as a proxy for filesize — a user agent on a slow mobile connection with a high-resolution screen may prefer to receive lower-resolution images rather than waiting for a higher resolution image to load.

image-set() allows the author to provide options rather than determining what each individual user needs.

句法

image-set() = image-set( <image-set-option># )
where <image-set-option> = [ <image> | <string> ] <resolution> and
      <string> is an <url>
					

Most commonly you'll see an url() <string> value, but the <image> can be any image type except for an image set. The image-set() function can not be nested inside another image-set() 函数。

<resolution> units include x or dppx , for dots per pixel unit, dpi , for dots per inch, and dpcm for dots per centimeter. Every image within an image-set() must have a unique resolution.

范例

Using image-set() to provide alternative background-image options

background-image: image-set( "cat.png" 1x,
                             "cat-2x.png" 2x,
                             "cat-print.png" 600dpi);
					

This example shows how to use image-set() to provide two alternative background-image options, chosen depending on the resolution needed: a normal version and a high-resolution version.

可访问性关注

Browsers do not provide any special information on background images to assistive technology. This is important primarily for screen readers, as a screen reader will not announce its presence and therefore convey nothing to its users. If the image contains information critical to understanding the page's overall purpose, it is better to describe it semantically in the document.

规范

规范 状态 注释
CSS Images Module Level 4
The definition of 'The image-set() notation' in that specification.
工作草案

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
image-set() Chrome 完整支持 21 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Edge 完整支持 79 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Firefox 不支持 No 注意事项
不支持 No 注意事项
注意事项 bug 1107646 .
IE 不支持 No Opera 完整支持 15 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Safari 部分支持 6 Prefixed 注意事项
部分支持 6 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 支持 url images only and x is the only supported resolution unit. See bug 160934 .
WebView Android 完整支持 4.4 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Chrome Android 完整支持 25 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Firefox Android 不支持 No 注意事项
不支持 No 注意事项
注意事项 bug 1107646 .
Opera Android 完整支持 14 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Safari iOS 部分支持 6 Prefixed 注意事项
部分支持 6 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 支持 url images only and x is the only supported resolution unit. See bug 160934 .
Samsung Internet Android 完整支持 1.5 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-

图例

完整支持

完整支持

部分支持

部分支持

不支持

不支持

见实现注意事项。

见实现注意事项。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改:
  1. CSS
  2. CSS 参考

Copyright  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1