image-rendering

Baseline 廣泛可用 *

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2020 年 1 月⁩ 起,所有主流瀏覽器均已支援。

* 此特性的某些部分可能存在不同級別的支援。

image-rendering CSS 屬性設定影像縮放演算法。該屬性適用於元素本身、在其其他屬性中設定的任何影像及其後代元素。

試一試

image-rendering: auto;
image-rendering: smooth;
image-rendering: crisp-edges;
image-rendering: pixelated;
<section id="default-example">
  <img
    class="transition-all"
    id="example-element"
    src="/shared-assets/images/examples/lizard.png" />
</section>
#example-element {
  height: 480px;
  object-fit: cover;
}

使用者代理會在頁面作者指定尺寸與影像自然尺寸不同時縮放影像。縮放也可能由於使用者互動(縮放)而發生。例如,如果影像的自然尺寸為 100×100px,但其實際尺寸為 200×200px(或 50×50px),那麼影像將使用 image-rendering 指定的演算法進行放大(或縮小)。此屬性對未縮放的影像沒有影響。

語法

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

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

auto

縮放演算法取決於使用者代理。自版本 1.9(Firefox 3.0)以來,Gecko 使用雙線性重取樣(高質量)。

平滑

影像應使用能最大化影像外觀的演算法進行縮放。特別是,能夠“平滑”顏色的縮放演算法是可接受的,例如雙線性插值。這適用於照片等影像。

crisp-edges

影像使用“最近鄰”等演算法進行縮放,以保留影像中的對比度和邊緣。通常用於畫素藝術或線條圖等影像,不會出現模糊或顏色平滑。

pixelated

影像使用“最近鄰”或類似演算法縮放到原始影像大小的最近整數倍,然後使用平滑插值將影像調整到最終所需大小。這旨在保留“畫素化”外觀,同時避免在放大解析度不是原始解析度的整數倍時引入縮放偽影。

注意:早期草案中存在的 optimizeQualityoptimizeSpeed 值(以及來自其 SVG 對應屬性 image-rendering)分別定義為 smoothpixelated 值的同義詞。

注意:CSS 影像模組為 image-rendering 屬性定義了一個 high-quality 值,以提供對更高質量縮放的偏好,但是,任何瀏覽器都不支援此值。

正式定義

初始值auto
應用於所有元素
繼承性
計算值同指定值
動畫型別離散

正式語法

image-rendering = 
auto |
smooth |
high-quality |
pixelated |
crisp-edges

示例

設定影像縮放演算法

在此示例中,影像重複三次,每次應用不同的 image-rendering 值。

CSS

css
.auto {
  image-rendering: auto;
}

.smooth {
  image-rendering: smooth;
}

.pixelated {
  image-rendering: pixelated;
}

.crisp-edges {
  image-rendering: crisp-edges;
}

結果

規範

規範
CSS 影像模組第三級
# the-image-rendering
Scalable Vector Graphics (SVG) 2
# ImageRendering

瀏覽器相容性

另見