Can't use relative paths in url() in scss files

See original GitHub issue

Bug Report or Feature Request (mark with an x)

- [ ] bug report -> please search issues before submitting
- [x] feature request

Command (mark with an x)

- [x] new
- [x] build
- [ ] serve
- [ ] test
- [ ] e2e
- [ ] generate
- [ ] add
- [ ] update
- [ ] lint
- [ ] xi18n
- [ ] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc

Versions

npm: 6.4.1 node: v10.7.0 ng cli: 6.2.6 macOS High Sierra

Repro steps

  1. Create new project

ng new ng-sass-test–style=scss

  1. Add following files:
  - src
    - sprite
      - _sprite.scss
      - sprite.svg

With the following content:

# _sprite.scss
.sprite-common {
    background-image: url('sprite.svg');
}

Add the following line to src/styles.scss. @import '~src/sprite/sprite'; Add the same line to src/app/app.component.scss.

The log given by the failure

ERROR in ./src/styles.scss (./node_modules/raw-loader!./node_modules/postcss-loader/lib??embedded!./node_modules/sass-loader/lib/loader.js??ref--15-3!./src/styles.scss)
Module Error (from ./node_modules/postcss-loader/lib/index.js):
(Emitted value instead of an instance of Error) CssSyntaxError: /***/ng-sass-test/src/sprite/_sprite.scss:2:22: Can't resolve 'sprite.svg' in '/***/ng-sass-test/src'

  1 | .sprite-common {
> 2 |     background-image: url('sprite.svg');
    |                      ^
  3 | }

ERROR in ./src/app/app.component.scss
Module Error (from ./node_modules/postcss-loader/lib/index.js):
(Emitted value instead of an instance of Error) CssSyntaxError: /***/ng-sass-test/src/sprite/_sprite.scss:2:22: Can't resolve 'sprite.svg' in '/***/ng-sass-test/src/app'

  1 | .sprite-common {
> 2 |     background-image: url('sprite.svg');
    |                      ^
  3 | }

Desired functionality

I would like to be able to use the relative file path in the scss file. I know this is solved by using an absolute path for the image. But I’m using the package svg-sprite, I’m not an expert in the package, but it looks like it doesn’t support generation of an absolute path. Maybe I’m missing something in the angular-cli setup, so I can use this way of path-handling.

Mention any other details that might be useful

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:36
  • Comments:41 (2 by maintainers)

github_iconTop GitHub Comments

52reactions
iwnowcommented, Jun 14, 2019
background-image: url('~/assets/images/logo.svg');

works for me (version 8.0.1)

17reactions
alan-agius4commented, Jan 15, 2020

I think the recommended approach here is to put these sprite file in the assets folder, and use root level paths (starting with /) for urls in sass files that are imported by other sass files.

In scss files at an absolute path;

background-image: url('/sprite/sprite.svg');

in your angular.json add the following so that the sprite is copied, under architect -> build -> options

"assets": [
    "src/sprite/sprite.svg"
],

From svg-sprite package point of view, there will be no changes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular scss url(), cant resolve relative path when project is ...
Try using the following syntax: background-image: url('../../assets/icons/<file-name>'). Where the route is relative to the css file.
Read more >
Sass: @use
Modules will always be loaded relative to the current file first, though. Load paths will only be used if no relative file exists...
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 >
Relative paths to images in CSS doesn't work anymore (ie ...
Hi I can't load image from CSS, I'm using this: background-image: url('. ... 2 Likes. Relative asset paths issue in SCSS files -...
Read more >
I cannot create relative path links - CSS - Codecademy Forums
You can replace your backslashes with forward slashes ( / ). · Alternatively, if you really love the Windows-style backslash, you can put...
Read more >

github_iconTop Related Medium Post

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