@visulima/css-style-inject
v1.1.1
Published
Inject style tag to document head.
Readme
[![typescript-image]][typescript-url] [![npm-image]][npm-url] [![license-image]][license-url]
Install
npm install @visulima/css-style-injectyarn add @visulima/css-style-injectpnpm add @visulima/css-style-injectExample
import { cssStyleInject } from "@visulima/css-style-inject";
const css = `
body {
margin: 0;
}
`;
cssStyleInject(css, options);Usage with Next.js (SSR)
If using a library that uses @visulima/css-style-inject for CSS modules in Next.js,
you need to inject styles during SSR, here's an example:
// file: pages/_document.js
import React from "react";
import { SSR_INJECT_ID } from "@visulima/css-style-inject";
const SSRInjectStyles = () => {
if (!globalThis[SSR_INJECT_ID]) return null;
return (
<>
{globalThis[SSR_INJECT_ID].map((module) => (
// React escapes text children, so `<style>{module.css}</style>` would
// turn `>` into `>` and break the CSS. Use dangerouslySetInnerHTML
// to emit the CSS verbatim.
//
// SECURITY: the CSS is injected as raw HTML. Only do this with CSS you
// trust, and guard against a literal `</style>` sequence in the CSS
// (e.g. reject/escape it) — otherwise it closes the tag early and
// becomes an HTML-injection vector.
<style id={module.id} key={module.id} dangerouslySetInnerHTML={{ __html: module.css }} />
))}
</>
);
};
const Document = (props) => {
const { locale } = props;
return (
<Html lang={locale}>
<Head>
{/* Inject styles during ssr */}
<SSRInjectStyles />
{/* ... */}
</Head>
<body>{/* ... */}</body>
</Html>
);
};
export default Document;Options
id
Type: string
Default: undefined
Unique identifier for the style tag. Prevents duplicate injection: in the browser, if an element with the same id already exists in the document, injection is skipped; during SSR, a module with an already-seen id is not stored again.
insertAt
Type: number | 'first' | 'last' | { before: string }
Default: 'last'
Where to insert the style tag:
number: Insert at a specific index.0inserts as the first child. A negative number counts back from the end and resolves tochildren.length + insertAt + 1, so-1appends at the end (after the last child) and-2inserts before the last child. Out-of-range positive indices append at the end.'first': Insert as first child'last': Insert as last child (default){ before: 'selector' }: Insert before the element matching the CSS selector (searched within the container). If no element matches, the style tag is appended at the end instead. An invalid selector throws.
singleTag
Type: boolean
Default: false
Whether to reuse a single style tag for multiple injections with the same configuration.
The cached tag is keyed by the resolved container element and the insertAt value. Because the tag is created only on the first matching call, the attributes, id, and nonce of later calls that reuse it are ignored — only the CSS of later calls is appended to the existing tag.
container
Type: string
Default: undefined
CSS selector for the container element. Defaults to head if not specified. If the selector matches no element, an error is thrown (the error message includes the selector).
attributes
Type: Record<string, string>
Default: undefined
Additional attributes to set on the style tag.
Reserved keys are dropped and cannot be set through this map: id, type, and nonce (these are controlled by the dedicated id / nonce options), as well as any event-handler attribute matching on* (e.g. onload, onclick) — rejected so the map can never create an executable handler.
nonce
Type: string
Default: undefined
Nonce value for CSP (Content Security Policy) compliance.
Supported Node.js Versions
This library supports the following Node.js versions:
- 22.x (>= 22.22.2)
- 24.x (>= 24.10.0)
Contributing
If you would like to help take a look at the list of issues and check our Contributing guidelines.
Note: please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
Credits
License
The visulima css-style-inject is open-sourced software licensed under the MIT
