1

I'm working on a React JS app with TailwindCSS, all Tailwind properties work, but the font is not updated when I run the app. If I put font with style prop, instead of className the font is shown correctly.

There is my page:

<div className="flex flex-row mr-8">
   <p className="text-white font-bold font-body">
    SALE ESPORTS
   </p>
</div>

This is my tailwind.config.js file

module.exports = {
  content: ["./src/**/*.{html, js, ts, jsx, tsx}"],
  theme: {
    fontFamily: {
      body: ["Chakra Petch"],
      gaming: ["Press Start 2P"],
      icons: ["Material Icons"],
    },
    extend: {
      colors: {
        primary: {
          accent: "#30ACFF",
          main: "#4448BB",
          dark: "#000A3B",
        },
        secondary: {
          accent: "#E84AE6",
          main: "#C01EE1",
          dark: "#70288F",
        },
        backColor: "#1A1B1F",
      },
    },
  },
  plugins: [],
};

There's my index.css

@tailwind base;
@tailwind components;
@tailwind utilities;

And in my index.html page I get the link from google fonts:

<link
      href="https://fonts.googleapis.com/css2?family=Chakra+Petch&display=swap"
      rel="stylesheet"
    />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link href="/dist/output.css" rel="stylesheet" />

In this picture I show the project structure:
1

2
  • try to inspect your page and check if your font is assigned to the element or not Commented Feb 28, 2023 at 17:02
  • as I mentioned in the question if I set the text font with prop "style" it works so it's probably a TailwindCss problem Commented Feb 28, 2023 at 17:48

1 Answer 1

0

Import the font in your main css file:

@tailwind base;
@tailwind components;
@tailwind utilities;
@import url("https://fonts.googleapis.com/css2?family=Chakra+Petch&display=swap");

And either escape the spaces in the font names, or wrap them in quotes:

    fontFamily: {
      body: ['"Chakra Petch"'], // <= This works
      gaming: ["Press\\ Start 2P"], // <= This works
      icons: ["Material Icons"], // <= This doesn't work
    },
Sign up to request clarification or add additional context in comments.

3 Comments

I tried but it still doesn't work
You also need to escape the spaces in font names, I edit my answer to reflect that.
It works because I have to set body like this: body: [" 'Chakra Petch' "] instaed of this: body: ["Chakra Petch"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.