Grunt task to extract Above the Fold CSS from a URL
JavaScript CSS
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Build Status

Grunt task using critical-css to extract Above the Fold styles from URLs.

Getting Started

This plugin requires Grunt ~0.4.5

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-critical-css --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:


The "critical_css" task


In your project's Gruntfile, add a section named critical_css to the data object passed into grunt.initConfig().

  critical_css: {
    options: {
      width: 1200,
      height: 900,
    your_target: {
      url: 'http://localhost:3000/', // Parse this page.
      dest: 'dist/critical.css',     // Path to save critical css to.



Type: Integer Default value: 1200

The width of the viewport used in the browser. Used to determine what is "above the fold", i.e what is visible during rendering the page initially.


Type: Integer Default value: 900

The height of the viewport used in the browser. Used to determine what is "above the fold", i.e what is visible during rendering the page initially.


Type: Array Default value: []

An array of CSS selectors or basically any pattern. These are matched against every individual style declaration and if the patterns provided here match the style rule is discarded from the output.


Type: Array Default value: []

An array of host names to serve as a whitelist where CSS can originate from. Any CSSRuleList objects with parentStyleSheet.href not having this host name are excluded from the critical CSS.

This can be useful to exclude certain styles supplied by 3rd party widgets that are loaded asynchronously anyways.


Type: Boolean Default value: false

Controls whether non-external styles should be included. These are usually rules which are already inlined or are set by JavaScript. These are excluded by default.


Type: Boolean Default value: true

Controls console output from the headless browser should be added to the output. Useful for debugging purposes.


Type: Integer Default value: 819200

Sets the output buffer for the child process.

Usage Examples

Default Options

In this example, the default options are used to generate the critical CSS from a development server. The generated stylesheet is then saved to a file under the specified path.

  critical_css: {
    dist: {
      url: 'http://localhost:3000/'
      file: 'dist/critical.css'

Custom Options, multiple targets

In this example, custom options are used to fetch from two different URLs whilst sharing some custom options.

  options: {
    excludeSelectors: [
      'html, body, div' // Prevent the CSS reset from appearing inline.
    enabledOrigins: [
      'localhost'       // Only include locally hosted styles.
  desktop: {
    options: {
      width: 1200,
      height: 760,
    url: ''
    file: 'dist/critical-desktop.css'
  mobile: {
    options: {
      width: 400,
      height: 800,
    url: ''
    file: 'dist/critical-mobile.css'

Pro tip: You can add the grunt-inline plugin to your build pipeline for embedding the resulting critical stylesheets in your html file or template.


In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.


  • 0.1.1 - Initial release


Project led and maintained by Attila Beregszaszi

Development sponsored by Dennis Publishing and Front Seed Labs