Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

html - Cannot get custom fonts to work, even when copy pasting tutorial code

<html>
<head>
  <title>Web Font Sample</title>
  <style type="text/css" media="screen, print">
    @font-face {
      font-family: "Bitstream Vera Serif Bold";
      src: url("https://mdn.mozillademos.org/files/2468/VeraSeBd.ttf");
    }

    body { font-family: "Bitstream Vera Serif Bold", serif }
  </style>
</head>
<body>
  This is Bitstream Vera Serif Bold.
</body>
</html>

This bit of code is available on https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face. It is supposed make the sentence "This is Bitstream Serif Bold" appear in the VeraSeBD font. However it simply does not.

I can change fonts to arial for example. Using this code, the font is different:

<html>
<head>
  <title>Web Font Sample</title>
  <style type="text/css" media="screen, print">
    @font-face {
      font-family: "Bitstream Vera Serif Bold";
      src: url("https://mdn.mozillademos.org/files/2468/VeraSeBd.ttf");
    }

    body { font-family: arial }
  </style>
</head>
<body>
  This is Bitstream Vera Serif Bold.
</body>
</html>

How does one proceed when even copy pasting sample code still does not make it work?

The setup for the test is using a PHP server running on localhost on a windows machine

question from:https://stackoverflow.com/questions/65908647/cannot-get-custom-fonts-to-work-even-when-copy-pasting-tutorial-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If this does not work then it is probably CORS you're dealing with. Please do not use the java2s website as a reference for html and css because this site is very very old and not up-to-date.

First, the browser will try to load the modern woff2 font. If it fails then it will try to load the older woff font. The ttf font is a fallback for very old browsers/devices. At the end, if no fonts can be loaded , it falss back to the default serif font.

edit all fonts fail to load. Reason: they has been blocked by CORS policy.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Web Font Sample</title>
<style>
@font-face {
    font-family: 'Bitstream Vera Serif Bold';
    font-style: normal;
    font-weight: 400;
    src: local('Bitstream Vera Serif Bold'),
         url('https://mdn.mozillademos.org/files/2468/VeraSeBd.woff2') format('woff2'),
         url('https://mdn.mozillademos.org/files/2468/VeraSeBd.woff')  format('woff'),
         url('https://mdn.mozillademos.org/files/2468/VeraSeBd.ttf')   format('truetype');
}
body {
    margin: 0;
    font-family: "Bitstream Vera Serif Bold", serif;
    font-weight: 400;
    font-size: 16px;
    line-height: 1.5;
}
</style>
</head>
<body>
This is Bitstream Vera Serif Bold.
</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...