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.

Referencing images from css

See original GitHub issue

Noob question again: How do I reference images in my css? I’m doing this (the image is next to the css file in the folder):

.poster {
    background: url(./hero-banner.jpg) no-repeat;
    background-size: cover
}

The outputted css is:

._33hlffvz0sgxpxLxtaBGAu {
    background: url(/b1e8ba2d0f312bdf6d0011f87d91bed1.jpg) no-repeat;
    background-size: cover;
}

The image dosen’t show up (and I can’t open it in a new tab, for instance).

If I do this:

.poster {
    background: url(/hero-banner.jpg) no-repeat;
    background-size: cover
}

The outputted css is:

._33hlffvz0sgxpxLxtaBGAu {
    background: url(/hero-banner.jpg) no-repeat;
    background-size: cover;
}

But the image still dosen’t show, and if I try to open the image in a new tab, this is the url I get: chrome-devtools://devtools/hero-banner.jpg

Sorry for my stupid questions, but any help is much appreciated!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
kachkaevcommented, Aug 22, 2016

I just faced the same issue, which only happened in dev mode - the images did show up after build.

Here are the steps that should help getting over it (inspired by https://github.com/webpack/style-loader/pull/96#issuecomment-228370602):

  1. Create internals/dev-preamble.js:

    __webpack_public_path__ = window.location.protocol + '//' + window.location.host + '/';
    
  2. Open internals/webpack/webpack.dev.babel.js and add one line:

    // Add hot reloading in development
    entry: [
      'eventsource-polyfill', // Necessary for hot reloading with IE
      'webpack-hot-middleware/client',
      path.join(process.cwd(), 'internals/dev-preamble.js'), // <--------------- this one
      path.join(process.cwd(), 'app/app.js'), // Start with js/app.js
    ],
    

Here is what changes when you look at your CSS in Chrome’s inspector:

# source
background: url('./logo.png') no-repeat 0 0;

# dev mode, before hacking
background: url(/8fe95655bf016c50f6c443abf0186652.png) no-repeat 0 0;

# dev mode, after hacking
background: url(http://localhost:3000/8fe95655bf016c50f6c443abf0186652.png) no-repeat 0 0;

Might be worth doing this right in the boilerplate, but I’m not sure if my solution is elegant enough for a PR. Hope others spend less time making this work!

1reaction
rizedrcommented, Nov 29, 2016

@mxstbr Has any solution been adopted for this issue? Except @kachkaev’s solution posted above? I am working on a project which has v3.0.0 as dependency.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CSS Styling Images - W3Schools
CSS Styling Images · Rounded Images · Thumbnail Images · Responsive Images · Center an Image · Polaroid Images / Cards · Transparent...
Read more >
How to reference an image from computer directory as a ...
Create a folder called, say, - "myWebsite" · Inside 'myWebsite' folder put all your html files · Inside 'myWebsite' folder create another folder ......
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 borders of the element are then drawn on top of them, and the background-color is drawn beneath them. How the images are...
Read more >
How do I reference images through CSS in a library ... - Drupal
Hi. I have created a module, added a library, added CSS and JS. The CSS and JS load correctly. However the images referenced...
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