Issue with Font Requests in Browsers when Using Angular 10 and SCSS with @font-face

I have a project built with Angular 10 that utilizes SCSS for styling. In my _typography.scss file, I have defined some @font-face rules pointing to the font files located in the assets/fonts directory. However, when I run the application, the browser does not initiate any requests to fetch these font files.

Below is the structure of the styles directory:

https://i.stack.imgur.com/A9qnT.png

This is how my _typography.scss looks like:

@font-face {
    font-family: 'Spartan';
    src: 'assets/fonts/Spartan-Medium.ttf' format('truetype');
    font-style: normal;
    font-weight: 500;
    font-display: swap;
}

@font-face {
    font-family: 'Spartan';
    src: 'assets/fonts/Spartan-SemiBold.ttf' format('truetype');
    font-style: normal;
    font-weight: 600;
    font-display: swap;
}

@font-face {
    font-family: 'Spartan';
    src: 'assets/fonts/Spartan-Bold.ttf' format('truetype');
    font-style: normal;
    font-weight: 700;
    font-display: swap;
}

I'm importing this file into my main styles.scss like so:

@import 'reset';
@import 'variables';
@import 'typography';
@import 'utilities';

body {
    font-family: 'Spartan', 'Segoe UI';
    font-weight: 500;
    font-size: 0.75rem;
}

h1,
h2,
h3,
h4,
h5,
h6 {
    font-weight: 700;
}

Despite the import, only the system font Segoe UI is being used. The browser does not even attempt to download the font files: https://i.stack.imgur.com/qEz85.png

I also tried preloading the fonts in my index.html, which did trigger requests from the browser, but the custom fonts were still not applied.

Any insights on why this might be happening?

P.S.: Here is the structure of my assets folder:

https://i.stack.imgur.com/vTkkp.png

Answer №1

My recent transition from using less to sass was met with a hiccup when it came to fonts not loading as expected.

After some troubleshooting, I discovered that the issue stemmed from declaring font-face within the html tag while working with sass, whereas it worked fine in less.

// experiencing issues
html {
font-face: ...
...
}

// resolved
font-face: ...

html { 
...
}

Answer №2

It may be past due, however in my situation the fonts on my website were overridden by the Angular Material theme. To verify this, you can inspect the computed styles using developer tools. If this is indeed the issue, it is advisable to create a custom typography for Angular Material.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing Angular-cli and Karma for seamless integration with jQuery

I have scoured the internet and cannot seem to find a solution for my current dilemma, despite it being a widely known issue... Upon running ng test, I am faced with an empty Karma browser: Here is what my console displays: To incorporate jQuery, I foll ...

What steps do I need to take to create an animation where an outline gradually becomes filled in

I've been experimenting with creating a loading animation inspired by the pre-loading animation on this website. My attempt using chat GPT resulted in an unexpected outcome where, instead of filling the SVG, it placed a black square on top of it. The ...

Struggling with Responsive Design Challenges with Grid Material UI?

Encountering an issue while using the Grid Component in my React JS Project. Let's delve into some code and then I'll explain what I aim to achieve with images: Assuming this is the rendered code: <div style="margin:100px 20%; width:80%" &g ...

What is the best way to automatically check dynamic checkboxes in Angular reactive forms based on database values?

As a beginner in reactive forms, I am facing challenges with dynamically setting the value of checkboxes to true. For instance, when retrieving pre-selected fruit values for a specific user from the database, I want those fruits to be checked when the page ...

Select hours using the PrimeNG slider feature

Is there a way to customize the PrimeNG slider in order to allow for selecting a time interval? I would like the slider to default to 12:00 when the user opens the application, but also be able to extend the selection range from 00:00 to 23:59. It seems ...

Move the footer down to the bottom of the page

I'm struggling to create a footer that consistently stays at the bottom of the page, regardless of the amount of content on the page. I've tried following various tutorials but haven't had much success. I'm starting to think I must be d ...

Implement a menu that can be scrolled through, but disable the ability to scroll on the body of the website

When viewed on a small screen, my website transforms its menu into a hamburger button. Clicking the button toggles a sidebar displaying a stacked version of the menu on top of the normal website (position: fixed; z-index: 5;). This sidebar also triggers a ...

CSS navigation issue

When using the third example provided on this page: http://simple-navigation-demo.andischacke.com/ I encountered an issue where the main page would display an empty div on the left instead of the content filling the entire area. However, when navigating ...

Choose an image to be displayed at either full width or full height, depending on which dimension is reached first

I have a query regarding resizing an image within a div to either 100% width or 100% height of the containing div. Despite setting max-height and max-width to 100% as recommended here, I still encounter overflow issues without wanting to crop the image usi ...

What are the best techniques for centering a prime ng form both vertically and horizontally?

I am currently using prime ng 9.1.3 along with primeflex 1.3.0 in my project. Despite applying p-align-center, I am facing difficulty in vertically and horizontally centering the form on the screen when using a full screen height of 100vh. <div class=&q ...

Utilizing the component within the template could potentially result in a cyclical dependency if it were to be imported

I have encountered an issue with two components that reference each other in a recursive manner. Ever since I upgraded to Angular 13, I am unable to use it and I keep receiving the error mentioned in the title. Component A (field.component.html): <app- ...

What is the main purpose of using the CSS transform:translate() property?

When CSS3 introduced animations with transition properties, it gave developers two main ways to change the position of an element. The first method involves setting the element's position as absolute and adjusting the left, right, top, and bottom prop ...

Angular Universal: Troubleshooting an Unrendered Route

Struggling for hours to make my site Universal and support Server Side Rendering, I came across the issue where the base route '' is not being rendered by the server. Surprisingly, all other routes are functioning properly when directly called fr ...

Chrome and Firefox: Images cling together

I've encountered an issue with my recently launched website. Some images in a grid appear to be stuck together, but this problem only occurs in Firefox and Chrome browsers. Oddly enough, zooming in to around 110% then back to 100% seems to temporarily ...

Angular: Connecting template data to different visual presentations

Looking for a solution to display data and map values to another presentation without needing complex ngIf statements or creating multiple components. Check out this sample: https://stackblitz.com/edit/angular-9l1vff The 'vals' variable contain ...

The nebular element is not displaying correctly

I recently followed the instructions in the official documentation to implement a nebular component into my project. Everything seemed to be working fine with the layout, but I ran into issues when trying to use the card component and other similar compone ...

Choose a Spot on the Open Layers map using a marker or icon

As a beginner in the world of Open Layers, I'm eager to learn how to utilize markers or icons to obtain user location. Additionally, I hope to harness the power of Angular to extract these location details. ...

Extension Overlay for Chrome

I'm currently working on developing a Chrome extension, but I'm encountering some confusion along the way. Despite researching online, I keep receiving conflicting advice and haven't been able to successfully implement any solutions. That&ap ...

What is the best way to apply a box-shadow to a specific side of an element?

How can I apply a box shadow to only the right side of a block element? Currently, I achieve this by wrapping the inner element with a box-shadow in an outer element with padding-right and overflow:hidden to hide the other three sides of the shadow. Is the ...

ensure that angular's ng-if directive sets aside space to display content when triggered by an event

I'm facing an issue with my modal form setup where if a text-field is left blank, a label saying "it is required" is generated below the field, causing the "Proceed" button to move (along with all other elements below it). How can I make sure that DOM ...