Regola font-face

La regola @font-face permette di definire un font personalizzato, la cui sorgente è memorizzata in un file. Questo font poi potrà essere collegato utilizzando la proprietà font-family.

Sintassi

css@font-face {
  nome del font;
  sorgente per trovare il font;
}

Esempio

Troviamo un font sul nostro computer locale:

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

Результат:

Esempio

Ora specifichiamo l'URL di un server remoto per trovare il font:

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

Результат: