Using self-hosted fonts in Next.js does not function properly

Exploring Font Hosting in Next.js

I am currently investigating the use of self-hosted fonts within a Next.js application.

Previous Attempts

https://i.sstatic.net/AuIwB.png

https://i.sstatic.net/P6ekH.png

Outcome

So far, CSS only seems to recognize the last font file.

Crucial Information

The Bold, Medium, Italic, and Regular fonts are separate files, all in woff2 format. I have experimented with different @font-face declarations for each file (bold, regular, and medium) and assigned unique names to each. While this method worked, I believe there may be a more efficient approach.

Answer №1

Make sure to include one @font-face declaration for each style variant, such as weights and italics, under the same font-family. Otherwise, styles may not be correctly linked to their respective font files.

In your current setup, you are only overriding the first url, causing "Metropolis-Medium.woff2" not to load as it is replaced by "Metropolis-Bold.woff2".

You can use multiple URLs for fallback formats like woff2, woff, ttf, etc.

If no font-weight values are specified, the browser will match the family name "Metropolis" with "Metropolis-Bold.woff2" file or a regular font weight (e.g., 400).

For better compatibility, consider using more detailed rules, although some browsers may be more forgiving:

@font-face {
  font-family: Metropolis;
  font-weight: 400;
  font-style: normal;
  src: url("../../../public/fonts/metropolis/Metropolis-Regular.woff2")
      format("woff2"),
    url("../../../public/fonts/metropolis/Metropolis-Regular.woff")
      format("woff");
}

@font-face {
  font-family: Metropolis;
  font-weight: 400;
  font-style: italic;
  src: url("../../../public/fonts/metropolis/Metropolis-Italic.woff2")
      format("woff2"),
    url("../../../public/fonts/metropolis/Metropolis-Italic.woff")
      format("woff");
}

@font-face {
  font-family: Metropolis;
  font-weight: 500;
  font-style: normal;
  src: url("../../../public/fonts/metropolis/Metropolis-Medium.woff2")
      format("woff2"),
    url("../../../public/fonts/metropolis/Metropolis-Medium.woff")
      format("woff");
}

@font-face {
  font-family: Metropolis;
  font-weight: 700;
  font-style: normal;
  src: url("../../../public/fonts/metropolis/Metropolis-Bold.woff2")
      format("woff2"),
    url("../../../public/fonts/metropolis/Metropolis-Bold.woff") 
      format("woff");
}

body {
  font-family: Metropolis, sans-serif;
}

.medium {
  font-weight: 500;
}

.bold {
  font-weight: 700;
}

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

Capybara - Navigating a page by clicking on a link related to another value

I'm currently facing a challenge in locating and selecting a particular link on a web page. All links within an HTML table contain the term 'Edit' (it's a link for editing configuration settings). However, the exact link changes depend ...

Unable to Render CSS After Submitting a Post with Express

Upon initially loading the page, I was successful in getting the CSS to load. However, when I submit data using POST with a button, the post functionality works fine but the CSS fails to load. As a result, the screen appears plain with only the post inform ...

Ways to stack added li elements in CSS

I've successfully implemented a feature where clicking on buttons appends and removes li items with images in a ul parent element. These li items are not hardcoded in the HTML, allowing users to add them in any order based on their button-click seque ...

Running multiple NextJs applications using a single nginx configuration

My root directory is located at /var/www/html/. Within this root directory, I have multiple projects such as project1 and project2. I am looking to host two NextJs applications within a single file in my nginx configuration. Here is the nginx config file ...

The Problem of Restoring Column Height in Tabulator 4.6.3 Filters

The Issue After activating and deactivating header filters, the column height does not return to its original state. Is this the expected behavior? Is there a way to reset the column height? Check out this JS Fiddle example: https://jsfiddle.net/birukt ...

Form a triangle in order to prevent the inner content from spilling out

I'm currently attempting to design a triangle shape that prevents the inner content from overflowing while still allowing the inner content to be draggable. So far, I have experimented with various solutions such as polygons and canvas, but none of t ...

Encountering a 404 error when attempting to reload a page with meta refresh in Next.js

Here is a link to the codesandbox where I've created a demonstration of the issue: https://codesandbox.io/p/sandbox/charming-surf-kmtbib In essence, this is what the index page looks like: index.tsx import Head from "next/head"; const Hom ...

Overflow of text arranged horizontally within a span element situated inside a div container

I am currently working on developing a ticketing system that involves using nested div elements to organize content. Each ticket is represented by a main div containing various other nested divs and media such as images. While the functionality of the sys ...

Achieve the hidden div scroll effect in React by initially hiding the div and then allowing

I am currently working on a web chat application using next.js. One of the features is an emoji picker button that, when clicked, displays a menu of emojis. The issue I am facing is that in order for the user to see the emoji menu, they have to scroll down ...

Encountering Nested CSS bugs while implementing Tailwind in a Next.js/React project

Just diving into the world of Next.js/React. I recently went through the Tailwind CSS for Next.js tutorial and successfully integrated Tailwind into my project by following these steps: npm install -D tailwindcss postcss autoprefixer npx tailwindcss init - ...

Unveiling the magic of NextJS: A guide to leveraging dynamic content in

I'm working on a website project that uses NextJS and Strapi as the CMS. My goal is to be able to manage the navbar links through Strapi. Within my NextJS application, I have created a layout component that wraps around the pages in the _app.js file. ...

How can I detect a DOM element mutation based on a CSS selector, and if this is possible, how can it be accomplished?

Imagine there's a website with a specific HTML element. It seems that this element has the same class during the DOMContentLoaded event as it does during the load event. However, after the load event, this class (and possibly the ID and other HTML att ...

The functionality of router.prefetch is not recognized, which is causing difficulties in verifying the submit function with React Testing Library

This is a test code for evaluating the functionality of a component. I am checking whether a form submit function is triggered or not. import React from 'react' import { render, screen, fireEvent } from '@testing-library/react' import & ...

Trouble with getElementsByClassName not targeting several divs at once

<a id="backgroundenamel_realbutton_powderblue" href="javascript:set_radio('radio_bgenamel_powderblue');" class="radio-picture-enamel" style="background-color: #97b4d2;" onclick="document.getElementsByClassName('cx00ringbuilder_topinsi ...

Tips for Incorporating HTML Code into an ASP Button's Text Attribute

How can I add a symbol to a button's text without it showing as blank? The symbol in question is an arrow, which you can find here. <asp:button id="btnAdd" runat="server" class="next"/> CSS: .next { content = &#10095; width:50px; } ...

Why are my cursor and my drawing line on opposite sides?

I've been working on a JavaScript drawing app, but I'm facing an issue where the drawn elements are not aligned with my cursor. The positioning seems off, especially when moving to the right or up on the canvas. As I move towards the furthest lef ...

The conflict between Material UI's CSSBaseline and react-mentions is causing issues

Wondering why the CSSBaseline of Material UI is causing issues with the background color alignment of React-mentions and seeking a solution (https://www.npmjs.com/package/react-mentions) Check out this setup: https://codesandbox.io/s/frosty-wildflower-21w ...

The component next/image is experiencing issues when used in conjunction with CSS

I struggled to create a border around an image because the custom CSS I wrote was being overridden by the Image component's CSS. Despite trying to leverage Tailwind and Bootstrap to solve the problem, my efforts were unsuccessful. Now, I am at a loss ...

The idea behind this jQuery and CSS 3 animation is to create a dynamic visual of moving along a road that disappears into the

At the outset, let's clarify that I am seeking a conceptual solution or approach rather than actual working code. The Issue at Hand I have been tasked with exploring a navigation concept for a timeline wherein you would walk down a road towards the ...

The powers of jQuery and CSS selectors

Previously, I utilized the following code to extract text from the data-placeholder attribute and apply it as a placeholder for my div. [contentEditable=true]:empty:not(:focus):before { content:attr(data-placeholder) } While this method worked well b ...