# inline-critical-css

> Inline critical CSS in HTML

Latest version **2.0.0** (published 2020-01-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install inline-critical-css
pnpm add inline-critical-css
yarn add inline-critical-css
bun add inline-critical-css
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2020-01-27 |
| First published | 2017-07-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 13.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 28 |
| Maintainers | ahdinosaur, bret, goto-bus-stop, hughsk, jongacnik, s3ththompson, yoshuawuyts, zhouhancheng |
| Keywords | html, css, inline, critical |

## Links

- npm: https://www.npmjs.com/package/inline-critical-css
- Repository: https://github.com/stackcss/inline-critical-css
- Homepage: https://github.com/stackcss/inline-critical-css#readme
- Issues: https://github.com/stackcss/inline-critical-css/issues
- npm.io page: https://npm.io/package/inline-critical-css

## Dependencies (7)

- [css](https://npm.io/package/css.md) ^2.2.1
- [hstream](https://npm.io/package/hstream.md) ^2.0.0
- [through2](https://npm.io/package/through2.md) ^3.0.1
- [end-of-stream](https://npm.io/package/end-of-stream.md) ^1.4.0
- [extract-html-id](https://npm.io/package/extract-html-id.md) ^1.0.0
- [extract-html-tag](https://npm.io/package/extract-html-tag.md) ^1.0.1
- [extract-html-class](https://npm.io/package/extract-html-class.md) ^1.0.1

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2020-01-27
- 1.2.1 — 2018-03-28
- 1.2.0 — 2018-01-24
- 1.1.1 — 2018-01-08
- 1.1.0 — 2017-10-03
- 1.0.9 — 2017-09-22
- 1.0.8 — 2017-09-22
- 1.0.7 — 2017-09-22
- 1.0.6 — 2017-07-20
- 1.0.5 — 2017-07-19
- 1.0.4 — 2017-07-18
- 1.0.3 — 2017-07-09
- 1.0.2 — 2017-07-09
- 1.0.1 — 2017-07-09
- 1.0.0 — 2017-07-09

## README

# inline-critical-css [![stability][0]][1]
[![npm version][2]][3] [![build status][4]][5]
[![downloads][8]][9] [![js-standard-style][10]][11]

Stream that inlines critical CSS in HTML. Looks at the used CSS on a page and
only inlines the CSS that's used.

## Usage
```js
var inline = require('inline-critical-css')
var pump = require('pump')

var css = `
  .red { color: red }
`

var html = `
  <html>
    <head></head>
    <body class="red">Hello world</body>
  </html>
`

var stream = inline(css)
pump(stream, process.stdout)
stream.end(html)
```

## FAQ
### Why is this is a stream?
[hyperstream](https://github.com/substack/hyperstream) makes it easy to chain
HTML transforms together. I was too lazy to write my own parser + selector so
hence it being a stream. Also I use streams for this stuff anyway so it would
make a lot of sense to keep it as a stream.

### Why does it inline _all_ CSS used on a page?
Ideally we'd only inline the "above the fold" CSS, but that requires knowing
which tokens are "above the fold". This would require looking at a specific
viewport, and checking which tokens are used (e.g. headless chrome or similar).
We opted for a slightly simpler option, which hopefully works out well enough
for most cases.

### Why doesn't it inline my fancy selectors?
Inlining fancy selectors (e.g. `.foo:not(:first-child)`) is really hard to
determine statically if it's used. The best way to do so would be to launch a
headless chrome instance - but that requires a fair amount of compute
resources. So we don't. If you want that behavior, we recommend writing a
headless chrome module specifically for that (and let us know, we'd be
interested in that!)

## API
### `transformStream = inline(css)`
Create a transform stream that inlines critical CSS in HTML.

## See Also
- [substack/hyperstream](https://github.com/substack/hyperstream)
- [stackcss/extract-html-class](https://github.com/stackcss/extract-html-class)

## License
[MIT](https://tldrlegal.com/license/mit-license)

[0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square
[1]: https://nodejs.org/api/documentation.html#documentation_stability_index
[2]: https://img.shields.io/npm/v/inline-critical-css.svg?style=flat-square
[3]: https://npmjs.org/package/inline-critical-css
[4]: https://img.shields.io/travis/stackcss/inline-critical-css/master.svg?style=flat-square
[5]: https://travis-ci.org/stackcss/inline-critical-css
[6]: https://img.shields.io/codecov/c/github/stackcss/inline-critical-css/master.svg?style=flat-square
[7]: https://codecov.io/github/stackcss/inline-critical-css
[8]: http://img.shields.io/npm/dm/inline-critical-css.svg?style=flat-square
[9]: https://npmjs.org/package/inline-critical-css
[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[11]: https://github.com/feross/standard

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