Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

relative file path to fonts in css file

Tags:

html

css

I have a stylesheet that is referenced in the header:

<link href="./css/stylesheet.css" rel="stylesheet">

All of the css works in it except this specific code:

@font-face {
    font-family: 'icomoon';
    src: url('fonts/icomoon.eot?hsw0h3');
    src: url('fonts/icomoon.eot?hsw0h3#iefix') format('embedded-opentype'),
        url('fonts/icomoon.ttf?hsw0h3') format('truetype'),
        url('fonts/icomoon.woff?hsw0h3') format('woff'),
        url('fonts/icomoon.svg?hsw0h3#icomoon') format('svg');
    font-weight: normal;
    font-style: normal;
}

When I put the above @font-face CSS in the page above where the icons are being shown it works but when I put it in the CSS file it stops working. I finally found out that this was likely due to the file path not being correct.

Here is the file structure:

enter image description here

Looking at this article (https://css-tricks.com/quick-reminder-about-file-paths/) it looks like I should either use:

url('/fonts/icomoon.ttf?hsw0h3')
url('../fonts/icomoon.ttf?hsw0h3')

to go back to the root and then into the fonts folder. But the icon still is not rendering from the CSS file.

What am I doing wrong and how do I fix it?

like image 838
David Tunnell Avatar asked Jan 03 '16 21:01

David Tunnell


People also ask

What's font-family in CSS?

Font Families: Serif, Sans-serif, and others In CSS (and in typography in general) there are five basic types, or families, of fonts: serif, sans serif, cursive, fantasy, and monospace. Serif fonts have small lines or strokes that extend from the ends of characters.

How do you reference OTF fonts in CSS?

Add a font-face section to your CSS codesrc: url('fonts/lovely_font. otf') format('opentype'); src: url('fonts/lovely_font. ttf') format('truetype'); As another optional efficiency measure, we can get the browser to check for a local copy of the font in case the user already has it.


1 Answers

URLs in CSS files are relative to the CSS file.

url('fonts/icomoon.eot?hsw0h3'); means http://example.com/css/fonts/icomoon.eot?hsw0h3, but your screenshot of your directory structure shows you need http://example.com/fonts/icomoon.eot?hsw0h3.

Add ../ or /

like image 179
Quentin Avatar answered Oct 22 '22 14:10

Quentin