question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

require image in CSS

See original GitHub issue

In header.jsx, we can require a image like,

<img className="Header-brandImg" src={require('./logo-small.png')} />

Is it possible to require a image in CSS? Something like:

.HomePage-container H1:before {
  content: url({require('/images/test.png')});
}

<bountysource-plugin>

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource. </bountysource-plugin>

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
cwtuancommented, Nov 16, 2015

I find out the solution: just use the relative path so that webpack will resolve the path:

Use this:

.HomePage-container H1:before {
  content: url('../../public/images/test.png');
}

Don’t use this

.HomePage-container H1:before {
  content: url('/public/images/test.png');
}
1reaction
cwtuancommented, Nov 16, 2015

Let me clarify the question:

Since my images will be deployed in CDN, I set the config in webpack.config.js like

config.output.publicPath = "http://mycdn.com/"

All my images are located at public/images, so I need ‘require’ method to add domain name for the images. It’s easy to require a image in JSX, like

<img src={require('../../public/images/logo-small.png')} />

Then it will be compiled to

<img src="http://mycdn.com/1fd0cda7af8115026a64413a9b9027b5.png" />

But I can’t figure out how to require a image in CSS. I need something like this:

.HomePage-container H1:before {
  content: url({require('../../public/images/logo-small.png'))});
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use images in css with Webpack - Stack Overflow
Webpack outputs to a dist folder, but I can't figure out how to get images there. Here is my webpack setup: var path...
Read more >
How to Add an Image to a CSS File
1. Open your website's stylesheet with your HTML editor or a text editor. · 2. Paste the following code into the sheet to...
Read more >
background-image - CSS: Cascading Style Sheets | MDN
The background-image CSS property sets one or more background images on an element.
Read more >
Images in CSS (file-loader) - SymfonyCasts
When we tell Webpack to load a CSS file, it actually parses the background images and - basically - requires them! It does...
Read more >
background-image | CSS-Tricks
The background-image property in CSS applies a graphic (e.g. PNG, SVG, JPG, GIF, WEBP) or gradient to the background of an element.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found