Search code examples
angularbackgroundng-style

ngStyle background image not visible


I've got the following problem: I want to insert a background image to my website with ngStyle. The following code works fine:

<div [ngStyle]="{'background-image': 'url(' + myBackgroundUrl + ')', 'width': '224px', 'height': '248px'}"></div>

However, if I want to add some more "attributes" to the background-image, the image isn't visible anymore:

<div [ngStyle]="{'background-image': 'url(' + myBackgroundUrl + ') no-repeat -228px 0', 'width': '224px', 'height': '248px'}"></div>

Solution

  • The background-image property sets one or more background images for an element.

    If you want to add some more "attributes" then use background instead:

    <div [ngStyle]="{
      'background': 'url(' + myBackgroundUrl + ') no-repeat -228px 0', 
      'width': '224px', 
      'height': '248px'
    }"></div>