Search code examples
csssassfont-facetruetype

Getting Error on Importing Custom Fonts using SASS


I am using Sass for my project and I am getting this error on my home page while importing fonts from my custom fonts folder of Merriweather.The problem is that when i check the network tab on chrome it shows that Agenda font file is included but not the merriweather font file.Both Agenda and Merriweather fonts are in separate folders.

Error :

**http://localhost/project-name/fonts/Merriweather/Merriweather-Italic.tff net::ERR_ABORTED 404 (Not Found)**

My Project folder structure in like this:

  • project-folder/fonts-folder/Merriweather-folder/All fonts of Merriweather here

  • project-folder/fonts-folder/Agenda-folder/All fonts of Agenda here

  • project-folder/scss-folder/partials-folder/global.scss

  • project-folder/scss-folder/variables.scss

QUESTIONS:

  • I want to know the is my syntax right for importing multiple fonts from the same font family like Merriweather ?

  • what exactly i am doing wrong in variables.scss ??

    Please help me to resolve this issue.

Code in Variables.scss

   @font-face {
    font-family: Agenda;
    font-weight: 500;
    font-style: normal;
    src: url("../fonts/Agenda/Agenda-Medium.otf") format("opentype");
}

@font-face {
    font-family: Merriweather-Italic;
    font-weight: normal;
    font-style: italic;
    src: url("../fonts/Merriweather/Merriweather-Italic.tff") format("truetype");
}


@font-face {
    font-family: Merriweather-Regular;
    font-weight: normal;
    font-style: normal;
    src: url("../fonts/Merriweather/Merriweather-Regular.tff") format("truetype");
}

This is how i am using fonts in globals.scss

@import "../variables";

body{

    font-family: Merriweather-Regular;
    font-size: 22px;
    background-color: $white;

}

h1{
    font-family: Merriweather-Italic;
    font-style: italic;
    font-size: 94px;
}

h2{
    font-family: Merriweather-Regular,Merriweather-Italic;
    font-size: 42px;
}

h3{
    font-family: Agenda;
    font-size: 30px;
}

h4{
    font-family: Merriweather-Regular;
    font-style: bold;
    font-size: 27px;
}

h5{
    font-family: Merriweather-Regular;
    font-size: 26px;
    

}

Solution

  • To be sure I would have to see a screenshot of your fonts folder, but I think the font isn't found because of typo in the font-face declaration. True-type font files usually have a .ttf extension instead of .tff.

    That means you would have to adjust your @font-face declaration from:

    @font-face {
       // omitted font-family, weight and style for clarity
       src: url("../fonts/Merriweather/Merriweather-Italic.tff") format("truetype");
    }
    

    to:

    @font-face {
       // omitted font-family, weight and style for clarity
       src: url("../fonts/Merriweather/Merriweather-Italic.ttf") format("truetype");
    }
    

    At this point, so in variables.scss, I usually also declare a font variable for each font (type) for usage in the rest of my SASS files, e.g $merriweather--italic: "Merriweather-Italic", serif;

    Therefore you can use these variables in your global.scss as such:

    h1{
      font-family: $merriweather--italic;
      font-size: 94px;
    }