Home >Web Front-end >CSS Tutorial >How to Add Fonts to Create-React-App Projects Without Ejecting?
Using the build pipeline is the recommended method for adding fonts to create-react-app projects. This ensures proper processing, hashes for browser caching, and compilation error detection.
Using Imports:
Reference fonts in the CSS file using relative paths, like:
<code class="css">@font-face { font-family: 'MyFont'; src: local('MyFont'), url(./fonts/MyFont.woff) format('woff'); }</code>
Using the public Folder (Not Recommended):
Add a link to this CSS file in public/index.html:
<code class="html"><link rel="stylesheet" href="%PUBLIC_URL%/index.css"></code>
Note that using the public folder method is not preferred as it doesn't ensure proper processing, hashing, or error detection. It's also more prone to caching issues.
Regardless of the method, you'll need to have the fonts in a supported format, such as WOFF, WOFF2, or TTF.
The above is the detailed content of How to Add Fonts to Create-React-App Projects Without Ejecting?. For more information, please follow other related articles on the PHP Chinese website!