How to use CSS-in-JS libraries
Source URL: https://nextjs.org/docs/pages/guides/css-in-js
How to use CSS-in-JS libraries
Section titled “How to use CSS-in-JS libraries”It’s possible to use any existing CSS-in-JS solution. The simplest one is inline styles:
function HiThere() { return <p style={{ color: 'red' }}>hi there</p>}
export default HiThereWe bundle styled-jsx to provide support for isolated scoped CSS. The aim is to support “shadow CSS” similar to Web Components, which unfortunately do not support server-rendering and are JS-only.
See the above examples for other popular CSS-in-JS solutions (like Styled Components).
A component using styled-jsx looks like this:
function HelloWorld() { return ( <div> Hello world <p>scoped!</p> <style jsx>{` p { color: blue; } div { background: red; } @media (max-width: 600px) { div { background: blue; } } `}</style> <style global jsx>{` body { background: black; } `}</style> </div> )}
export default HelloWorldPlease see the styled-jsx documentation for more examples.