1

I am trying to add a background image to a control in a ReactJS project, using css and it's url functionality.

.jumbotron {
    background-image: url(../images/image.jpeg); 
    background-position: center;
    background-repeat: no-repeat;
    background-size: 100% auto;
    color: 6b6b6b;
}

However, a loader is breaking, as it's trying to parse the binary file. An unexpected char. Removing the background line, and all works.

I am using webpack, and my modules are loaded like this:

module: {
        loaders: [
            {
                test: /.jsx?$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                query: {
                    presets: ['es2015', 'react']
                }
            },
            {
                rules: [
                  {
                    test: /\.css$/,
                    use: [ 'style-loader', 'css-loader' ]
                  }
                ]
              }
        ]
    }

How can I get my react app to work with this css image as a background url? Creating the project with create-react-app - it works, but I can't see how it's handling the images.

I have a repo: https://github.com/CraigInBrisbane/ReactLearning

Any assistance would be great.

4
  • Shouldn't that be url("../images/image.jpeg")? Note the quotation marks. Commented Dec 10, 2017 at 22:37
  • I can try that, but it worked that way (without quotes) when I created the project using create-react-app. Now, creating a project structure manually, creating my own webpack.config etc... and pulling in the modules I think I need - fails. Same css. Commented Dec 10, 2017 at 22:43
  • Have you tried to use url-loader? Commented Dec 10, 2017 at 22:55
  • Oh! No, I'll look that up. Commented Dec 10, 2017 at 22:59

1 Answer 1

2

When you use css-loader, webpack removes all your background-image: url(../images/image.jpeg); and inserts require(../images/image.jpeg). There is two way to solve this problem:

1) add file-loader/url-loader for these types of files

2) use absolute path instead of relative ( background-image: url(/images/image.jpeg); ) and css-loader will skip this image

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

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.