
Security News
CVE Volume Surges Past 48,000 in 2025 as WordPress Plugin Ecosystem Drives Growth
CVE disclosures hit a record 48,185 in 2025, driven largely by vulnerabilities in third-party WordPress plugins.
@fatfisz/styled-jsx
Advanced tools
Full, scoped and component-friendly CSS support for JSX (rendered on the server or the client).
Firstly, install the package:
$ npm install --save styled-jsx
Next, add styled-jsx/babel to plugins in your babel configuration:
{
"plugins": [
"styled-jsx/babel"
]
}
Now add <style jsx> to your code and fill it with CSS:
export default () => (
<div>
<p>only this paragraph will get the style :)</p>
{ /* you can include <Component />s here that include
other <p>s that don't get unexpected styles! */ }
<style jsx>{`
p {
color: red;
}
`}</style>
</div>
)
<style scoped>, but the styles get injected only once per componentThe example above transpiles to the following:
import _JSXStyle from 'styled-jsx/style'
export default () => (
<div data-jsx='cn2o3j'>
<p data-jsx='cn2o3j'>only this paragraph will get the style :)</p>
<_JSXStyle styleId='cn2o3j' css={`p[data-jsx=cn2o3j] {color: red;}`} />
</div>
)
Data attributes give us style encapsulation and _JSXStyle is heavily optimized for:
Notice that the parent <div> above also gets a data-jsx atribute. We do this so that
you can target the "root" element, in the same manner that
:host works with Shadow DOM.
If you want to target only the host, we suggest you use a class:
export default () => (
<div className="root">
<style jsx>{`
.root {
color: green;
}
`}</style>
</div>
)
To skip scoping entirely, you can make the global-ness of your styles explicit by adding global.
export default () => (
<div>
<style jsx global>{`
body {
background: red
}
`}</style>
</div>
)
The advantage of using this over <style> is twofold: no need
to use dangerouslySetInnerHTML to avoid escaping issues with CSS
and take advantage of styled-jsx's de-duping system to avoid
the global styles being inserted multiple times.
Sometimes it's useful to skip prefixing. We support :global(),
inspired by css-modules.
This is very useful in order to, for example, generate an unprefixed class that
you can pass to 3rd-party components. For example, to style
react-select which supports passing a custom class via optionClassName:
import Select from 'react-select'
export default () => (
<div>
<Select optionClassName="react-select" />
<style jsx>{`
/* "div" will be prefixed, but ".react-select" won't */
div :global(.react-select) {
color: red
}
`}</style>
</div>
)
className togglingTo make a component's visual representation customizable from the outside world, there are two options. The first one is to pass properties that toggle class names.
const Button = (props) => (
<button className={ 'large' in props && 'large' }>
{ props.children }
<style jsx>{`
button {
padding: 20px;
background: #eee;
color: #999
}
.large {
padding: 50px
}
`}</style>
</button>
)
Then you would use this component as either <Button>Hi</Button> or <Button large>Big</Button>.
styleImagine that you wanted to make the padding in the button above completely customizable. You can override the CSS you configure via inline-styles:
const Button = ({ padding, children }) => (
<button style={{ padding }}>
{ children }
<style jsx>{`
button {
padding: 20px;
background: #eee;
color: #999
}
`}</style>
</button>
)
In this example, the padding defaults to the one set in <style> (20), but the user can pass a custom one via <Button padding={30}>.
It is possible to use constants like so:
import { colors, spacing } from '../theme'
import { invertColor } from '../theme/utils'
const Button = ({ children }) => (
<button>
{ children }
<style jsx>{`
button {
padding: ${ spacing.medium };
background: ${ colors.primary };
color: ${ invertColor(colors.primary) };
}
`}</style>
</button>
)
N.B. Only constants defined outside of the component scope are allowed here.
If you want to use or toggle dynamic values depending on the component state or props then we recommend to use one of the techniques from the Dynamic styles section
styled-jsx/serverThe main export flushes your styles to an array of React.Element:
import React from 'react'
import ReactDOM from 'react-dom/server'
import flush from 'styled-jsx/server'
import App from './app'
export default (req, res) => {
const app = ReactDOM.renderToString(<App />)
const styles = flush()
const html = ReactDOM.renderToStaticMarkup(<html>
<head>{ styles }</head>
<body>
<div id="root" dangerouslySetInnerHTML={{__html: app}} />
</body>
</html>)
res.end('<!doctype html>' + html)
}
We also expose flushToHTML to return generated HTML:
import React from 'react'
import ReactDOM from 'react-dom/server'
import { flushToHTML } from 'styled-jsx/server'
import App from './app'
export default (req, res) => {
const app = ReactDOM.renderToString(<App />)
const styles = flushToHTML()
const html = `<!doctype html>
<html>
<head>${styles}</head>
<body>
<div id="root">${app}</div>
</body>
</html>`
res.end(html)
}
It's paramount that you use one of these two functions so that the generated styles can be diffed when the client loads and duplicate styles are avoided.
When working with template literals a common drawback is missing syntax highlighting. The following editors currently have support for highlighting CSS inside <style jsx> elements.
If you have a solution for an editor not on the list please open a PR and let us now.
The language-babel package for the Atom editor has an option to extend the grammar for JavaScript tagged template literals.
After installing the package add the code below to the appropriate settings entry. In a few moments you should be blessed with proper CSS syntax highlighting. (source)
"(?<=<style jsx>{)|(?<=<style jsx global>{)":source.css.styled

The IDE let you inject any language in place with Inject language or reference in an Intention Actions (default alt+enter). Simply perform the action in the string template and select CSS. You get full CSS highlighting and autocompletion and it will last until you close the IDE.
If you're using Emmet you can add the following snippet to ~/emmet/snippets-styledjsx.json This will allow you to expand style-jsx to a styled-jsx block.
{
"html": {
"snippets": {
"style-jsx": "<style jsx>{`\n\t$1\n`}</style>"
}
}
}
Launch VS Code Quick Open (⌘+P), paste the following command, and press enter.
ext install vscode-styled-jsx
By now, this extension doesn't support autocomplete. However, you can install ES6 Template Literal Editor extension to edit styles in another pane, and you will get full feature of css language service provided by VS Code.
murmurhash2 (minimal and fast hashing) and an efficient style injection logic.FAQs
Full CSS support for JSX without compromises
We found that @fatfisz/styled-jsx demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
CVE disclosures hit a record 48,185 in 2025, driven largely by vulnerabilities in third-party WordPress plugins.

Security News
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.

Security News
Tailwind Labs laid off 75% of its engineering team after revenue dropped 80%, as LLMs redirect traffic away from documentation where developers discover paid products.