1

The Scenario:

I have an element in my Angular app that I want to add a background image to. At the moment I have this:

<div class={{background-class}}></div>

where background-class is, for example, "background-style-1", which is in my stylesheet like this:

background-style-1 {
  background-image: url(../../../../assets/images/contextmenu/person.png);
}

The element successfully displays the image, and if I query it, it shows this: enter image description here

The Problem:

I don't want to hard-code all the background image URLs into my stylesheet. Instead, I want to just pass the URL into the HTML tag.

What I've Tried

<div style="background-image: url(../../../../assets/images/contextmenu/person.png)"></div>

This fails to display the image, and when I query the element, it shows this:

enter image description here

What I Want To Know:

Why does passing the image URL as an inline style property not work, and how can I make it work?

4 Answers 4

1

You can dynamicaly set the background image from the html using this syntax : [style.someproperty]="'somevalue'". In your case, it can look something like this.

<div [style.backgroundImage]="'url(' + url + ')'"></div>

This will take the value of url ( defined in your controller) as the url for the background image.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Nicholas, unfortunately it's still not working. The image doesn't load, and when I hover over the element it says: background-image: url(../../../../assets/images/contextmenu/person.png); Is there any other info I can give you to help diagnose the problem?
Are you sure the image ../../../../assets/images/contextmenu/person.png exists ?
Yep, double-checked ;)
I'm not sure but it might have something to do with the way Angular bundle your files. Remember that when you load your site, you are loading compiled file from the dist folder. So if your dist folder doesn't have your image is it, it is not available to the browser.
if everythings is in ~/projects/somefolders/yourapp/dist/ then ../../../../ goes back all the way to the project folder ( in this example ) @RNdev
1

You can use ngStyle:

HTML:

[ngStyle]="{'background-image': 'url(' + image + ')'}"

TS:

image = "assets/images/contextmenu/person.png"

2 Comments

Hi Adrita, unfortunately it's still not working. The image doesn't load, and when I hover over the element it says: background-image: url(../../../../assets/images/contextmenu/person.png); Is there any other info I can give you to help diagnose the problem?
Are you sure the image path is correct? Simply test it by binding image src
0

You can use ngStyle directive to bind styles dynamically on html elements

In component set your path variable

path = '../../../../assets/images/contextmenu/person.png'

use it in template like below

<div [ngStyle]="{'background-image': 'url(' + path + ')'}"></div>

Try this

https://stackblitz.com/edit/angular-cxk5dr

Documention link for ngStyle

https://angular.io/api/common/NgStyle

3 Comments

Hi Mamta, unfortunately it's still not working. The image doesn't load, and when I hover over the element it says: background-image: url(../../../../assets/images/contextmenu/person.png); Is there any other info I can give you to help diagnose the problem?
can you please provide stackblitz link with some sample code of yours
I don't think you can upload images to stackblitz. My code is exactly the same as yours in your stackblitz link, except that rather than using "https..." as the URL, I'm using the ref to a local file. Do you know if it's possible to upload images to stackblitz to replicate my scenario? I don't think I can.
0

You can either use [ngStyle] or [style.backgroundImage] to bind dynamic styles.

and it's better to pass image/icon name to the component.

[ngStyle]="{'background-image': 'url(path/to/icons' + iconName + ')'}"

or smililarly

[style.backgroundImage]= 'url(path/to/icons' + iconName + ')'

and the iconName would be

const iconName = 'logo.svg';

PS: Ensure that logo.svg is present in the given path.

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.