Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Font not loading in Reactjs

This is how I am loading my Font files in reactjs

import styled from 'styled-components';
import fontUrls from './UberMove-Bold.ttf';
import fontUrls1 from './UberMove-Light.ttf';
import fontUrls2 from './UberMove-Medium.ttf';
import fontUrls3 from './UberMove-Regular.ttf';

const Fonts = styled`
    @font-face {
       font-familty: 'UberMove-Light';
       src: url(${fontUrls1}) format('truetype');
    }`;

This throws an error while importing

Uncaught Error: Cannot create styled-component for component: @font-f .ace{font-familty:'UberMove-Light';src:url(,) format('truetype');}.

like image 764
Asif Saeed Avatar asked Jun 30 '26 03:06

Asif Saeed


1 Answers

You need to load the font-face globally and then use it in the components:

import { createGlobalStyle } from "styled-components";

const GlobalStyles = createGlobalStyle`
  body {
    @import url(${fontUrls1});
    font-family: 'UberMove-Light';
  }
`
const App = () => (
  <div>
    <GlobalStyles />

   //...

  </div>
)
like image 138
Clarity Avatar answered Jul 01 '26 19:07

Clarity



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!