คำสั่ง 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;
}

Результат: