Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why I cannot set background image in Angular 13?

Following another stackoverflow answer, I tried following:

<div [style.background-image]="'url(https://picsum.photos/200)'"></div>

This does have no effect and no picture is shown. If I use the <img> tag instead and set the same url, it works. But I need it as a div background.

Can anyone tell me what I am doing wrong and how to set the image background of the div correctly?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem stands in your div definition. A div element have nothing inside so his height/width is 0. Try to something like:

your.component.html:

<div class="image"></div>

your.component.css:

.image {
  height: 200px;
  width: 200px;
  background-image: url('https://picsum.photos/200');
}

An example in stackblitz:
https://stackblitz.com/edit/angular-ivy-bmrp6j

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading