# extract-css

> Extract the CSS from an HTML document.

Latest version **3.0.2** (published 2024-12-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install extract-css
pnpm add extract-css
yarn add extract-css
bun add extract-css
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.2 |
| Published | 2024-12-27 |
| First published | 2015-01-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 5.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 438 |
| Author | Jonathan Kemp |
| Maintainers | jonkemp |
| Keywords | css |

## Links

- npm: https://www.npmjs.com/package/extract-css
- Repository: https://github.com/jonkemp/inline-css.git#master
- Homepage: https://github.com/jonkemp/inline-css/tree/master/packages/extract-css
- Issues: https://github.com/jonkemp/inline-css/issues
- npm.io page: https://npm.io/package/extract-css

## Dependencies (4)

- [async](https://npm.io/package/async.md) ^3.2.6
- [style-data](https://npm.io/package/style-data.md) ^2.0.1
- [href-content](https://npm.io/package/href-content.md) ^2.0.3
- [list-stylesheets](https://npm.io/package/list-stylesheets.md) ^2.0.1

## Alternatives

- [style-dictionary](https://npm.io/package/style-dictionary.md) — 2.0M weekly downloads
- [postcss-merge-idents](https://npm.io/package/postcss-merge-idents.md) — 1.7M weekly downloads
- [@fontsource/noto-sans](https://npm.io/package/@fontsource/noto-sans.md) — 93.0K weekly downloads
- [uglifycss](https://npm.io/package/uglifycss.md) — 71.6K weekly downloads
- [mat4-interpolate](https://npm.io/package/mat4-interpolate.md) — 23.3K weekly downloads

## Recent versions

- 3.0.2 (latest) — 2024-12-27
- 3.0.1 — 2023-02-08
- 3.0.0 — 2022-07-08
- 2.0.1 — 2022-01-17
- 2.0.0 — 2020-10-24
- 1.5.5 — 2020-02-21
- 1.5.4 — 2020-01-26
- 1.5.3 — 2020-01-26
- 1.5.2 — 2020-01-25
- 1.5.1 — 2020-01-25
- 1.5.0 — 2020-01-19
- 1.4.0 — 2020-01-19
- 1.3.0 — 2020-01-18
- 1.2.4 — 2019-10-06
- 1.2.3 — 2019-05-31
- … 11 more at https://npm.io/package/extract-css/versions

## README

# extract-css

[![NPM](https://nodei.co/npm/extract-css.png?downloads=true)](https://nodei.co/npm/extract-css/)

> Extract the CSS from an HTML document.

## Install

Install with [npm](https://npmjs.org/package/extract-css)

```
npm install --save extract-css
```

## Usage

```js
var extractCss = require('extract-css');
var options = {
      url: './',
      applyStyleTags: true,
      removeStyleTags: true,
      applyLinkTags: true,
      removeLinkTags: true,
      preserveMediaQueries: false
  };

extractCss(document, options, function (err, html, css) {
    console.log(html);
    console.log(css);
});
```

## API

### extractCss(html, options, callback)

#### options.applyStyleTags

Type: `Boolean`  

Whether to inline styles in `<style></style>`.


#### options.applyLinkTags

Type: `Boolean`  

Whether to resolve `<link rel="stylesheet">` tags and inline the resulting styles.


#### options.removeStyleTags

Type: `Boolean`  

Whether to remove the original `<style></style>` tags after (possibly) inlining the css from them.


#### options.removeLinkTags

Type: `Boolean`  

Whether to remove the original `<link rel="stylesheet">` tags after (possibly) inlining the css from them.


#### options.url

Type: `String`  

How to resolve hrefs. Required.

#### options.preserveMediaQueries

Type: `Boolean`  

Preserves all media queries (and contained styles) within `<style></style>` tags as a refinement when `removeStyleTags` is `true`. Other styles are removed.

#### options.codeBlocks

Type: `Object`  
Default: `{ EJS: { start: '<%', end: '%>' }, HBS: { start: '{{', end: '}}' } }`

An object where each value has a `start` and `end` to specify fenced code blocks that should be ignored during parsing. For example, Handlebars (hbs) templates are `HBS: {start: '{{', end: '}}'}`. Note that `codeBlocks` is a dictionary which can contain many different code blocks, so don't do `codeBlocks: {...}` do `codeBlocks.myBlock = {...}`.

### Special markup

#### data-embed

When a data-embed attribute is present on a <style></style> tag, extract-css will not inline the styles and will not remove the <style></style> tags.

This can be used to embed email client support hacks that rely on css selectors into your email templates.

## Credit

The code for this module was originally taken from the [Juice](https://github.com/Automattic/juice) library.

## License

MIT © [Jonathan Kemp](http://jonkemp.com)

---
_Source: https://npm.io/package/extract-css · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
