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.

Better explanation for font-face

See original GitHub issue

Hey guys,

I spent a few minutes trying to inject a custom font-face for the first time and thought that there is some scope for improvement in the docs. The only example on font-face I’ve found uses a relative path to a font inside ``, which suggests to a newbie like me that the paths are somehow magically resolved by webpack from the given string (like this is done in standard webpack-controlled css files). Despite such expectations, that magic path replacement does not seem to happen.

Instead, the code should look like this:

import { injectGlobal } from 'styled-components';
import MyFont from './MyFont.otf';

injectGlobal`
  @font-face {
    font-family: MyFont;
    src: url('${MyFont}') format('opentype');
  }
`

It should be probably also noted that webpack needs to have a special loader for the fonts, e.g.:

{
test: /\.(eot|svg|ttf|woff|woff2|otf)$/,
loader: 'file-loader',
}

Perhaps an explanation like the one above can also be useful for those who want to embed background images etc. (I suspect that they don’t resolve magically too).

Hope this helps! styled-components is an awesome idea!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:69
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

11reactions
gabeweavercommented, Jul 8, 2017

@mxstbr I just burned several hours on this same issue. I sort of understand not wanting to promote webpack, but a lot of people use styled-components with Webpack and intentionally not adding examples to some sort of FAQ results in a poor user experience.

The same argument could be said for not promoting TypeScript…or Flow…yet there is explicit documentation and examples on using styled-components with them.

I’m happy to write the docs - because I’m thankful for you and all the work you’ve done here. I politely ask you to reconsider inclusion of this issue in the docs somewhere, primarily because enabling end-developers to ship value more quickly, with less waste should be our first and foremost goal. I don’t want to have to sift through stack overflow and github issues to try and find answers to a common scenario.

Overall, converting from css-modules and post-css to styled-components has been a breeze up until the font issue 😃

7reactions
mxstbrcommented, Nov 21, 2016

I like this! We probably won’t add this to the docs, since we don’t want to promote anything webpack specific. (since nothing about styled-components is webpack specific)

That being said, this is a great reference issue that people will find. Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use @font-face in CSS
The @font-face rule allows custom fonts to be loaded on a webpage. Once added to a stylesheet, the rule instructs the browser to...
Read more >
font-face - CSS: Cascading Style Sheets - MDN Web Docs
Chrome Edge @font‑face Full support. Chrome1. Toggle history Full support... OpenType CBDT and CBLC rendering Full support. Chrome66. Toggle history Full support... OpenType COLRv0 rendering Full...
Read more >
How To Define Custom Fonts in CSS with @font-face and font ...
@font-face is a CSS at-rule used to define custom fonts. With @font-face , you provide a path to a font file hosted on...
Read more >
The Essential Guide to @font-face - WebFX
The web font licensing problem has caused a major rise in the popularity of sites offering quality free fonts. Among these, the two...
Read more >
Best practices for fonts - web.dev
The @font-face declaration is an essential part of working with any web font. At a minimum, it declares the name that will be...
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