Since you're using the ES6 import syntax you may use the same syntax to import your stylesheet
import './App.css'
Also, you can wrap your class with :global to switch to the global scope (this mean CSS Module won't modulify it, eg: adding a random id next to it)
:global(.myclass) {
background-color: red;
}
Answer from felixyadomi on Stack OverflowSince you're using the ES6 import syntax you may use the same syntax to import your stylesheet
import './App.css'
Also, you can wrap your class with :global to switch to the global scope (this mean CSS Module won't modulify it, eg: adding a random id next to it)
:global(.myclass) {
background-color: red;
}
This can be done by simply adding:
require('./App.css');
(thanks @elmeister who correctly answered this question.)
How to Create global styles with Styled components in react and next js and is it better to use combination of css files and styled component?
Global styles sheet for CSS modules?
Where to store global css variables?
reactjs - Should global css be put in each component or the root component (webpack entry file)? - Stack Overflow
Videos
Hi,
TLDR: Code at the end to see how I want to style my app. Doable approach?
I am very new to react and now I need styling. I have seen a lot of ways how people style their react app for instance:
css modules
styled components
tailwind ulitiy first
and so on...
The thing is that I dont like all of these solutions (except tailwind but I would use that on top of my styles). I dont see a reason why I should bloat my JS components with styling at all.
So my approach would be like this:
My react components just set class names like class="MyComp__wrap". Then I would style that selector "MyComp__wrap" in an external css/scss file. In my index.html I would just include that css file very early, even before the app is loaded. Just like this: "<link rel="stylesheet" href="/css/index.css">" Almost the same approach would be if I include this css file in the first react app js file (right before "ReactDOM.createRoot") like this "import './css/.index.css';" - But I dont see the reason why I should load the CSS from the react app if I can just do it in the index.html
I think its a super easy approach which does not reinvent the wheel. The components are not bloatet with styles only classNames. Furthermore it reduces complexity because the app does not even know that styling is applied. Classes wouldnt clash because of the nameing schema BEM.
I like this version a lot but of course I dont want to go down a road which is completely against all standards. What do you think?
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="/css/index.css">
<title>React App</title>
</head>
<body>
<div id="root"></div>
<script src="/js/react-app-build.js"></script>
</body>
</html>index.js
or I would import the /css/index.css here (but I dont see a reason to do that at the moment therefore I would just stick to the index.html solution):
import './css/index.css'
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<App />
);SomeReactComp.js
const SomeReactComp = (props) => {
return (
<div className='SomeReactComp__wrap'></div>
);
}
export default SomeReactComp;css/index.css
.SomeReactComp__wrap {
backrgound: grey;
}Relatively new react dev here. I am using css modules in one of my projects. I am familiar basically with how it works, but I have a few questions when it comes to reusable code. If we have certain styles that are reused (buttons, card/container styling, h1/h2/h3... etc.), would we create a global style sheet with all of those stylings and include it in the App.js? What about if we have multiple variants of these: for example, a small button, a large button, etc? Would we have multiple classes for each of those variants in the global style sheet as well, or would we maybe have a separate style sheet called like buttons.module.css that we could include in our different components and import styles from? I guess in general I'm just confused about how to organize the styling of an entire app with css modules. If anyone has a link to a well architected, large code base that uses CSS modules that would be very helpful also, thanks!
Hey,
I set up my react project to support sass following this article, which worked out fine (I configured the webpack part slightly different though).
Since I will use a set of different colors throughout my application, I would like to store these into variables, which I can access from my components for example. Since I don't want to import my global.scss file everytime into every components .scss, I would like to somehow set the variables global, making them accessible without imports.
How could I achieve this?
I'd import your font.css file when and where you use it (but not exactly the way you suggest, see below) and not just in the root component. I suggest this because when and if you decide to code split, you would only want that CSS to exist in the bundle that uses it.
If the import is in your root component, you might remove all components that are using the fa fa-bandcamp classes but your import remains in the root (because you forget it's there and not alongside your component) and you'd be bundling in CSS that is not even in the chunk that uses it.
On the contrary though, when importing at the component level you could also end up in a situation where you use those classes and forget to import that font.css because ANOTHER component has imported the global CSS already. It looks like it works but if you code split you might find that your chunk does not have the right font because the CSS import is in another chunk. In this case importing it in the root would have solved your issue!
What I would do:
I would argue that any global css is bad and you should be using something like CSS modules. So I'd go one step further and create a <Text/> component that is something like:
import React from 'react'
import '../font.css'
export default ({ className, children, tagName: TagName }) => <TagName className={`fa fa-bandcamp ${className}`>{ children }</TagName>;
Now you can use the <Text tagName="span">Hey!</Text> in all your components safely because:
- You no longer have to import the CSS all the time.
- If you code split, or remove all
<Text/>you won't be left with a bundle that contains unused CSS imports in the root that you forgot about. - It's not possible to use those classes and forget to import the CSS.
- Everything is nice and encapsulated in a modular way and your bundles are as efficient as possible.
I wouldn't employ this kind of strategy for something like a reset.css though. Obviously.
TL;DR Summary
Root level - Potential inefficient code splitting. Harder to maintain as CSS does not live along side the component that uses it.
Individual component level - Pain to import all the time. Fragile as you can end up using a class that doesn't exist in a chunk if forgetting to import the global CSS.
"Text" component - Awesome. Just make sure everybody uses the fa classes via this component and everything is golden. Modular. Easy to maintain. Robust.
Adding css in global space is easy and works just out of the box.
With use of webpack you can easily import css file in each component file respectively as style!css!sass for your .scss imports if you need it.
It allows you to simply import and use the styles within your React components in the following manner:
import styles from './mycomponent.scss';
export default props => <button className={styles.mycomponent.button} />
Here are the respective loaders:
style-loader
css-loader
sass-loader
The style-loader will handle hot-loading during development and bundling the outputted styles (these get compiled into your component module) at production build time.
I recommend using the modules option for the css-loader so that you ensure your CSS is correctly scoped and will never clash with external code.