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

瀏覽器相容性

另見