Answer the question
In order to leave comments, you need to log in
How to add fonts to styled components?
Hello, please tell me why fonts are not added to the create-react-app application?
The fonts are in the src/fonts/ folder.
There are no errors when connecting, what could be the problem?
import styled, { createGlobalStyle } from 'styled-components'
export function fontFace(
name: string,
src: string,
fontWeight: number | string = 'normal',
fontStyle: string = 'normal'
) {
/* eslint-disable */
return `
@font-face{
font-family: "${name}";
src: url(${require('../fonts/' + src + '.eot')});
src: url(${require('../fonts/' + src + '.eot')}?#iefix) format("embedded-opentype"),
url(${require('../fonts/' + src + '.woff')}) format("woff"),
url(${require('../fonts/' + src + '.ttf')}) format("truetype"),
font-style: ${fontStyle};
font-weight: ${fontWeight};
}
`
}
export const GlobalStyle = createGlobalStyle`
${fontFace('OswaldRegular', 'Oswald-Regular', 'normal', 'normal')}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
li {
list-style: none;
}
a {
text-decoration: none;
}
body {
font-family: 'OswaldRegular';
}
`
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question