font

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Thuộc tính font CSS là một thuộc tính viết tắt dùng để đặt tất cả các thuộc tính font khác nhau của một phần tử. Ngoài ra, nó cũng có thể đặt font của phần tử thành một font hệ thống.

Try it

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;
}

Cũng như bất kỳ thuộc tính viết tắt nào, những giá trị riêng lẻ không được chỉ định sẽ được đặt về giá trị khởi tạo tương ứng (có thể ghi đè các giá trị đã được đặt trước đó bằng thuộc tính dạng đầy đủ). Dù không thể đặt trực tiếp bằng font, các thuộc tính dài font-size-adjustfont-kerning cũng được đặt lại về giá trị khởi tạo.

Các thuộc tính thành phần

Thuộc tính này là viết tắt của các thuộc tính CSS sau:

Cú pháp

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;

Thuộc tính font có thể được chỉ định dưới dạng một từ khóa đơn (để chọn font hệ thống), hoặc dưới dạng viết tắt cho nhiều thuộc tính liên quan đến font.

Nếu font được chỉ định là một từ khóa hệ thống, nó phải là một trong: caption, icon, menu, message-box, small-caption, status-bar.

Nếu font được chỉ định là viết tắt của một số thuộc tính liên quan đến font, thì:

  • nó phải bao gồm các giá trị cho:

  • nó có thể tùy chọn bao gồm các giá trị cho:

  • font-style, font-variantfont-weight phải đứng trước font-size.

  • font-variant chỉ có thể chỉ định các giá trị được định nghĩa trong CSS 2.1, tức là normalsmall-caps.

  • font-stretch chỉ có thể là một giá trị từ khóa đơn.

  • line-height phải đứng ngay sau font-size, ngăn cách bằng "/", ví dụ: 16px/3.

  • font-family phải là giá trị được chỉ định cuối cùng.

Các giá trị

<'font-style'>

Xem thuộc tính CSS font-style.

<'font-variant'>

Xem thuộc tính CSS font-variant.

<'font-weight'>

Xem thuộc tính CSS font-weight.

<'font-stretch'>

Xem thuộc tính CSS font-stretch.

<'font-size'>

Xem thuộc tính CSS font-size.

<'line-height'>

Xem thuộc tính CSS line-height.

<'font-family'>

Xem thuộc tính CSS font-family.

Các giá trị font hệ thống

caption

Font hệ thống được dùng cho các điều khiển có chú thích (ví dụ: nút, danh sách thả xuống, v.v.).

icon

Font hệ thống được dùng để gắn nhãn các biểu tượng.

Font hệ thống được dùng trong menu (ví dụ: menu thả xuống và danh sách menu).

message-box

Font hệ thống được dùng trong hộp thoại.

small-caption

Font hệ thống được dùng để gắn nhãn các điều khiển nhỏ.

status-bar

Font hệ thống được dùng trong thanh trạng thái cửa sổ.

Các từ khóa font hệ thống có tiền tố

Các trình duyệt thường triển khai thêm một số từ khóa có tiền tố: Gecko triển khai -moz-window, -moz-document, -moz-desktop, -moz-info, -moz-dialog, -moz-button, -moz-pull-down-menu, -moz-list, và -moz-field.

Định nghĩa hình thức

Initial valueas each of the properties of the shorthand:
Applies toall elements and text. It also applies to ::first-letter and ::first-line.
Inheritedyes
Percentagesas each of the properties of the shorthand:
  • font-size: refer to the parent element's font size
  • line-height: refer to the font size of the element itself
Computed valueas each of the properties of the shorthand:
Animation typeas each of the properties of the shorthand:

Cú pháp hình thức

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]>

<absolute-size> =
xx-small |
x-small |
small |
medium |
large |
x-large |
xx-large |
xxx-large

<relative-size> =
larger |
smaller

<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

Ví dụ

Đặt thuộc tính font

css
/* Đặt cỡ chữ là 12px và chiều cao dòng là 14px.
   Đặt họ font là sans-serif */
p {
  font: 12px/14px sans-serif;
}

/* Đặt cỡ chữ bằng 80% của phần tử cha
   hoặc giá trị mặc định (nếu không có phần tử cha).
   Đặt họ font là sans-serif */
p {
  font: 80% sans-serif;
}

/* Đặt độ đậm là bold,
   kiểu font là italic,
   cỡ chữ là large,
   và họ font là serif. */
p {
  font: bold italic large serif;
}

/* Dùng font giống với thanh trạng thái của cửa sổ */
p {
  font: status-bar;
}

Mẫu trực tiếp

Thông số kỹ thuật

Thông số kỹ thuật
CSS Fonts Module Level 4
# font-prop

Tương thích trình duyệt

Xem thêm