font-optical-sizing

Baseline 已广泛支持

此功能已经成熟,并可在许多设备和浏览器版本上使用。自 ⁨2020 年 3 月⁩起,它已在各浏览器中推出。

font-optical-sizing CSS 属性设置是否针对在不同大小下查看文本进行优化。

试一试

font-optical-sizing: auto;
font-optical-sizing: none;
<section id="default-example">
  <div id="example-element">
    <h2>Chapter 3</h2>
    <p>
      On this particular Thursday, something was moving quietly through the
      ionosphere many miles above the surface of the planet; several somethings
      in fact, several dozen huge yellow chunky slablike somethings, huge as
      office blocks, silent as birds.
    </p>
  </div>
</section>
@font-face {
  src: url("/shared-assets/fonts/variable-fonts/AmstelvarAlpha-VF.ttf");
  font-family: "Amstelvar";
  font-style: normal;
}

#example-element {
  font-family: "Amstelvar", serif;
  text-align: left;
}

#example-element h2 {
  font-size: 36px;
}

#example-element p {
  font-size: 12px;
}

语法

css
/* keyword values */
font-optical-sizing: none;
font-optical-sizing: auto; /* default */

/* Global values */
font-optical-sizing: inherit;
font-optical-sizing: initial;
font-optical-sizing: revert;
font-optical-sizing: revert-layer;
font-optical-sizing: unset;

none

浏览器不会修改字形的形状以实现最佳显示。

auto

浏览器将修改字形的形状以实现最佳显示。

描述

对于具有光学尺寸可变轴的字体,默认启用光学尺寸调整。光学尺寸可变轴在 font-variation-settings 中以 opsz 表示。

使用光学尺寸调整时,小文本通常会以更粗的笔画和更大的衬线呈现,而大文本通常会呈现得更精致,粗细笔画之间的对比度更高。

正式定义

初始值auto
应用于所有元素和文本。它也适用于 ::first-letter::first-line
继承性
计算值同指定值
动画类型离散

正式语法

font-optical-sizing = 
auto |
none

示例

禁用光学尺寸调整

html
<p class="optical-sizing">
  This paragraph is optically sized. This is the default across browsers.
</p>

<p class="no-optical-sizing">
  This paragraph is not optically sized. You should see a difference in
  supporting browsers.
</p>
css
@font-face {
  src: url("AmstelvarAlpha-VF.ttf");
  font-family: "Amstelvar";
  font-style: normal;
}

p {
  font-size: 36px;
  font-family: "Amstelvar", serif;
}

.no-optical-sizing {
  font-optical-sizing: none;
}

注意: 上述引用的字体(包含光学尺寸调整功能并免费许可)非常适合测试。您可以在 GitHub 上下载它

规范

规范
CSS 字体模块第 4 级
# font-optical-sizing-def

浏览器兼容性

另见