image-rendering

CSS 属性 image-rendering 用于设置图像缩放算法。它适用于元素本身,适用于元素其他属性中的图像,也应用于子元素。

The user agent will scale an image when the page author specifies dimensions other than its natural size. Scaling may also occur due to user interaction (zooming). 举个例子,如果有一张尺寸为 100×100px 的图片,但作者有意将尺寸设置为 200×200px(或50×50px),然后,图片便会根据 image-rendering 指定的算法,缩小或放大到新尺寸。此属性对于未缩放的图像没有影响。

Note: The Canvas API can provide a fallback solution for crisp-edges through manual image data manipulation.

语法

/* 专有属性值 */
image-rendering: auto;
image-rendering: crisp-edges;
image-rendering: pixelated;

/* 全局属性值 */
image-rendering: inherit;
image-rendering: initial;
image-rendering: unset;

属性值

auto
自 Gecko 1.9 (Firefox 3.0)起,Gecko 使用双线性bilinear)算法进行重新采样(高质量)。
smooth
应使用能最大化图像客观观感的算法来缩放图像。特别地,会“平滑”颜色的缩放算法是可以接受的,例如双线性插值。这适用于照片等类型的图像。
high-quality
Identical to smooth, but with a preference for higher-quality scaling. If system resources are constrained, images with high-quality should be prioritized over those with any other value, when considering which images to degrade the quality of and to what degree.
crisp-edges
必须使用可有效保留对比度和图像中的边缘的算法来对图像进行缩放,并且,该算法既不会平滑颜色,又不会在处理过程中为图像引入模糊。合适的算法包括最近邻居nearest-neighbor)算法和其他非平滑缩放算法,比如 2×SaIhqx-* 系列算法。此属性值适用于像素艺术作品,例如一些网页游戏中的图像。
pixelated
放大图像时, 使用最近邻居算法,因此,图像看着像是由大块像素组成的。缩小图像时,算法与 auto 相同。

注意:The values optimizeQuality and optimizeSpeed present in an early draft (and coming from its SVG counterpart) are defined as synonyms for the smooth and pixelated values respectively.

形式化语法

auto | crisp-edges | pixelated

例子

.auto {
  image-rendering: auto;
}

.pixelated {
  -ms-interpolation-mode: nearest-neighbor;
  image-rendering: pixelated;
}

.crisp-edges {
  image-rendering: -webkit-optimize-contrast;
  image-rendering: crisp-edges;
}

规范

规范 状态 备注
CSS Images Module Level 3
image-rendering
Candidate Recommendation Initial definition.
初始值auto
适用元素all elements
是否是继承属性yes
适用媒体visual
计算值as specified
Animation typediscrete
正规顺序the unique non-ambiguous order defined by the formal grammar

注意:虽然在最初此属性与 SVG 的 image-rendering 属性定义相近, 但到现在,它们之间已有相当的差别。

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
image-rendering
Experimental
Chrome Full support 13Edge No support NoFirefox Full support 3.6IE No support NoOpera Full support 15Safari Full support 6WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 14Safari iOS Full support 6Samsung Internet Android Full support 1.0
crisp-edges
Experimental
Chrome Full support 13
Alternate Name
Full support 13
Alternate Name
Alternate Name Uses the non-standard name: -webkit-optimize-contrast
Edge No support NoFirefox Full support 65
Full support 65
Full support 3.6
Prefixed
Prefixed Implemented with the vendor prefix: -moz-
IE No support NoOpera Full support 15
Alternate Name
Full support 15
Alternate Name
Alternate Name Uses the non-standard name: -webkit-optimize-contrast
No support ? — 15
Prefixed
Prefixed Implemented with the vendor prefix: -o-
Safari Full support 6
Alternate Name
Full support 6
Alternate Name
Alternate Name Uses the non-standard name: -webkit-optimize-contrast
WebView Android Full support ≤37
Alternate Name
Full support ≤37
Alternate Name
Alternate Name Uses the non-standard name: -webkit-optimize-contrast
Chrome Android Full support 18
Alternate Name
Full support 18
Alternate Name
Alternate Name Uses the non-standard name: -webkit-optimize-contrast
Firefox Android Full support 65
Full support 65
Full support 4
Prefixed
Prefixed Implemented with the vendor prefix: -moz-
Opera Android Full support 14
Alternate Name
Full support 14
Alternate Name
Alternate Name Uses the non-standard name: -webkit-optimize-contrast
No support ? — 14
Prefixed
Prefixed Implemented with the vendor prefix: -o-
Safari iOS Full support 6
Alternate Name
Full support 6
Alternate Name
Alternate Name Uses the non-standard name: -webkit-optimize-contrast
Samsung Internet Android Full support 1.0
Alternate Name
Full support 1.0
Alternate Name
Alternate Name Uses the non-standard name: -webkit-optimize-contrast
optimizeQuality
DeprecatedNon-standard
Chrome Full support 28Edge No support NoFirefox Full support 3.6IE No support NoOpera Full support 15Safari Full support 6.1WebView Android Full support ≤37Chrome Android Full support 28Firefox Android Full support 4Opera Android Full support 14Safari iOS Full support 7Samsung Internet Android Full support 1.5
optimizeSpeed
DeprecatedNon-standard
Chrome Full support 28Edge No support NoFirefox Full support 3.6IE No support NoOpera Full support 15Safari Full support 6.1WebView Android Full support ≤37Chrome Android Full support 28Firefox Android Full support 4Opera Android Full support 14Safari iOS Full support 7Samsung Internet Android Full support 1.5
pixelated
Experimental
Chrome Full support 41Edge No support NoFirefox No support NoIE No support NoOpera Full support 26Safari Full support 10WebView Android Full support 41Chrome Android Full support 41Firefox Android No support NoOpera Android Full support 26Safari iOS Full support 10Samsung Internet Android Full support 4.0

Legend

Full support  
Full support
No support  
No support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
Uses a non-standard name.
Uses a non-standard name.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

备注

译者注:这是来自旧版浏览器兼容性表格的备注,保留以便查阅。可另行参阅 Can I use

Internet Explorer 7 和 8 支持非标准的 -ms-interpolation-mode 属性,有两个属性值:bicubic 和 nearest-neighbor,和大量差距:

  • 只应用于 <img> 等元素带的图片
  • IE 7 上此属性只支持无透明度的图片
  • 不能继承
  • IE 7 默认值: nearest-neighbor (低质量)
  • IE 8 默认值: bicubic (高质量)
  • IE 9 不支持此非标准属性

WebKit 支持一个非标准属性: -webkit-optimize-contrast.

WebKit Nightly 支持,见 bug

Canvas 可以通过人工方式支持 crisp-edge/optimize-contrast 属性.