font-stretch

已弃用:此特性不再推荐。虽然某些浏览器可能仍然支持它,但它可能已经从相关的网络标准中删除,可能正在删除过程中,或者可能仅为兼容性目的而保留。请避免使用它,如果可能,请更新现有代码;请参阅本页底部的兼容性表格以指导您的决策。请注意,此特性可能随时停止工作。

注意:font-stretch 属性现在已在规范中更名为 font-widthfont-stretch 名称已被保留作为 font-width 属性的别名。新名称 font-width 尚未被任何浏览器支持。

font-stretch CSS 属性从字体中选择正常、凝缩或扩展的字形。

试一试

font-stretch: condensed;
font-stretch: expanded;
font-stretch: ultra-expanded;
font-stretch: 50%;
font-stretch: 100%;
font-stretch: 150%;
<section class="default-example" id="default-example">
  <p class="transition-all" id="example-element">
    London. Michaelmas term lately over, and the Lord Chancellor sitting in
    Lincoln's Inn Hall. Implacable November weather. As much mud in the streets
    as if the waters had but newly retired from the face of the earth, and it
    would not be wonderful to meet a Megalosaurus, forty feet long or so,
    waddling like an elephantine lizard up Holborn Hill.
  </p>
</section>
@font-face {
  src: url("/shared-assets/fonts/LeagueMono-VF.ttf") format("truetype");
  font-family: "League";
  font-style: normal;
  font-weight: normal;
  font-stretch: 50% 200%; /* Required by Chrome - allow 50% to 200% */
}

section {
  font-size: 1.2em;
  font-family: "League", sans-serif;
}

语法

css
/* <font-stretch-css3> keyword values */
font-stretch: normal;
font-stretch: ultra-condensed;
font-stretch: extra-condensed;
font-stretch: condensed;
font-stretch: semi-condensed;
font-stretch: semi-expanded;
font-stretch: expanded;
font-stretch: extra-expanded;
font-stretch: ultra-expanded;

/* Percentage values */
font-stretch: 50%;
font-stretch: 100%;
font-stretch: 200%;

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

此属性可以指定为单个 <font-stretch-css3> 关键字值或单个<percentage> 值。

normal

指定正常字体字形。

semi-condensedcondensedextra-condensedultra-condensed

指定比正常字形更凝缩的字体字形,其中 ultra-condensed 是最凝缩的。

semi-expandedexpandedextra-expandedultra-expanded

指定比正常字形更扩展的字体字形,其中 ultra-expanded 是最扩展的。

<percentage>

一个介于 50% 和 200%(含)之间的<percentage> 值。此属性不允许使用负值。

关键字到数字的映射

下表显示了 <font-stretch-css3> 关键字值和数字百分比之间的映射

关键字 百分比
ultra-condensed 50%
extra-condensed 62.5%
condensed 75%
semi-condensed 87.5%
normal 100%
semi-expanded 112.5%
expanded 125%
extra-expanded 150%
ultra-expanded 200%

描述

一些字体家族提供额外的字形,其中字符比正常字形更窄(凝缩字形)或更宽(扩展字形)。

您可以使用 font-stretch 从此类字体中选择凝缩或扩展的字形。如果您使用的字体不提供凝缩或扩展的字形,则此属性无效。

字体字形选择

为给定 font-stretch 值选择的字形取决于所选字体支持的字形。如果字体未提供与给定值完全匹配的字形,则小于 100% 的值映射到较窄的字形,大于或等于 100% 的值映射到较宽的字形。

下表演示了在两种不同字体上提供各种不同百分比的 font-stretch 值时的效果

  • Anek Malayalam 是一种可变 Google 字体,支持 75% 到 125% 的宽度。此范围之外的值会选择最接近的匹配字体。
  • Inconsolata 是一种可变字体,提供 50% 到 200% 的连续宽度范围。

正式定义

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

正式语法

font-width = 
normal |
<percentage [0,∞]> |
ultra-condensed |
extra-condensed |
condensed |
semi-condensed |
semi-expanded |
expanded |
extra-expanded |
ultra-expanded

示例

设置字体拉伸百分比

html
<p class="condensed">an elephantine lizard</p>
<p class="normal">an elephantine lizard</p>
<p class="expanded">an elephantine lizard</p>
css
@font-face {
  src: url("https://mdn.github.io/shared-assets/fonts/LeagueMono-VF.ttf");
  font-family: "LeagueMonoVariable";
  font-style: normal;
  font-stretch: 1% 500%; /* Required by Chrome */
}

p {
  font:
    1.5rem "LeagueMonoVariable",
    sans-serif;
}

.condensed {
  font-stretch: 50%;
}

.normal {
  font-stretch: 100%;
}

.expanded {
  font-stretch: 200%;
}

规范

规范
CSS 字体模块第 4 级
# font-stretch-prop

浏览器兼容性

另见