-
Notifications
You must be signed in to change notification settings - Fork 12k
Can't use relative paths in url() in scss files #12797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Just had a quick look at this, at this is tricky one. Since in |
Well yes, if I use a relative path in the one sass-file, it uses it as relative from every other file where I import it. Isn't there a way to just have it relative to the single file with the url()? |
I think the recommended approach here is to put these sprite file in the assets folder, and use root level paths (starting with In background-image: url('/sprite/sprite.svg'); in your
From |
The problem with that is that I can't change the sprite.scss, it is generated by the svg-sprite package. For as far as I know, it does not have the option to generate with an absolute path. |
The simplest way to do it is to copy https://github.com/jkphl/svg-sprite/blob/6966592489dfcf2cd9ed2bba500cb8ee31c00b9c/tmpl/css/sprite.scss and add a lead slashes
|
Tried it out, works perfect. Thanks for the idea @alan-agius4 ! |
Doesn't work same issue with absolute paths (at least behaviour is inconsistent with angular6 ). Happened after upgrade to 7. I think what changed is automatically adding resolved scss assets to assets option/public folder. Hence people reporting having to add each file to assets (At least in the past we didn't need to). |
btw I am not getting any errors for entirely wrong paths either as I used to. |
is sass loader what angular uses internally? |
any update on this issue ? |
I’m also interested on this, since Stylus and LESS work fine with relative URLs and SASS doesn’t. For ejected projects |
I get the following, rocking Angular CLI: 7.3.0, Angular: 7.2.3. // angular.json
"assets": [
"src/assets"
] background-image: url('/src/assets/images/icon.png');
// GET http://localhost:3000/src/assets/images/icon.png 404 (Not Found)
|
As @agalazis, correctly pointed out the issue. Sass itself doesn't provide Url rewiring. While indeed |
@ruimarques, I suggest you open a separate issue with a reproduction for the issue you are encountering. |
@alan-agius4 another point to mention in the past svgs in my css but after giving up and setting assets path now they are loaded as links after build |
Please consider not removing |
background-image: url('~/assets/images/logo.svg'); works for me (version 8.0.1) |
@iwnow This has been said to cause the app to be deployed with images copies to the dist folder (out of the assets folder) meaning you have multiple copies for no reason. Angular Team: This ticket has been here for well over a year. What was the plan? Was there one? It seems the idea was to just break AngularCLI and say use this method that doesn't work and hope for the best. |
@michael-letcher yes I understand but I have no multiple copies, all works as expected |
background-image: url('../../assets/img/bg-masthead.jpg'); worked for me. |
The solution is to write the path as if you were starting from "angular.json". Instead of Write it like this (witout the dots at the beggining) -- this worked for me ☺ |
A couple of the workarounds here are basically: "just use absolute paths". The issue is about the inability to use relative paths :D In some cases you can't use absolute because you have a baseHrefUrl like You can workaround this using Express I guess but |
I am having a similar issue. My production server is running the solution in a subfolder (mydomain.com/myapp/). I have setup the base href to "/myapp/" on localhost it is set to "/". I have a css file placed in "assets/css" this file is referring to a file in "assets/images", I have done that writing "/assets/images/myimage.png" This is working fine localhost, but when publishing to the server it can't find the image. If I change url on the server in the css to "assets/images/myimage.png", it works on the server. Then I have tried changing the url in the project to the one on the server ("assets/images/myimage.png"), but then it can't compile I am getting the error: Can't resolve 'assets/images/myimage.png". I have tried the following combinations, but without any luck :-| './assets/images/myimage.png' |
@pnrdk did you try the following (note the / between the ~ and 'assets')? |
This worked for me with the latest version of the CLI. |
thankk you |
You can use inline CSS style |
@iwnow MVP, that really works! Works in latest Angular version (8.3.21)! 🚀 |
It's compiled to
in my case which is not what's expected. Globally I'd recommand to never use absolute paths in However, the compiler complains when using relative paths, especially if you import a |
the problem this works in dev env, but not when building the app. (in SCSS - didn't try plain CSS) the build output for '~/assets/images/logo.svg' is -> '/assets/images/logo.svg' which prevents from deploying the app in any other path than the root, even though in my index.html i set the baseHref to be relative './' |
There is still this fix if you can use custom-webpack. |
the only way i found how to fix it in builds is to specify the base-href attribute to be the same as the path i plan to deploy to, for example:
and then in the SCSS file use: after building the app, the ~ char is replaced with whatever value you provide to the --base-href attribute. |
@bnohad unfortunately this didn't work with
|
Just found that when using a caret .box {
background: url("^myfiles/images")
} Of course, there is an In addition, as off-topic, when resolving the urls (not using ^), the files will be copied to the "assets": [
"src/favicon.ico",
"src/assets",
{ "glob": "**/*", "input": "./node_modules/mymodule/assets/", "output": "./assets/" }
], |
FYI - the caret sign was previously discussed in the following issue angular/angular#32811 |
Thanks @dannyskoog , |
Yes @devniel. And that was why I wasn't allowed to add it to the documentation (reference: angular/angular#32921 (comment)) |
…s when using preprocessors Previously, when using a preprocessor, resources (e.g., `url(./my-image.jpg)`) referenced in a stylesheet that was imported into another stylesheet would retain the exact URL. This would be problematic as the resource would not be at the relative location within the new combined stylesheet. With this change the resource URLs will now be adjusted to reference the origin location of the resource. This allows the resources to be found without any additional changes to the application or build process. CSS and Less already functioned in this manner. This change brings Sass and Stylus to parity. Fixes: angular#12797
…s when using preprocessors Previously, when using a preprocessor, resources (e.g., `url(./my-image.jpg)`) referenced in a stylesheet that was imported into another stylesheet would retain the exact URL. This would be problematic as the resource would not be at the relative location within the new combined stylesheet. With this change the resource URLs will now be adjusted to reference the origin location of the resource. This allows the resources to be found without any additional changes to the application or build process. CSS and Less already functioned in this manner. This change brings Sass and Stylus to parity. Fixes: #12797
Thanks It worked 👍 |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Bug Report or Feature Request (mark with an
x
)Command (mark with an
x
)Versions
npm: 6.4.1
node: v10.7.0
ng cli: 6.2.6
macOS High Sierra
Repro steps
With the following content:
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
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
The text was updated successfully, but these errors were encountered: