7

I am forever plagued by trying to reference an image from across my application. The directory structure is:

src/app/assets/images/splashImage.png

In this trivial case, I'm trying to reference it from:

src/app/app.component.html

The first line of the html:

<div class="splashContainer">

The css file:

.splashContainer {
background: url("../../assets/images/splashImage.png") no-repeat center center fixed;

The build diagnostic says it can't find the file at --> the URL path from above.

My rule that obviously is wrong is to count up starting at the folder housing the html. app is 1 and src is 2, which gets me above the assets folder. Thus the two ../ parts.

What am I doing wrong?

Thanks in advance. Yogi

4
  • Where is the scss file located? Commented Jan 31, 2018 at 12:21
  • Scss is in the same location as the html. :-) Commented Jan 31, 2018 at 12:45
  • Then try ./assets/images/splashImage.png as location for background-img Commented Jan 31, 2018 at 12:51
  • ERROR in ./src/app/app.component.scss Module not found: Error: Can't resolve './assets/Images/SolutionHunter%20Splash%20Image.png' in 'D:\Solution Hunter Engineering\Active Development\Hunter2\src\app' Commented Jan 31, 2018 at 12:53

4 Answers 4

8

Try

background: url(assets/images/splashImage.png) no-repeat center center fixed;
Sign up to request clarification or add additional context in comments.

3 Comments

I tried: background: url("assets/Images/SolutionHunter%20Splash%20Image.png") no-repeat center center fixed; and got ERROR in ./src/app/app.component.scss Module not found: Error: Can't resolve './assets/Images/SolutionHunter%20Splash%20Image.png' in 'D:\Solution Hunter Engineering\Active Development\Hunter2\src\app' resolve './assets/Images/SolutionHunter%20Splash%20Image.png' in 'D:\Solution Hunter Engineering\Active Development\Hunter2\src\app' using description file: D:\Solution Hunter Engineering\Active Development\Hunter2\package.json (relative path: ./src/app)
What is Solution Hunter after images here? assets/Images/SolutionHunter. The image file name is splashImage.png right?
Great Yogi, you got a solution. But i have a suggestion, i thinks it's better to use a image file name without space, what i understand from your answer is you are having an image file with name 'SolutionHunter Splash Image.png', and also it should work background: url(assets/images/yourfilename.png), as it's working for me. Thanks
6

Do not use paths like ../dir or ../../dir since relative paths like this will most likely fail in the server after build. Since assets/ folder is declared by default in the angular.json file. I suggest you use inline css just for the image only. For instance:

<div class="bg-div" style="background-image: url(assets/images/imageX.png)"></div>

Then continue with the other css in your css file: i.e.

.bg-div: {
  background-size: cover;
  background-position: center center;
  height: 100%;
  position: relative;
}

This way it will work for both local builds during development and during deployment. You wont have to change the paths during build again.

This is if you have another external css file apart from the main angular's styles.css

Comments

1

Thanks for all your help. I figured out the answer. First: my "formula" for the path was wrong. I should start counting from the parent of the folder that contains the file. So in this case: app contains the html, scss so I don't count it. However, its parent Src is 1 and it IS above the assets. so the path is ../assets etc at the point webstorm was happy with the path, but not with the file name. SO, I got rid of the %20's in the path and replaced them with blanks. Thus:

background: url("../assets/Images/SolutionHunter Splash Image.png") no-repeat center center fixed;

This works. Thanks for your assist. Yogi

2 Comments

You should not use spaces in filenames, it can really mess up things. I suggest you change the image name to solutionHunterSplashImage.png
I agree .. will do. Thanks.
1

See this Answer.

Detailed explanation to Angular's path resolution in CSS files.

https://stackoverflow.com/a/65799235/10569886

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.