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.

Asset paths in CSS

See original GitHub issue

README says:

Webpack finds all relative module references in CSS (they start with ./) and replaces them with the final paths from the compiled bundle. If you make a typo or accidentally delete an important file, you will see a compilation error, just like when you import a non-existent JavaScript module. The final filenames in the compiled bundle are generated by Webpack from content hashes. If the file content changes in the future, Webpack will give it a different name in production so you don’t need to worry about long-term caching of assets.

But what should I do if the assets are not located in the same directory as the CSS files? All my assets are in src/assets/..., but CSS files can be in other folders as well. Neither ./assets/... nor ../assets/... seems to work.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

15reactions
doronnaccommented, Dec 26, 2017

~assets/... should do the trick

3reactions
gaearoncommented, Jan 20, 2018

I think this is expected, even if a bit counter-intuitive.

Webpack treats URLs like stuff/a.jpg in CSS as relative (./) because that’s how existing CSS stylesheets work. If it didn’t, it would be very hard to reuse existing CSS code in webpack (e.g. from CSS libraries or even from existing apps migrating to webpack).

I don’t see us fixing it, as it would create more issues. As suggested by @doronnac, you can explicitly tell webpack to use its resolution by prepending the path with ~: https://github.com/facebookincubator/create-react-app/issues/3582#issuecomment-353965421. I verified this works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Path of assets in CSS files in Symfony 2 - Stack Overflow
I have came across the very-very-same problem. In short: Willing to have original CSS in an "internal" dir (Resources/assets/css/a.css); Willing to have the ......
Read more >
Image path CSS
It shows in local test environment but not on live website. I can access the image if I add /assets/images/header-bottom.png after URL, ...
Read more >
url() - CSS: Cascading Style Sheets - MDN Web Docs
The url() CSS function is used to include a file. The parameter is an absolute URL, a relative URL, a blob URL, or...
Read more >
HTML File Paths - W3Schools
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP,...
Read more >
Assets: CSS, Images, etc - SymfonyCasts
Here's the deal: whenever you reference a static file on your site - like a CSS file, JavaScript file or image, instead of...
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