Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular dynamic background images

In the html template I have this style with a dynamic image:

<div style="background: url('/img/{{item.img}}'); width: 200px; height: 150px"></div>

Which works in web browsers and android browser. However background images that are dynamic using "style=" are not showing on iPad.

I could always create dynamic images using the img tag but I'm looking for a style/css solution for iPad.

like image 764
Pat M Avatar asked Jun 01 '16 17:06

Pat M


4 Answers

Use instead

<div [ngStyle]="{background: 'url(/img/' + item.img + ')', width: '200px', height: '150px'"></div>

or

<div [style.background]="'url(/img/' + item.img + ')'"
    [style.width.px]="200" [style.height.px]="150p"></div>

See also In RC.1 some styles can't be added using binding syntax

like image 153
Günter Zöchbauer Avatar answered Nov 15 '22 15:11

Günter Zöchbauer


You should do that as the +Günter Zöchbauer second part answer generates unwanted supplementary style background-position: initial and background-size: initial on Safari at least.

Note that I only set the background-image style:

<div [style.background-image]="'url(/img/' + item.img + ')'"
 [style.width.px]="200" [style.height.px]="150p"></div>
like image 15
Stéphane de Luca Avatar answered Nov 15 '22 15:11

Stéphane de Luca


<div id="component-id" [style.background-image]="'url(www.domain.com/path/'+bgImageVariable+')'">
    <!-- ... -->
</div>

or

<div id="component-id" [style.background-image]="'url('+bgImageVariable+')'">
    <!-- ... -->
</div>

you can use in second way by adding URL path in a single variable. for example

bgImageVariable="www.domain.com/path/img.jpg";
like image 8
Coder Girl Avatar answered Nov 15 '22 16:11

Coder Girl


use this and set height and width of div tag as per you need

<div id="component-id" [style.backgroundImage]="'url('+bgImageVariable+')'"></div>
like image 1
Manoj Rana Avatar answered Nov 15 '22 17:11

Manoj Rana