2

I'm having problems getting css background images showing when using webpack.

I have the following css class:

.fb {
  display: block;
  width:30px;
  height: 30px;
  background-color: red;
  background-image: url('../images/icons/facebook.png');
}

Usage (Using React so className not class):

<div className="fb"></div>

Below is an image of my 'web_build' folder where webpack bundles all my files into. Highlighted is the culprit image.

build folder

The following is the bundles SCSS files which I see in the Network tab of chrome dev tools. No images files show on the 'Img' tab.

.fb {
  display: block;
  width: 30px;
  height: 30px;
  background-color: red;
  background-image: url(1b725f86d1e04faadfad0cda2ac6ee89.png); 
}

All I see rendered is a 30x30px red square.

enter image description here

NOTES:

  • If I directly reference the image using an <img> tag, the image shows.
  • I'm using webpack-dev-server
  • I'm using image-webpack-loader with the following configuration
{
    test: /\.(jpe?g|png|gif|svg|ttf|eot|svg|woff(2)?)$/i,
    loaders: [
        'file?hash=sha512&digest=hex&name=[hash].[ext]',
        'image-webpack?bypassOnDebug&optimizationLevel=7&interlaced=false'
    ]
  },

Let me know if any other information is needed.

Thanks.

2 Answers 2

2

After some more searching around it turns out that including sourceMap in my CSS/SASS bundling breaks relative image URLs in CSS.

Solution: Turn sourcemaps off or specify a fully qualified publicPath URL.

The following worked for me.

publicPath: 'http://localhost:8080'

More information here https://github.com/webpack/style-loader/issues/55

Sign up to request clarification or add additional context in comments.

Comments

0

The image-webpack-loader only compresses the images using imagemin. Use url-loader or file-loader to load the file into your css.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.