Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use global css style sheet includes with GatsbyJS

Tags:

webpack

gatsby

By default, GatsbyJS is pushing the use of css modules and custom css-in-js libraries like glamorous and typography

Unfortunately, I am porting an existing site to GatsbyJS that uses traditional global style sheets. The effort to convert all the existing HTML-CSS instrumentation to CSS modules is a chore.

My current hack is to update the GatsbyJS html.js and add a

<link rel="stylesheet" type="text/css" href="/style.old.css" />

To the header. I have style.old.css living in /public this, however, gets wiped out when a build is run.

I was hoping there would be a plugin to support this but seems the Gatsby team is discouraging global CSS which is understandable for fresh projects.

I attempted to write a plugin to add this but found limited resources on how to write a "style plugin".

like image 896
kevzettler Avatar asked Oct 27 '17 17:10

kevzettler


People also ask

Can a component be styled with a global style sheet?

The important thing to remember is to place the GlobalStyle component as a sibling component to your main application component(s). And that's it! Global styling is now all set up with Styled Components.

How do you add global styles in The Great Gatsby?

The best way to add global styles is with a shared layout component. This layout component is used for things that are shared throughout the site, including styles, header components, and other common items. NOTE: This pattern is implemented by default in the default starter.

How do I use Global CSS in next JavaScript?

Adding a Global Stylesheet To add a stylesheet to your application, import the CSS file within pages/_app.js . Create a pages/_app.js file if not already present. Then, import the styles.css file. These styles ( styles.css ) will apply to all pages and components in your application.


1 Answers

You're close!

Instead of placing style.old.css in /public, place it in /layouts. Then, simply import it into /layouts/index.js like this:

import './style.old.css'

When you start a new gatsby project (gatsby new project-name), you'll notice an index.css file in the /layouts folder, which is imported into layouts/index.js by default. Just follow this example to add your own global stylesheets.

In general, you should not place anything in /public directly. If you'd like static files to end up in the /public folder, create a /static folder and place them there (Gatsby will copy any files in /static to /public for you).

like image 87
ooloth Avatar answered Oct 13 '22 17:10

ooloth