I'll show you how to implement custom fonts in TailwindCSS and NuxtJS!

Currently, I am in the process of developing a website using NuxtJS along with Tailwind CSS for styling. To incorporate my desired fonts, I have utilized the @nuxtjs/tailwindcss module.

Despite applying the correct font-family through CSS, it seems that my fonts are not loading in the browser as expected. Even though the devtools screenshot displays the appropriate font being used, the text on the browser still renders in Times New Roman.

--View Devtools Screenshot

I have stored my font files as .ttf files within a /assets/fonts/ directory located in the root of my project.

The contents of my tailwind.css file are as follows:

@import 'tailwindcss/components';
@import 'tailwindcss/utilities';

@font-face {
  font-family: 'Montserrat';
  font-weight: 400;
  src: url('../fonts/Montserrat-Regular.ttf') format('ttf');
}

@font-face {
  font-family: 'Montserrat';
  font-weight: 700;
  src: url('../fonts/Montserrat-Bold.ttf') format('ttf');
}

@font-face {
  font-family: 'Montserrat';
  font-weight: 900;
  src: url('../fonts/Montserrat-Black.ttf') format('ttf');
}

Additionally, the configuration in my tailwind.config.js is configured like this:

module.exports = {
  theme: {
    fontFamily: {
      sans: ['Montserrat'],
      serif: ['Montserrat'],
      mono: ['Montserrat'],
      display: ['Montserrat'],
      body: ['Montserrat']
    },
    // Additional theme customization
 },
  variants: {},
  plugins: []
}

Upon conducting further investigation, I realized that simply extending Tailwind's base fonts may not yield the desired results. However, I do intend to clean up this implementation and utilize different fonts for specific texts once I find a suitable solution.

Although initial suspicions pointed towards Tailwind as the culprit, inspection via Devtools does show that Montserrat is indeed the computed font, and there have been no errors during the webpack build.

I have attempted the solutions provided in a relevant question on Stack Overflow (link here). While implementing the accepted answer, which mirrors my current setup, I have yet to achieve the desired outcome.

If anyone could offer assistance, I would greatly appreciate it!

UPDATE: I have created a Github repository to replicate the issue, available here. The README.MD contains detailed steps to reproduce the problem.

Answer №1

The issue is not related to Tailwind, Vue, or Nuxt – it solely lies within the realm of CSS.

The problem stems from incorrectly specifying the format value in the @font-face src. "ttf" should be an extension, not a format name. It should be changed to "truetype" instead. The confusion arises because for woff or svg formats, the extension matches the format name, unlike with "ttf" and "truetype".

To resolve this:

src: url('../fonts/Montserrat-Regular.ttf') format('ttf');

Should be replaced with:

src: url('../fonts/Montserrat-Regular.ttf') format('truetype');

Alternatively, you can omit the format altogether as it will still function correctly.

src: url('../fonts/Montserrat-Regular.ttf');

WOFF

Furthermore, it's advisable to utilize newer, more compact formats like woff and woff2.

src:
  url('../fonts/Montserrat-Regular.ttf') format('truetype'),
  url('../fonts/Montserrat-Regular.woff2') format('woff2'),
  url('../fonts/Montserrat-Regular.woff') format('woff')

Personally, I prefer using only woff and woff2, given their widespread support across major browsers. Based on caniuse coverage (>98% at the moment), there seems to be little reason to adhere to TTF anymore.

src:
  url('../fonts/Montserrat-Regular.woff2') format('woff2'),
  url('../fonts/Montserrat-Regular.woff') format('woff')

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

Ways to align a nested list vertically

There are two nested lists: <ul> <li>First item <ul> <li> <a href="#">some item</a> </li> <li> <a href="#">some item</a> <</li& ...

Steps to access a Windows folder after clicking on a link within an HTML page using Asp.net

I attempted to use the following code : <a href="file:///C:\Programs\sort.mw">Link 1</a> <a href="file:///C:\Videos\lecture.mp4">Link 2</a> Unfortunately, this code does not work in Google Chrome or Firefox bro ...

Vue automatically clears the INPUT field when disabling it

Have you ever noticed why Vue resets a text input field when it's set to disabled? This behavior is not observed with plain Javascript and also doesn't affect textarea fields. var app = new Vue({ el: '.container', data: { disab ...

The dynamic trio of Laravel, inertiajs, and vuejs

I am currently working on a project that involves laravel, inertiajs, and vuejs. Whenever I attempt to delete a user from the database, I consistently encounter a 404 | NOT FOUND error. My goal is to successfully delete the user by clicking a button using ...

Attention: validation of DOM nesting encountered an error: element <div> is not permitted to be nested inside <p>

I am facing the issue of not being able to find a solution to a known problem. The paragraph in question must not contain any other tags, yet I did not use any paragraph tags in my case. return ( <div className={classes.root}> <Stepper acti ...

What is the best way to position a grid of divs in the center?

To elaborate on the issue in depth, I need to establish a few assumptions: I possess a list of items with an unknown length (can range from 1 to 50 or more). My objective is to exhibit these items in a grid format, where the number of items per row varie ...

Error: Uncaught TypeError when using a for loop in Vue and GraphQL

I encountered a TypeError while attempting to iterate through a GraphQL query in my Vue project. The script snippet in question is as follows: <script> import { useQuery } from "@vue/apollo-composable"; import gql from "graphql-tag&qu ...

Attempting to position an image in the middle-right of a column using bootstrap

After spending all day researching flexbox and vertical align, I still can't figure out how to align an image to the right in my bootstrap column with text wrapping around it on the left. Check out what I've tried so far here: https://codepen.io/ ...

What could be causing my data to not appear in a new row?

Currently, I am facing an issue with how checked checkboxes are displayed. Is there a way to have each checkedbox appear on a new line instead of stacking beside each other in the same line? b-modal#modal-1.d-flex(title='Dodaj leki' hide-footer) ...

Container displaying zero height due to masonry issue

Currently, I am developing a project that requires a responsive masonry layout. The container has a maximum width set to 960px, but it is supposed to adjust based on the browser's size. Each grid item has a percentage-based width (100%, 66.666666etc%, ...

Looking for a way to identify when a DOM element is resized as a result of web fonts loading asynchronously?

Within this div element lies text styled with a custom font via CSS's @font-face attribute. The issue arises when the font hasn't loaded yet, causing a delay in updating the font style of the text within the div. As a result, taking measurements ...

Testing HTTP requests on a form click in Vue.js 2 - Let's see how

Within my component, I have the following method: methods:{ ContactUs(){ this.$http.post("/api/contact-us").then((res)=>{ ///do new stuff },(err)=>{ //do new stuff }) ...

Uninstalling plugins using vue-cli3

Is it feasible to eliminate plugins and their configurations using vue-cli3 in a pre-existing project? As an illustration, let's say I want to swap out the Mocha unit test plugin with Jest. While I understand how to incorporate and utilize a new plugi ...

Exceeded call stack size error when using Vuex and Onboard.js

Currently, I'm facing an issue while attempting to utilize Vuex and Onboard.js for storing a web3 provider in my DApp. Each time I try to commit the selected wallet in my state, I encounter a Maximum call stack size exceeded. Below is how my files are ...

Vue3 "Trying to access a variable that has not been defined on the current instance during rendering"

Whenever I compile my Vue3 code using Vite, I encounter the following error: runtime-core.esm-bundler.js:38 [Vue warn]: Property "tasks" was accessed during render but is not defined on instance. at <Home onVnodeUnmounted=fn ref=Ref< u ...

Center text within a dropdown using Bootstrap styling

In a row within my inline form, I have both an email input and a dropdown. However, after resizing the page, the text within the dropdown is no longer positioned correctly (text on the left, arrow on the right). I attempted to fix this by using: text-alig ...

Is there a function called 'onViewportChange' in media queries that triggers a search in the .css file for applicable styling?

So I'm not entirely sure about all the specifics, but my understanding is that each time a new HTML element is created, it checks through all linked CSS files and inline styles to see if those styles should be applied to that element. This suggests t ...

Combining Vue with Typescript and rollup for a powerful development stack

Currently, I am in the process of bundling a Vue component library using TypeScript and vue-property-decorator. The library consists of multiple Vue components and a plugin class imported from a separate file: import FormularioForm from '@/FormularioF ...

Attach the footer to the bottom of the screen only if the main page text is limited in length

How can I ensure that my footer sticks to the bottom of the screen only when there is minimal content on the page? When there is a lot of content, I'd like the footer to appear after the content rather than being pinned at the bottom, especially since ...

CSS // code that works on any device or platform

I have since the beginning been annoyed with the compatibility of CSS. Whats the best practice for coding css, that works with the most common platforms... ( IE7+, Firefox, Safari, Chrome AND iPad / iPhone, Blacberry, Android) Are there any list to be fo ...