High-performance library for inlining CSS into HTML 'style' attributes
npm install @css-inline/css-inlinecss-inline is a high-performance library for inlining CSS into HTML 'style' attributes.
This library is designed for scenarios such as preparing HTML emails or embedding HTML into third-party web pages.
For instance, the library transforms HTML like this:
``html`
Big Text
into:
`html`
Big Text
- Uses reliable components from Mozilla's Servo project
- Inlines CSS from style and link tagsstyle
- Removes and link tags
- Resolves external stylesheets (including local files)
- Optionally caches external stylesheets
- Works on Linux, Windows, and macOS
- Supports HTML5 & CSS3
- Tested on Node.js 20 & 22.
If you'd like to try css-inline, you can check the WebAssembly-powered playground to see the results instantly.
Install with npm:
`shell`
npm i @css-inline/css-inline
`typescript
import { inline } from "@css-inline/css-inline";
var inlined = inline(
,
);
// Do something with the inlined HTML, e.g. send an email
`Note that
css-inline automatically adds missing html and body tags, so the output is a valid HTML document.Alternatively, you can inline CSS into an HTML fragment, preserving the original structure:
`javascript
import { inlineFragment } from "@css-inline/css-inline";var inlined = inlineFragment(
who am i
,
h1 { who am i
color: blue;
}
`
);
// HTML becomes this:
//
// Hello
//
//
//
//
- inlineStyleTags. Specifies whether to inline CSS from "style" tags. Default: truekeepStyleTags
- . Specifies whether to keep "style" tags after inlining. Default: falsekeepLinkTags
- . Specifies whether to keep "link" tags after inlining. Default: falsekeepAtRules
- . Specifies whether to keep "at-rules" (starting with @) after inlining. Default: falseminifyCss
- . Specifies whether to remove trailing semicolons and spaces between properties and values. Default: falsebaseUrl
- . The base URL used to resolve relative URLs. If you'd like to load stylesheets from your filesystem, use the file:// scheme. Default: nullloadRemoteStylesheets
- . Specifies whether remote stylesheets should be loaded. Default: truecache
- . Specifies caching options for external stylesheets (for example, {size: 5}). Default: nullextraCss
- . Extra CSS to be inlined. Default: nullpreallocateNodeCapacity
- . Advanced. Preallocates capacity for HTML nodes during parsing. This can improve performance when you have an estimate of the number of nodes in your HTML document. Default: 32removeInlinedSelectors
- . Specifies whether to remove selectors that were successfully inlined from