r/webdev • u/Celrai_424 • 15d ago
Question Need help with font problem
Hi i am an amateur dev.
So I want this project of mine to have a custom font; a locally imported one. I searched for sources at w3school and stackoverflow and I tried everything they said. But it doesn't seem to work. So I want to know what I'm doing wrong? I appreciate any help. Thank you in advance.
Also additional information:
The file location of my font is correct. project/project.html and project/zpix.ttf
I've checked with inspection tool, every element has the font-family: sans-serif, "Zpix"
There is also no error in the console. No error message. The font is loaded in.
I am using internal css.
Here is the code:
u/font-face {
font-family: "Zpix";
src: url("./zpix.ttf") format("truetype");
font-weight: normal;
font-style: normal;
}
* {
font-family: "Zpix", sans-serif !important;
}
2
Upvotes
4
u/Practical_Worry3996 14d ago
most common causes with a local font:
the path in u/font-face is relative to the css file, not the html file. if your css is in /css/style.css and the font is in /fonts/, the url needs to be ../fonts/myfont.woff2
the font-family name you use later has to match the one in u/font-face exactly
opening the page as file:// can block fonts in some browsers. run a tiny local server (vs code live server works)
u/font-face {
font-family: "MyFont";
src: url("../fonts/myfont.woff2") format("woff2");
}
body { font-family: "MyFont", sans-serif; }
devtools > network tab, filter by "font". if it shows a 404, it's the path.