npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@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-inject
yarn add @visulima/css-style-inject
pnpm add @visulima/css-style-inject

Example

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 `&gt;` 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. 0 inserts as the first child. A negative number counts back from the end and resolves to children.length + insertAt + 1, so -1 appends at the end (after the last child) and -2 inserts 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