If you're using styled-components version 3.4.10, then you have to use injectGlobal instead of createGlobalStyle, because createGlobalStyle was only release in v4 of styled-components. Check it out: [Deprecated] injectGlobal
So, in order for your code to work, you have to change a few things:
import { injectGlobal } from 'styled-components';
const GlobalStyle = injectGlobal`
html {
height: 100%
}
* {
padding: 0;
margin: 0
}
`
export default GlobalStyle
And in your index.ts
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import GlobalStyle from './theme/globalStyles'
ReactDOM.render(
<React.StrictMode>
<GlobalStyle /> // this comes first
<App />
</React.StrictMode>,
document.getElementById('root')
)
Answer from mindmaster on Stack Overflowstyled-components
styled-components.com › docs › api
styled-components
Place it at the top of your React tree and the global styles will be injected when the component is "rendered". import { createGlobalStyle } from 'styled-components' const GlobalStyle = createGlobalStyle<{ $whiteColor?: boolean; }>` body { color: ${props => (props.$whiteColor ?
Scalablecss
scalablecss.com › styled-components-global-styles
How to Create Global Styles with Styled Components - Scalable CSS
June 2, 2020 - Now we need to use that file to apply the global styles to the application. Find your component which is at the top of your React tree. In many react applications, that’s typically the App.js file. Here, import your GlobalStyle component and place it inside the the top of your React tree:
Videos
01:39
How to Properly Use createGlobalStyle with Multiple Files in Styled ...
03:09
React Styled Components - 10 - Global Styles - YouTube
05:10
lecture 7 adding global styles using createGlobalStyle + adding ...
02:18
Learn Styled Components | Global Styles with createGlobalStyle ...
06:19
React Styled Components - Creating Global Styles - YouTube
Stack Overflow
stackoverflow.com › questions › 69027693 › alternative-to-createglobalstyle-from-styled-components-that-can-also-be-importa
reactjs - Alternative to createGlobalStyle from styled-components that can also be importable - Stack Overflow
This seems like it would be a good fit for your situation, as it would be relatively easy to transition from the existing structure using createGlobalStyle to one that uses this. ... import { injectGlobal } from 'styled-components'; injectGlobal` @font-face { font-family: 'Name'; font-style: normal; font-weight: 400; font-display: swap; src: url(https://fonts.gstatic.com/s/name/v12/iJWZBXyIfDnIV5PNhY1KTN7Z-Yh-B4iFU0U1Z4Y.woff2) format('woff2'); } // more fonts..
GitHub
github.com › styled-components › styled-components › issues › 2670
v5 createGlobalStyles does not work on production with 2 global styles · Issue #2670 · styled-components/styled-components
July 5, 2019 - import { createGlobalStyle } from 'styled-components/macro'; const GlobalStyling = createGlobalStyle` html { height: 100%; } :global(#root) { min-height: 100%; margin: 0; padding: 0; display: flex; flex-direction: column; flex: 1; } body { ...
Author chriserickson
GitHub
github.com › styled-components › styled-components › issues › 3038
@import url(...) inside of createGlobalStyle causes styles other than global disappear in built version · Issue #3038 · styled-components/styled-components
February 27, 2020 - createGlobalStyle` @import url('https://fonts.googleapis.com/css?family=Ubuntu&display=swap&subset=latin-ext'); `
Author arkadiuszbachorski
DEV Community
dev.to › mochafreddo › improving-css-loading-in-react-applications-avoiding-import-in-createglobalstyle-4d9p
Improving CSS Loading in React Applications: Avoiding `@import` in `createGlobalStyle` - DEV Community
June 17, 2024 - import { createGlobalStyle } from 'styled-components'; const GlobalStyle = createGlobalStyle` html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video { margin: 0; pa
Dilshankelsen
dilshankelsen.com › create-global-styles-with-styled-components
How To Create Global Styles With Styled Components | Dilshan Kelsen
June 24, 2021 - createGlobalStyle is a web-only API, i.e. it won't work in React Native. Create a file that contains your global styles, e.g. global.css.js. JSXCopy · import { createGlobalStyle } from 'styled-components' export default createGlobalStyle` body { margin: 0; padding: 0; color: ${props => ...
Storybook
storybook.js.org › recipes › styled-components
Styled Components | Storybook recipes
// .storybook/preview.jsx import { withThemeFromJSXProvider } from '@storybook/addon-themes'; import { createGlobalStyle } from 'styled-components'; const GlobalStyles = createGlobalStyle` body { font-family: "Nunito Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } `; export const decorators = [ withThemeFromJSXProvider({ GlobalStyles, // Adds your GlobalStyle component to all stories }), ];
Js
goober.js.org › api › createGlobalStyles
createGlobalStyles | goober
To define your global styles you need to create a GlobalStyles component and use it as part of your tree. The createGlobalStyles is available at goober/global addon. import { createGlobalStyles } from 'goober/global';
GitHub
github.com › styled-components › styled-components › blob › main › packages › styled-components › src › constructors › createGlobalStyle.ts
styled-components/packages/styled-components/src/constructors/createGlobalStyle.ts at main · styled-components/styled-components
import css from './css'; · declare const __SERVER__: boolean; · export default function createGlobalStyle<Props extends object>( strings: Styles<Props>, ...interpolations: Array<Interpolation<Props>> ) { const rules = css<Props>(strings, ...
Author styled-components
GitHub
gist.github.com › chrislopresto › 490ef5692621177ac71c424543702bbb
styled-components v4 + createGlobalStyle + TypeScript · GitHub
import * as styledComponents from 'styled-components/native'; import {ReactNativeThemedStyledComponentsModule} from 'styled-components/native'; import {Theme} from '../themes'; const { default: styled, css, ThemeProvider, } = styledComponents as ReactNativeThemedStyledComponentsModule<Theme>; export {css, ThemeProvider}; export default styled;
GitHub
github.com › styled-components › styled-components › issues › 2526
Document or clearer support for use of @import in createGlobalStyle · Issue #2526 · styled-components/styled-components
May 2, 2019 - The styles created with createGlobalStyle are visible by hoisting imports to top-level OR the css specification is followed in this case and an error is shown to advise the user @import will not be supported within css element selectors when using createGlobalStyle.
Author sidhuko
Top answer 1 of 2
4
You can do it this way:
import { createGlobalStyle } from "styled-components";
import { Fonts } from "./fonts";
import { FontFamily } from "./fontFamily";
const GlobalStyle = createGlobalStyle`
${Fonts}
${FontFamily}
`;
2 of 2
1
If you have a lot of global styles like we did, you can use styled-components css method to leverage styled-components (css) IDE syntax highlighting & linting you can also do the following:
import React from 'react'
import { createGlobalStyle, css } from 'styled-components'
const Reset = css`
* {
box-sizing: border-box;
}
`
const Accessibility = css`
.hidden {
display: none !important;
visibility: hidden;
}
`
const BaseStyles = createGlobalStyle`
${Reset};
${Accessibility};
`
export const GlobalStyles = () => (
<>
<BaseStyles />
</>
)
Import GlobalStyles and render as sibling to
{children}