font

Baseline 广泛可用 *

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

* 此特性的某些部分可能存在不同级别的支持。

font CSS 简写属性用于设置元素字体所有不同的属性。或者,它将元素的字体设置为系统字体。

试一试

font:
  1.2rem "Fira Sans",
  sans-serif;
font:
  italic 1.2rem "Fira Sans",
  serif;
font: italic small-caps bold 16px/2 cursive;
font: small-caps bold 24px/1 sans-serif;
font: caption;
<section id="default-example">
  <p 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 {
  font-family: "Fira Sans";
  src:
    local("FiraSans-Regular"),
    url("/shared-assets/fonts/FiraSans-Regular.woff2") format("woff2");
  font-weight: normal;
  font-style: normal;
}

@font-face {
  font-family: "Fira Sans";
  src:
    local("FiraSans-Italic"),
    url("/shared-assets/fonts/FiraSans-Italic.woff2") format("woff2");
  font-weight: normal;
  font-style: italic;
}

section {
  margin-top: 10px;
  font-size: 1.1em;
}

与任何简写属性一样,任何未指定的值都将设置为其对应的初始值(可能会覆盖以前使用非简写属性设置的值)。尽管 font 不能直接设置,但长式属性 font-size-adjustfont-kerning 也将重置为其初始值。

构成属性

此属性是以下 CSS 属性的简写:

语法

css
/* font-size font-family */
font: 1.2em "Fira Sans", sans-serif;

/* font-size/line height font-family */
font: 1.2em/2 "Fira Sans", sans-serif;

/* font-style font-weight font-size font-family */
font: italic bold 1.2em "Fira Sans", sans-serif;

/* font-stretch font-variant font-size font-family */
font: ultra-condensed small-caps 1.2em "Fira Sans", sans-serif;

/* system font */
font: caption;

font 属性可以指定为单个关键字(用于选择系统字体),也可以指定为各种字体相关属性的简写形式。

如果 font 指定为系统关键字,则必须是以下之一:captioniconmenumessage-boxsmall-captionstatus-bar

如果 font 被指定为多个字体相关属性的简写形式,则:

  • 它必须包含以下值:

  • 它可选择包含以下值:

  • font-stylefont-variantfont-weight 必须在 font-size 之前。

  • font-variant 只能指定 CSS 2.1 中定义的值,即 normalsmall-caps

  • font-stretch 只能是单个关键字值。

  • line-height 必须紧跟在 font-size 之后,前面带 "/",像这样:16px/3

  • font-family 必须是最后指定的值。

<'font-style'>

请参阅 font-style CSS 属性。

<'font-variant'>

请参阅 font-variant CSS 属性。

<'font-weight'>

请参阅 font-weight CSS 属性。

<'font-stretch'>

请参阅 font-stretch CSS 属性。

<'font-size'>

请参阅 font-size CSS 属性。

<'line-height'>

请参阅 line-height CSS 属性。

<'font-family'>

请参阅 font-family CSS 属性。

系统字体值

caption

用于带标题控件(例如按钮、下拉菜单等)的系统字体。

icon

用于标记图标的系统字体。

用于菜单(例如下拉菜单和菜单列表)的系统字体。

message-box

用于对话框的系统字体。

small-caption

用于标记小型控件的系统字体。

status-bar

用于窗口状态栏的系统字体。

带前缀的系统字体关键字

浏览器通常会实现更多带前缀的关键字:Gecko 实现了 -moz-window-moz-document-moz-desktop-moz-info-moz-dialog-moz-button-moz-pull-down-menu-moz-list-moz-field

正式定义

初始值作为简写中的每个属性
应用于所有元素和文本。它也适用于 ::first-letter::first-line
继承性
百分比作为简写中的每个属性
计算值作为简写中的每个属性
动画类型作为简写中的每个属性

正式语法

font = 
[ [ <'font-style'> || <font-variant-css2> || <'font-weight'> || <font-width-css3> ]? <'font-size'> [ / <'line-height'> ]? <'font-family'># ] |
<system-family-name>

<font-style> =
normal |
italic |
left |
right |
oblique <angle [-90deg,90deg]>?

<font-variant-css2> =
normal |
small-caps

<font-weight> =
<font-weight-absolute> |
bolder |
lighter

<font-width-css3> =
normal |
ultra-condensed |
extra-condensed |
condensed |
semi-condensed |
semi-expanded |
expanded |
extra-expanded |
ultra-expanded

<font-size> =
<absolute-size> |
<relative-size> |
<length-percentage [0,∞]> |
math

<line-height> =
normal |
<number [0,∞]> |
<length-percentage [0,∞]>

<font-family> =
[ <family-name> | <generic-family> ]#

<system-family-name> =
caption |
icon |
menu |
message-box |
small-caption |
status-bar

<font-weight-absolute> =
normal |
bold |
<number [1,1000]>

<length-percentage> =
<length> |
<percentage>

<family-name> =
<string> |
<custom-ident>+

<generic-family> =
<generic-script-specific> |
<generic-complete> |
<generic-incomplete>

<generic-script-specific> =
generic( fangsong ) |
generic( kai ) |
generic( khmer-mul ) |
generic( nastaliq )

<generic-complete> =
serif |
sans-serif |
system-ui |
cursive |
fantasy |
math |
monospace

<generic-incomplete> =
ui-serif |
ui-sans-serif |
ui-monospace |
ui-rounded

示例

设置字体属性

css
/* Set the font size to 12px and the line height to 14px.
   Set the font family to sans-serif */
p {
  font: 12px/14px sans-serif;
}

/* Set the font size to 80% of the parent element
   or default value (if no parent element present).
   Set the font family to sans-serif */
p {
  font: 80% sans-serif;
}

/* Set the font weight to bold,
   the font-style to italic,
   the font size to large,
   and the font family to serif. */
p {
  font: bold italic large serif;
}

/* Use the same font as the status bar of the window */
p {
  font: status-bar;
}

实时示例

规范

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

浏览器兼容性

另见