How to add fonts to create-react-app based projects?

Cover Image for How to add fonts to create-react-app based projects?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

How to Add Fonts to create-react-app Based Projects?

šŸ“ Intro: So, you're working on a project using create-react-app, and you want to add some custom fonts without ejecting? Don't worry, I got you covered! šŸŽ‰

šŸ¤” The Problem: The issue is, when importing fonts via @font-face and loading them locally, it's not clear where the fonts should go in your project structure. šŸ˜•

šŸ”Ž The Solution: Here's a step-by-step guide to help you add fonts to your create-react-app based projects:

  1. Create a "fonts" folder in your public/static directory.

    • Open your project in your favorite code editor.

    • Navigate to the "public" folder.

    • Inside the "public" folder, create a new folder called "static".

    • Inside the "static" folder, create another folder called "fonts".

  2. Place your font files in the "fonts" folder.

    • Find or download the font files you want to use (e.g., .woff files).

    • Move the font files into the "fonts" folder you created in the previous step.

  3. Update your CSS to reference the font files.

    • Open the CSS file where you want to use the custom fonts (e.g., GameModule.css).

    • Use the @font-face rule to define the custom fonts.

    • In the src property, provide the local font name and the URL to the font file.

    • Use the %PUBLIC_URL% placeholder to reference the "public" folder.

    • For example:

      @font-face { font-family: 'Myriad Pro Regular'; font-style: normal; font-weight: normal; src: local('Myriad Pro Regular'), url('%PUBLIC_URL%/static/fonts/MYRIADPRO-REGULAR.woff') format('woff'); }
  4. Use the custom font in your project.

    • Now that you have defined the custom font, you can use it just like any other font.

    • For example, you can apply it to specific elements using the font-family property.

That's it! šŸŽ‰ You have successfully added custom fonts to your create-react-app based project without ejecting. šŸ™Œ

šŸŽ‰ Call-to-Action: Now that you know how to add fonts to your create-react-app based projects, why not give it a try? āœØ Share your experience or any cool font choices you made in the comments below. Let's inspire each other! šŸ’¬šŸ’”

Happy coding! šŸ‘©ā€šŸ’»šŸŽØ


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

šŸ”„ šŸ’» šŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! šŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings šŸ’„āœ‚ļø Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide šŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? šŸ¤” Well, my

Matheus Mello
Matheus Mello