font-faceコマンド

コマンド @font-face は、 ファイルに保存されたソースを持つ カスタムフォントを定義することを可能にします。 このフォントはその後、プロパティ font-family を使用して接続することができます。

構文

css@font-face {
  フォント名;
  フォント検索ソース;
}

例

ローカルコンピューター上からフォントを探してみましょう:

html<body>
  This is Montserrat SemiBold.
</body>
css@font-face {
  font-family: "Montserrat SemiBold";
  src: local("Montserrat SemiBold");
}
  
body {
  font-family: "Montserrat SemiBold";
}

#elem {
  animation: anim 3s infinite;
  border: 1px solid black;
  width: 50px;
  height: 50px;
}

Результат:

例

次に、フォントを検索するためのリモートサーバーのアドレスを指定しましょう:

html<body>
  This is Bitstream Vera Serif Bold.
</body>
css@font-face {
  font-family: "Bitstream Vera Serif Bold";
  src: url("https://mdn.github.io/css-examples/web-fonts/VeraSeBd.ttf");
}
  
body {
  font-family: "Bitstream Vera Serif Bold", serif;
}

#elem {
  animation: anim 3s infinite;
  border: 1px solid black;
  width: 50px;
  height: 50px;
}

Результат: