question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to import and use local fonts

See original GitHub issue

I have added the TPHero font in public folder and @font-face (refer to this old commit) but it does not seem to work.

Could you please help?

// webpack
{
  test: /\.(ico|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2)(\?.*)?$/,
  loader: 'file-loader',
  query: {
    name: isDebug ? '[path][name].[ext]?[hash:8]' : '[hash:8].[ext]',
  },
},
// in variable.css

@font-face {
  font-family: 'TPHero';
  font-style: normal;
  font-weight: normal;
  src:
    url('../../../public/fonts/TPHero/TPHero-Bold.otf') format('otf'),
    url('../../../public/fonts/TPHero/TPHero-BoldItalic.otf') format('otf'),
    url('../../../public/fonts/TPHero/TPHero-ExtraBold.otf') format('otf'),
    url('../../../public/fonts/TPHero/TPHero-ExtraBoldItalic.otf') format('otf'),
    url('../../../public/fonts/TPHero/TPHero-Hairline.otf') format('otf'),
    url('../../../public/fonts/TPHero/TPHero-HairlineItalic.otf') format('otf'),
    url('../../../public/fonts/TPHero/TPHero-Light.otf') format('otf'),
    url('../../../public/fonts/TPHero/TPHero-LightItalic.otf') format('otf'),
    url('../../../public/fonts/TPHero/TPHero-Medium.otf') format('otf'),
    url('../../../public/fonts/TPHero/TPHero-MediumItalic.otf') format('otf'),
    url('../../../public/fonts/TPHero/TPHero-Regular.otf') format('otf'),
    url('../../../public/fonts/TPHero/TPHero-RegularItalic.otf') format('otf'),
    url('../../../public/fonts/TPHero/TPHero-SemiBold.otf') format('otf'),
    url('../../../public/fonts/TPHero/TPHero-SemiBoldItalic.otf') format('otf'),
    url('../../../public/fonts/TPHero/TPHero-Super.otf') format('otf'),
    url('../../../public/fonts/TPHero/TPHero-Thin.otf') format('otf'),
    url('../../../public/fonts/TPHero/TPHero-ThinItalic.otf') format('otf'),
    url('../../../public/fonts/TPHero/TPHero-UltraLight.otf') format('otf'),
    url('../../../public/fonts/TPHero/TPHero-UltraLightItalic.otf') format('otf');
}
screen shot 2017-05-22 at 10 25 25 pm

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

65reactions
frenzzycommented, May 23, 2017

.otf is format('opentype'), demo

27reactions
frenzzycommented, May 23, 2017

You should not put to variables.css anything except variables, otherwise the code will be duplicated multiple times - depends on how many @imports of the variables.css file do you have.

The best place for custom fonts is your top level react component (src/components/Layout/Layout.css in RSK).

Then you need to define @font-face for each font separately. For example light, italic, bold etc. are different fonts. Also it is not recommended to import more than 2-3 fonts per page - it it too heavy.

About path to fonts, you can use the same modular/component approach here, just put them to the components folder and use relative path in url(./fonts/font-name.ext), but if you want save font-file name on production, put them to public/fonts/font-name.ext folder and use absolute path in css url(/fonts/font-name.ext).

Example:

/src/component/Layout/
├── /fonts/
│   ├── /Roboto-Regular.woff2
│   ├── /Roboto-Regular.woff
│   ├── /Roboto-Bold.woff2
│   └── /Roboto-Bold.woff
├── Layout.css
├── Layout.js
└── package.json
/* src/components/Layout/Layout.css */
@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 400;
  src:
    local('Roboto'),
    local('Roboto-Regular'),
    url(./fonts/Roboto-Regular.woff2) format('woff2'),
    url(./fonts/Roboto-Regular.woff) format('woff');
}

@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 700;
  src:
    local('Roboto Bold'),
    local('Roboto-Bold'),
    url(./fonts/Roboto-Bold.woff2) format('woff2'),
    url(./fonts/Roboto-Bold.woff) format('woff');
}

html {
  font-family: 'Roboto', sans-serif;
}

/* ... */
Read more comments on GitHub >

github_iconTop Results From Across the Web

To use local font in HTML using font face - Stack Overflow
To use local font in HTML using font face · Try to open the developer console (F12), what does it say? – Bálint...
Read more >
How To Load and Use Custom Fonts with CSS | DigitalOcean
Using local fonts relieves the browser of downloading and processing ... To do that, select the <link> option instead of the @import option ......
Read more >
The Easy Way to Add Fonts to Your Website (Including ...
How to add custom fonts to your website using @font-face · Step 1: Download the font · Step 2: Create a WebFont Kit...
Read more >
How to use @font-face in CSS
Google Fonts offers this as a way to use their fonts. The following is an example of using @import to load the Open...
Read more >
Access local fonts on your computer - Figma Help Center
This is a secure background service that allows Figma to access any fonts on your computer and make them available in the font...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found