Critical extracts & inlines critical-path (above-the-fold) CSS from HTML
$ npm install --save critical
var critical = ;
Full blown example with available options:
Generate and minify critical-path CSS:
Generate, minify and inline critical-path CSS:
Generate and return output via callback:
Generate and return output via promise:
When your site is adaptive and you want to deliver critical CSS for multiple screen resolutions this is a useful option. note: (your final output will be minified as to eliminate duplicate rule inclusion)
This is a usefull option when you e.g. want to defer loading of webfonts or background images.
||Inline critical-path CSS using filamentgroup's loadCSS|
||Base directory in which the source and destination are to be written|
||HTML source to be operated against. This option takes precedence over the
||Location of the HTML source to be operated against|
||Location of where to save the output of an operation (will be relative to base if no absolute path is set)|
||Subfolder relative to base directory. Only relevant without src (if raw html is provided) or if the destination is outside base|
||Width of the target viewport|
||Height of the target viewport|
||An array of objects containing height and width. Takes precedence over
||Enable minification of generated critical-path CSS|
||Remove the inlined styles from any stylesheets referenced in the HTML. It generates new references based on extracted content so it's safe to use for multiple HTML files referencing the same stylesheet. Use with caution. Removing the critical CSS per page results in a unique async loaded CSS file for every page. Meaning you can't rely on cache across multiple pages|
||List of directories/urls where the inliner should start looking for assets|
||Sets a max file size (in bytes) for base64 inlined images|
||Sets a maximum timeout for the operation|
||Path to prepend CSS assets with. You must make this path absolute if you are going to be using critical in multiple target files in disparate directory depths. (eg. targeting both
||Force include CSS rules. See
||Ignore CSS rules. See
||Ignore options. See
$ npm install -g critical
critical works well with standard input.
$ cat test/fixture/index.html | critical --base test/fixture --inline > index.critical.html
You can also pass in the critical CSS file as an option.
$ critical test/fixture/index.html --base test/fixture > critical.css
var gulp = ;var gutil = ;var critical = stream;// Generate & Inline Critical-path CSSgulp;
For best performance, you may want to consider inlining the critical CSS directly into the HTML document. This eliminates additional roundtrips in the critical path and if done correctly can be used to deliver a “one roundtrip” critical path length where only the HTML is a blocking resource.
Why, yes!. Take a look at this Gulp project which demonstrates using Critical to generate and inline critical-path CSS. It also includes a mini-tutorial that walks through how to use it in a simple webapp.
The main differences between Critical and Penthouse, a module we use, are:
That said, if your site or app has a large number of styles or styles which are being dynamically injected into the DOM (sometimes common in Angular apps) I recommend using Penthouse directly. It will require you to supply styles upfront, but this may provide a higher level of accuracy if you find Critical isn't serving your needs.
Critical has been used on a number of production sites that have found it stable for everyday use. That said, we welcome you to try it out on your project and report bugs if you find them.
Of course. We appreciate all of our contributors and welcome contributions to improve the project further. If you're uncertain whether an addition should be made, feel free to open up an issue and we can discuss it.
Apache-2.0 © Addy Osmani