The properties of the NextFont variable cannot be utilized in the CSS global scope when using the var() function

// font.ts file

import { Quicksand, Syncopate } from 'next/font/google';

export const quickSandRegular = Quicksand({
  subsets: ['latin'],
  variable: '--font-quicksand-reg',
  weight: '400',
  display: 'swap',
});
export const quickSandSmBold = Quicksand({
  subsets: ['latin'],
  variable: '--font-quicksand-smbold',
  weight: '600',
  display: 'swap',
});

export const syncopateRegular = Syncopate({
  subsets: ['latin'],
  variable: '--font-syncopate-reg',
  weight: '400',
  display: 'swap',
});

export const syncopateBold = Syncopate({
  subsets: ['latin'],
  variable: '--font-syncopate-bold',
  weight: '700',
  display: 'swap',
});

//layout.tsx file

import {
  quickSandRegular,
  quickSandSmBold,
  syncopateBold,
  syncopateRegular,
} from '@/lib/fonts';

export default function RootLayout({
 children,
}: Readonly<{
  children: React.ReactNode;
}>) {return (
    <html lang="en">
      <body
        className={`${[
          syncopateRegular.variable,
          syncopateBold.variable,
          quickSandRegular.variable,
          quickSandSmBold.variable,
        ]}  `}
      >
        {children}
      </body>
    </html>
  );
}
// global.css file
@tailwind base;
@tailwind components;
@tailwind utilities;

@layer components {
  .card {
    & .header {
      font-family: var(--font-syncopate-reg);
      @apply center;
    }
  }
}
// page.tsx file

export default function CustomerList() { 
  return (<Card className="card">
        <CardHeader>
          <CardTitle className="header">RIMIX</CardTitle>
        </CardHeader>
      </Card>)
}

I attempted to modify the font using NextFont's variable in my CSS file, but it did not take effect.

Following the guidance from NextJs documentation, I tried specifying the font family inside my css file using the defined variable name.

const syncopateRegular = Syncopate({
  subsets: ['latin'],
  variable: '--font-syncopate-reg', /** using this in global css file in var() */
  weight: '400',
  display: 'swap',
});

However, the font of my text remained unchanged.

Initially, I suspected the issue was due to using .className instead of .variable suffix for the fonts inserted into the Root Layout, but that was not the cause.

Answer №1

After delving into the documentation, I successfully tackled this issue by realizing that I needed to include the .variable classNames individually instead of placing them all in an array.

<body
        className={`${syncopateRegular.variable} ${syncopateBold.variable} ${quickSandRegular.variable} ${quickSandSmBold.variable}  `}
      >
        {children}
      </body>

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

Troubleshooting the Confirm Form Resubmission problem on my website

Hello everyone! I'm working on a website and facing an issue where refreshing the page triggers a confirm form resubmission prompt. Could someone please advise me on how to resolve this? Thank you in advance! ...

Next.js - navigate to the homepage without causing a page refresh

// import Link from "next/link"; <Link href="/">Home</Link> Is there a way to prevent the page from refreshing when users navigate to the homepage using the link above? ...

"Customize your Angular application by updating the background with a specified URL

Hello there, I've been attempting to modify the background URL once a button is clicked. In my CSS, I have the following default style set up. .whole-container { background: linear-gradient( rgba(0, 0, 0, 2.5), ...

Prevent CSS3 columns from reverting back to their original state after being rendered

Utilizing css3 columns, I have created a layout with an unordered list displayed in 3 columns. Each list item contains another list that can be toggled to show or hide by clicking on the title using jQuery. The structure of the html is as follows (with ex ...

Bootstrap icon issue: square displaying instead of the intended icon

I am currently in the process of creating a responsive website using Bootstrap 3. I have successfully downloaded and imported Bootstrap 3 into the root directory of my website. However, I have encountered an issue where the icon does not display correctly ...

What is the level of visibility in Nextjs?

Is it safe to expose the sources of files located in the 'pages/' directory? For instance, if you set up a page specifically for administrators at pages/admin and restrict access through Middleware, does this enhance security measures? ...

Building websites similar to Medium using ReactJS with emphasis on server-side rendering

I am in the process of creating a project similar to Medium.com using ReactJS. However, I am facing issues with meta tags and SEO. It is known that modifying meta tags on the client side (e.g. using react-helmet) is not recommended due to web crawlers not ...

Changing the color of tabs using inline styles in material ui does not seem to work

I am brand new to using material ui and have been attempting to alter the colors of the selected tab. Currently, the color is a dark blue shade and I am aiming to change it to red. I tried applying inline styles, but unfortunately, there was no change. C ...

What is the reason behind installing both Typescript and Javascript in Next.js?

After executing the command npx create-next-app --typescript --example with-tailwindcss my_project, my project ends up having this appearance: https://i.stack.imgur.com/yXEFK.png Is there a way to set up Next.js with Typescript and Tailwind CSS without i ...

Are we utilizing this JavaScript function properly for recycling it?

Two functions have been implemented successfully. One function adds the autoplay attribute to a video DOM element if the user is at a specific section on the page. The other function smoothly slides in elements with a transition effect. The only limitatio ...

My element's padding being overlooked by Tailwind CSS

In the process of developing a comment/response mechanism through MPTT, I am utilizing indentation to clearly designate replies and their respective levels. The comment.level attribute is being employed to establish the padding value. Consequently, a comm ...

A step-by-step guide to effectively dockerizing the development and production environments for a Next.js 13 application directory

My development Dockerfile is set up as follows: FROM node:18-alpine WORKDIR /app # Installing dependencies based on the preferred package manager COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ RUN \ if [ -f yarn.lock ]; then ya ...

Ways to incorporate design elements for transitioning focus between different elements

When focusing on an element, I am using spatialNavigation with the following code: in index.html <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dfb5acf2acafbeabb6beb3f2b1bea9b6 ...

How can I add a black-colored name and a red-colored star to the Placeholder Star as a required field?

I'm looking to customize the placeholder text in an input field. I want the main placeholder text to be in black and have a red star indicating it's a required field. However, my attempt to set the color of the star specifically to red using `::- ...

What is the best way to superimpose text on a variety of images with varying sizes using CSS

I have a client who is requesting a sold text to be displayed on top of all images that have been sold, positioned in the bottom left corner of each image. While I have successfully implemented this feature, the issue arises when dealing with images of var ...

The HTML content retrieved through an ajax service call may appear distorted momentarily until the JavaScript and CSS styles are fully applied

When making a service call using ajax and loading an HTML content on my page, the content includes text, external script calls, and CSS. However, upon loading, the HTML appears distorted for a few seconds before the JS and CSS are applied. Is there a sol ...

"How can you enhance the performance of JavaScript and CSS in a Chrome Extension without using exclude_matches/globs or excluding domains

I have been in the process of creating a Chrome Extension, and unfortunately, when I tried to make it work on specific URLs, I encountered an issue. While Chrome has options like exclude_matches and exclude_globs for this purpose, there seems to be a bug i ...

Overflow and contain a flex item within another flex item

I am facing a challenge where I need to prevent a flex-child of another flex-child from overflowing the parent, ensuring it does not exceed the screen height or the height of the parent (which is also a flex-child). Here's the CodePen link for refere ...

The socket context provider seems to be malfunctioning within the component

One day, I decided to create a new context file called socket.tsx: import React, { createContext } from "react"; import { io, Socket } from "socket.io-client"; const socket = io("http://localhost:3000", { reconnectionDela ...

The Link tag in the Hero.jsx file of Next.js is malfunctioning and failing to redirect to the intended URL

Having a problem with the button in my Hero.jsx component, part of the Page.js implementation. The button uses a Link tag to redirect to the url.js page, but it's not working as expected and showing an Error 404 page instead. I'm new to Next.js s ...