Search code examples
cssreactjsstyled-componentsgoogle-fonts

I want to import and use Google Web Fonts in the style component


I was trying to import Google Web Fonts using the style component GlobalStyled, but I got the following error.

Please do not use @import CSS syntax in createGlobalStyle at this time, as the CSSOM APIs we use in production do not handle it well. Instead, we recommend using a library such as react-helmet to inject a typical meta tag to the stylesheet, or simply embedding it manually in your index.html section for a simpler app.

Is there a way to import and use Google Web Fonts within a styled component?


Solution

  • Download the Google font file(WOFF or TTF) from here and use the below CSS

    /* open-sans-regular - latin */
    @font-face {
      font-family: 'Open Sans';
      font-style: normal;
      font-weight: 400;
      src: url('../fonts/open-sans-v29-latin-regular.eot'); /* IE9 Compat Modes */
      src: local(''),
           url('../fonts/open-sans-v29-latin-regular.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
           url('../fonts/open-sans-v29-latin-regular.woff2') format('woff2'), /* Super Modern Browsers */
           url('../fonts/open-sans-v29-latin-regular.woff') format('woff'), /* Modern Browsers */
           url('../fonts/open-sans-v29-latin-regular.ttf') format('truetype'), /* Safari, Android, iOS */
           url('../fonts/open-sans-v29-latin-regular.svg#OpenSans') format('svg'); /* Legacy iOS */
    }