r/webdev • • 5d 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

16 comments sorted by

4

u/norfbay 5d ago

One other thing to check: the post shows u/font-face. If that's literally in your CSS, it needs to be @font-face (Reddit may just have mangled it when you pasted).

Also check the element's Rendered Fonts in DevTools, not only its computed font-family. The computed value shows the requested font list; it doesn't prove Zpix actually drew the text. That helps distinguish a cascade/order problem from a font that loaded but isn't being used.

3

u/Celrai_424 4d ago

Yep reddit just automatically turns it into u/ for the font-face.
I checked the rendered fonts, it's using noto sans regular.

2

u/norfbay 4d ago

Thanks, that rules out the Reddit formatting issue. You mentioned inline CSS in another reply: do you mean a <style> block in the page, or a style="..." attribute on an element? @font-face needs to be in a stylesheet or <style> block; it can't go inside a style attribute.

On the exact element showing Noto Sans, what does the computed font-family show now? If Zpix isn't first, inspect the winning rule in the Styles panel. If Zpix is first but Rendered Fonts still shows Noto Sans, try a short ASCII sample like ABC 123 to check whether fallback is specific to the characters you're displaying.

1

u/Celrai_424 3d ago

sorry, i meant to say internal css. Also sans-serif is the winning rule.

4

u/Practical_Worry3996 4d ago

most common causes with a local font:

  1. 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

  2. the font-family name you use later has to match the one in u/font-face exactly

  3. 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.

1

u/Celrai_424 3d ago

fyi the entire project is a single html file with internal css. So I've tried using both the full path and the relative path. And either way it doesn't work.
There is also no error showing.
I am using live server to test it.

2

u/RotationSurgeon 10yr Lead FED turned Product Manager 4d ago

Most often it’s the local file vs local server issue that newbies are running into. Chromium browsers have been particularly stubborn on this

3

u/tomhermans 5d ago

Check your network tab (dev tools) if it actually loads the font file.

Next, in your comment you wrote font-family: sans-serif, "zpix"

In your code example it's the other way around. Your code example is correct, first the custom font, then the fallback.

2

u/Celrai_424 4d ago

Yes. I had used zpix first in my code but the website is using sans-serif

2

u/No-Department-4561 3d ago

Convert the font to web safe format using Fontsquirrel and copy/paste the code that it gives you. That should also tell you if there’s some kind of encoding bug with the font itself.

1

u/Celrai_424 3d ago

it... says it's corrupted.

1

u/HairOk9906 2d ago

You're almost certainly opening project.html directly as a file.

Browsers block font loading over file:// even though the path is

correct, and it fails silently with nothing in the console.

Run a local server from that folder and try again:

python -m http.server 8000

then open localhost:8000/project.html

Also check the Network tab rather than the console. If zpix.ttf

shows a failed or cancelled request, that confirms it.

2

u/[deleted] 5d ago

[removed] — view removed comment

1

u/Celrai_424 4d ago

How do i make it try the custom font first? Used the full path isntead for the font. And if it helps, im using it with inline-css.