在CSS中加载其他字体,主要有两种方式:

使用@font-face规则

@font-face规则允许你在CSS中定义一个字体,然后在你需要的任何地方使用它。以下是一个例子:

css

@font-face {

  font-family: "My Custom Font";

  src: url("my-custom-font.woff2") format("woff2"),

       url("my-custom-font.woff") format("woff"),

       url("my-custom-font.ttf") format("truetype");

}

在上面的代码中,我们首先定义了一个新的字体家族"My Custom Font"。然后,我们提供了多个源,包括.woff2, .woff, 和 .ttf。如果用户的浏览器支持其中一种格式,那么它就会被加载和使用。为了提供最佳的兼容性,建议提供多种字体格式。


然后,你就可以在你的CSS中使用这个字体了:

css

body {

  font-family: "My Custom Font", sans-serif;

}