To create global styles we can use createGlobalStyle function. For next.js project, you can do that in _app.js or _app.tsx file, depending on whether you are using typescript.
import type { AppProps } from "next/app";
import { createGlobalStyle } from "styled-components";
import Header from "@/components/Header";
const GlobalStyles = createGlobalStyle`
html,
body {
padding: 0;
margin: 0;
}
a {
color: inherit;
text-decoration: none;
}
* {
box-sizing: border-box;
}
`;
export default function MyApp({ Component, pageProps }: AppProps) {
return (
<>
<GlobalStyles />
<Header></Header>
<Component {...pageProps} />
</>
);
}
Of course, if you have more global styles you can export the GlobalStyles from other file, say globalStyle.js.
But then there is nothing out there that stops you from using CSS or SCSS for your global styles. You can always import the .css or .scss or any other supported CSS preprocessor file types as you would normally do.
import '@/styles/global.scss'
And this would work perfectly fine with your Styled Components as well. Though there is a little advantage you get by using createGlobalStyle. You can use all SCSS features without having to add SCSS dependency in your project. And a little drawback is that the auto-complete features of the ide don't work as well with styled-components as they do with .css and .scss files.
Do you guys create global Styled Components and just import them in your components/pages?
Request: use :global or :local similar to css-modules
What’re the benefits of using styled components or css-in-js over importing a global css file?
I really like that styled components lets me have each bit of styling attached to specific components. I find it easier to reason about components with this as opposed to having to reference separate CSS files.
I think if you haven't used it before then it's completely understandable that it would seem counterintuitive.
More on reddit.comCreateGlobalStyles Rendered After Component Styles
Videos
Do you create for an example <Container> in some folder and just import he <Container> in your react component and use it with some props?
<Input> <Container width="100%" color="red"> hello text
</Container>
</Input>
Or do you redifne Container always in each of the components you would want to use?
Reading through the Next.js docs doesn’t really mention any performance perks. Only benefit they mention really is that you don’t have to worry about class naming conflicts.
Aside from that, is using a global css file bad practice?
It feels more natural assigning class names and ID names then referencing them via a global style sheet that’s imported once rather than separating out the styling for each component.
What am I missing?
I really like that styled components lets me have each bit of styling attached to specific components. I find it easier to reason about components with this as opposed to having to reference separate CSS files.
I think if you haven't used it before then it's completely understandable that it would seem counterintuitive.
My personal favourite styling option (especially with Next) is Tailwind.
I find it to be the best of both worlds. I did like styled-components when I first tried it, but I realized it just creates a lot of extra lines of code, and forces me to spend a bunch of time coming up with names for components. Container, ContentContainer, ContentContainerWrapper, etc.
And when I spend so much time writing the same things over and over:
display: flex;
align-items: center;
justify-content: center;
it makes way more sense (to me) to just extract those common styles to a utility class and just write:
<div className="flex items-center justify-center" />
instead of:
const WrapperDiv = styled.div`
display: flex;
align-items: center;
justify-content: center;
`;
...
<WrapperDiv />
And that's not even to mention the life-changing other things Tailwind can do!
-
breakpoint utility classes,
flex md:flex-row lg:flex-col -
font sizes / spacing / color token management:
text-sm text-green-100 p-5 m-2
And all super easily customizable to fit a design system in the Tailwind config file at the root of the repo.
Anyway, just wanted to mention Tailwind in this discussion because I've been obsessed with Tailwind + Next lately. I've never felt so productive as a frontend developer as when I'm using this stack.
But for when you do need more custom stuff that wouldn't be covered by Tailwind classes (which is a lot less than I thought it would be), I still haven't figured out my favourite option there. The css prop method is pretty cool though, and I like that it keeps your styles "with" the component.