criticalCSS icon indicating copy to clipboard operation
criticalCSS copied to clipboard

Relative paths to background images in CSS

Open malcomio opened this issue 9 years ago • 3 comments

If the original CSS contains a relative path to an image, the output CSS will keep that relative path. Once the CSS is put inline, it's likely to generate a 404.

This is quite common in the case of themes for content management systems, e.g. in Drupal, the CSS is likely to be at /sites/all/themes/themename/css, or in Wordpress it would be in /wp-content/themes/themename/css, so it's common for the image path to be ../images/image.png

I guess a workaround would be to author the original CSS to include the full path, but that makes the code less portable.

malcomio avatar Nov 19 '15 18:11 malcomio

+1. Had the same problem recently. It would be great if there was a way to pass an optional path to criticalCSS, that replaces the relative path.

Fetten avatar Dec 18 '15 13:12 Fetten

I've run into the same problem and fixed it by adding a simple string replace output = output.replace("images/", "wp-content/themes/ms-theme/images/");

BUT: I would also love to see an exclude-option.

schneyra avatar Mar 20 '16 12:03 schneyra

I faced the same problem due to Drupal themes.

I solved this problem using grunt-criticalcss in conjunction with grunt-contrib-cssmin. cssmin allows you to set a root path which will rebase paths to be absolute.

clean-css could be an option if you're not using grunt.

andrewcuthill avatar Apr 08 '16 03:04 andrewcuthill