Face font doesn’t load a font I downloaded and placed in the same folder as my website.
@font-face {
font-family: 'fontName';
src: url('font.ttf');
}
header {
text-align: center;
}
h1 {
font-family: 'fontName';
I did change the ttf file name, but I don’t think that is the issue as it didn’t work on another font I tried.
Are there any errors in my code?
2
Answers
The
.ttf
file format is not enough on its own, you have to specify itsformat
as well (as seen in the provided CSS), as well as thefont-family
must be the determined according to the font’s title, for example: "font_Name_regular".And the following formats are also needed:
for the text to appear in a cross-browser way (in most browser environments).
You can generate them via a third-party online app, like this: https://www.fontsquirrel.com/tools/webfont-generator
using the "Expert…" mode, choosing all the file-formats. The more you choose the better the cross-browser compatibility will be.
Then you’ll get (in a zip) something like the following:
font-name-webfont.ttf
(which is your base file, but again, you need to be attentive about thefont-family
name, that is the "title"),font-name-webfont.eot
,font-name-webfont.svg
,font-name-webfont.woff
&
font-name-webfont.woff2
files.
And inside the given
stylesheet.css
, you’ll find the following, which is important, as said earlier:Which then you can use, as desired, on your website.
Try adding
local('fontName'),
tosrc:
before theurl()
. See the MDN page on@font-face
. This is working for me, although I am loading a local*.woff2
font, not*.ttf
.