<image> CSS data type represents a two-dimensional image.

句法

<image> data type can be represented with any of the following:

  • An image denoted by the <url> data type
  • A <gradient> data type
  • A part of the webpage, defined by the element() function
  • An image, image fragment or solid patch of color, defined by the image() function
  • A blending of two or more images defined by the cross-fade() 函数。
  • A selection of images chosen based on resolution defined by the image-set() 函数。

描述

CSS can handle the following kinds of images:

  • Images with intrinsic dimensions (a natural size), like a JPEG, PNG, or other raster format .
  • Images with multiple intrinsic dimensions , existing in multiple versions inside a single file, like some .ico formats. (In this case, the intrinsic dimensions will be those of the image largest in area and the aspect ratio most similar to the containing box.)
  • Images with no intrinsic dimensions but with an intrinsic aspect ratio between its width and height, like an SVG or other vector format .
  • Images with neither intrinsic dimensions, nor an intrinsic aspect ratio , like a CSS gradient.

CSS determines an object's concrete size using (1) its intrinsic dimensions ; (2) its specified size , defined by CSS properties like width , height ,或 background-size ; and (3) its default size , determined by the kind of property the image is used with:

Kind of Object (CSS Property) Default object size
background-image The size of the element's background positioning area
list-style-image The size of a 1em character
border-image-source The size of the element's border image area
cursor The browser-defined size matching the usual cursor size on the client's system
mask-image ?
shape-outside ?
mask-border-source ?
symbols for @counter-style At risk feature. If supported, the browser-defined size matching the usual cursor size on the client's system
content for a pseudo-element ( ::after / ::before ) A 300px × 150px rectangle

The concrete object size is calculated using the following algorithm:

  • If the specified size defines both the width and the height , these values are used as the concrete object size.
  • If the specified size defines only the width or only the height , the missing value is determined using the intrinsic ratio, if there is any, the intrinsic dimensions if the specified value matches, or the default object size for that missing value.
  • If the specified size defines neither the width nor the height , the concrete object size is calculated so that it matches the intrinsic aspect ratio of the image but without exceeding the default object size in any dimension. If the image has no intrinsic aspect ratio, the intrinsic aspect ratio of the object it applies to is used; if this object has none, the missing width or height are taken from the default object size.
注意: Not all browsers support every type of image on every property. See the browser compatibility section 了解细节。

可访问性关注

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.

范例

Valid images

url(test.jpg)               /* A <url>, as long as test.jpg is an actual image */
linear-gradient(blue, red)  /* A <gradient> */
element(#realid)            /* A part of the webpage, referenced with the element() function,
                               if "realid" is an existing ID on the page */
image(ltr 'arrow.png#xywh=0,0,16,16', red)
                            /* A section 16x16 section of <url>, starting from the top, left of the original
                               image as long as arrow.png is a supported image, otherwise a solid
                               red swatch. If language is rtl, the image will be horizontally flipped. */
cross-fade(20% url(twenty.png), url(eighty.png))
                            /* cross faded images, with twenty being 20% opaque
                               and eighty being 80% opaque. */
image-set('test.jpg' 1x, 'test-2x.jpg' 2x)
                            /* a selection of images with varying resolutions */
					

Invalid images

nourl.jpg            /* An image file must be defined using the url() function. */
url(report.pdf)      /* A file pointed to by the url() function must be an image. */
element(#fakeid)     /* An element ID must be an existing ID on the page. */
image(z.jpg#xy=0,0)  /* The spatial fragment must be written in the format of xywh=#,#,#,# */
image-set('cat.jpg' 1x, 'dog.jpg' 1x) /* every image in an image set must have a different resolutions */
					

规范

规范 状态 注释
CSS Images Module Level 4
The definition of '<image>' in that specification.
工作草案 添加 element() , image() , image-set() , conic-gradient() , repeating-conic-gradient() ,和 image-resolution .
CSS Images Module Level 3
The definition of '<image>' in that specification.
候选推荐 Initial definition. Before this, there was no explicitly defined <image> data type. Images could only be defined using the url() functional notation.

浏览器兼容性

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> Chrome 完整支持 1 Edge 完整支持 12 Firefox 完整支持 1 IE 完整支持 10 Opera 完整支持 2 Safari 完整支持 1 WebView Android 完整支持 1 Chrome Android 完整支持 18 Firefox Android 完整支持 4 Opera Android 完整支持 11 Safari iOS 完整支持 1 Samsung Internet Android 完整支持 1.0
cross-fade() Chrome 完整支持 17 Prefixed 注意事项
完整支持 17 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Supports the original dual-image with percentage implementation only.
Edge 完整支持 79 Prefixed 注意事项
完整支持 79 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Supports the original dual-image with percentage implementation only.
Firefox 不支持 No IE 不支持 No Opera 完整支持 15 Prefixed 注意事项
完整支持 15 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Supports the original dual-image with percentage implementation only.
Safari 完整支持 10 注意事项
完整支持 10 注意事项
注意事项 Supports the original dual-image with percentage implementation only.
完整支持 5.1 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Supports the original dual-image with percentage implementation only.
WebView Android 完整支持 ≤37 Prefixed 注意事项
完整支持 ≤37 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Supports the original dual-image with percentage implementation only.
Chrome Android 完整支持 18 Prefixed 注意事项
完整支持 18 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Supports the original dual-image with percentage implementation only.
Firefox Android 不支持 No Opera Android 完整支持 14 Prefixed 注意事项
完整支持 14 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Supports the original dual-image with percentage implementation only.
Safari iOS 完整支持 9.3 注意事项
完整支持 9.3 注意事项
注意事项 Support for the original dual-image with percentage implementation only.
完整支持 5 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Supports the original dual-image with percentage implementation only.
Samsung Internet Android 完整支持 1.0 Prefixed 注意事项
完整支持 1.0 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Support for the original dual-image with percentage implementation only.
element() Experimental Chrome 不支持 No Edge 不支持 No Firefox 完整支持 57 Prefixed
完整支持 57 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
不支持 29 — 57 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -moz-
注意事项 -moz-element() is limited to background-image , background , border-image and border-image-source .
不支持 4 — 29 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -moz-
注意事项 -moz-element() is limited to background-image and background .
IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 完整支持 60 Prefixed
完整支持 60 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
不支持 29 — 60 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -moz-
注意事项 -moz-element() is limited to background-image , background , border-image and border-image-source .
不支持 4 — 29 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -moz-
注意事项 -moz-element() is limited to background-image and background .
Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No
<gradient> Chrome 完整支持 26 Edge 完整支持 12 Prefixed
完整支持 12 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Firefox 完整支持 3.6 注意事项
完整支持 3.6 注意事项
注意事项 Gradients are limited to background-image , border-image ,和 mask-image .
IE 完整支持 10 Prefixed
完整支持 10 Prefixed
Prefixed Implemented with the vendor prefix: -ms-
Opera 完整支持 12.1
完整支持 12.1
不支持 11 — 15 Prefixed
Prefixed Implemented with the vendor prefix: -o-
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Safari 完整支持 6.1 WebView Android 完整支持 ≤37 Chrome Android 完整支持 26 Firefox Android 完整支持 4 注意事项
完整支持 4 注意事项
注意事项 Gradients are limited to background-image , border-image ,和 mask-image .
Opera Android 完整支持 12.1
完整支持 12.1
不支持 11 — 14 Prefixed
Prefixed Implemented with the vendor prefix: -o-
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Safari iOS 完整支持 7 Samsung Internet Android 完整支持 1.5
image() Chrome 不支持 No Edge 不支持 No Firefox 不支持 No 注意事项
不支持 No 注意事项
注意事项 -moz-image-rect() function supports fragments as of Firefox 4.
IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 不支持 No 注意事项
不支持 No 注意事项
注意事项 -moz-image-rect() function supports fragments as of Firefox 4.
Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No
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-
paint() Chrome 完整支持 65 Edge 完整支持 79 Firefox 不支持 No IE 不支持 No Opera 完整支持 52 Safari 不支持 No WebView Android 完整支持 65 Chrome Android 完整支持 65 Firefox Android 不支持 No Opera Android 完整支持 47 Safari iOS 不支持 No Samsung Internet Android 完整支持 9.2

图例

完整支持

完整支持

部分支持

部分支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

见实现注意事项。

见实现注意事项。

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

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

另请参阅

元数据

  • 最后修改: