image-rendering - CSS: Cascading Style Sheets 编辑

The image-rendering CSS property sets an image scaling algorithm. The property applies to an element itself, to any images set in its other properties, and to its descendants.

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). For example, if the natural size of an image is 100×100px, but its actual dimensions are 200×200px (or 50×50px), then the image will be upscaled (or downscaled) using the algorithm specified by image-rendering. This property has no effect on non-scaled images.

Syntax

/* Keyword values */
image-rendering: auto;
image-rendering: crisp-edges;
image-rendering: pixelated;

/* Global values */
image-rendering: inherit;
image-rendering: initial;
image-rendering: unset;

Values

auto
The scaling algorithm is UA dependent. Since version 1.9 (Firefox 3.0), Gecko uses bilinear resampling (high quality).
smooth This is an experimental API that should not be used in production code.
The image should be scaled with an algorithm that maximizes the appearance of the image. In particular, scaling algorithms that "smooth" colors are acceptable, such as bilinear interpolation. This is intended for images such as photos.
high-quality This is an experimental API that should not be used in production code.
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
The image must be scaled with an algorithm that preserves contrast and edges in the image, and which does not smooth colors or introduce blur to the image in the process. Suitable algorithms include nearest-neighbor and other non-smoothing scaling algorithms such as 2×SaI and hqx-family algorithms. This value is intended for pixel-art images, such as in browser games.
pixelated
When scaling the image up, the nearest-neighbor algorithm must be used, so that the image appears to be composed of large pixels. When scaling down, this is the same as auto.

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

Formal definition

Initial valueauto
Applies toall elements
Inheritedyes
Computed valueas specified
Animation typediscrete

Formal syntax

auto | crisp-edges | pixelated

Examples

Setting image scaling algorithms

In practical use, the pixelated and crisp-edges rules can be combined to provide some fallback for each other. (Just prepend the actual rules with the fallback.) The Canvas API can provide a fallback solution for pixelated through manual image data manipulation or with imageSmoothingEnabled.

<div>
  <img class="auto" alt="auto" src="https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/blumen.jpg" />
  <img class="pixelated" alt="pixelated" src="https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/blumen.jpg" />
  <img class="crisp-edges" alt="crisp-edges" src="https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/blumen.jpg" />
</div>
img {
  height: 200px;
}

CSS

.auto {
  image-rendering: auto;
}

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

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

Result

Specifications

SpecificationStatusComment
CSS Images Module Level 3
The definition of 'image-rendering' in that specification.
Candidate RecommendationInitial definition.

Browser compatibility

BCD tables only load in the browser

Note: Although crisp-edges is supposed to use a pixel-art scaler like in the specification example, in practice no browsers (as of January 2020) does so. In Firefox, crisp-edges is interpreted as nearest-neighbor, pixelated is not supported, and auto is interpolated as trilinear or linear.

For behavior on Chromium and Safari (WebKit), see the GetInterpolationQuality function and CSSPrimitiveValue::operator ImageRendering() respectively.

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:95 次

字数:9660

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文