3

I'm trying to pass an URL image to my CSS from my html in Angular but I can't do that:

My CSS is :

card-profile_visual {
      height: $visual-height;
      overflow: hidden;
      position: relative;
      background: linear-gradient(to bottom, darken(#545559, 10%), saturate(darken(#3A4A7B, 2%), 20%), saturate(darken(#3A4A7B, 15%), 20%));
      &:before,
      &:after {
        display: block;
        content: '';
        width: 100%;
        height: 100%;
        position: absolute;
        z-index: 0;       
        background: url(myUrl) no-repeat center center/cover;
        opacity: 0.5;
        mix-blend-mode: lighten;
      }

    }

And my html is :

<div class="card-profile_visual" [style.background-image]="'url('+offer.photo_url+')'"></div>

How can I achieve this to pass offer.photo_url to CSS in :

background: url(myUrl) no-repeat center center/cover;

Thank you soo much

Josseph
  • 31
  • 2
  • I don't think there is a pure CSS way to do this. See here: https://stackoverflow.com/questions/26967890/css-set-background-image-by-data-image-attr — `background: url(attr(style));` is only in your dreams! – Rick Davies Jul 24 '18 at 08:47

3 Answers3

1

Use ngStyle and with CSS Variables for this like below:

Instead of:

CSS:

card-profile_visual {
      height: $visual-height;
      overflow: hidden;
      position: relative;
      background: linear-gradient(to bottom, darken(#545559, 10%), saturate(darken(#3A4A7B, 2%), 20%), saturate(darken(#3A4A7B, 15%), 20%));
      &:before,
      &:after {
        display: block;
        content: '';
        width: 100%;
        height: 100%;
        position: absolute;
        z-index: 0;       
        background: url(myUrl) no-repeat center center/cover;
        opacity: 0.5;
        mix-blend-mode: lighten;
      }

    }

HTML:

<div class="card-profile_visual" [style.background-image]="'url('+offer.photo_url+')'"></div>

Use this:

CSS:

card-profile_visual {
      height: $visual-height;
      overflow: hidden;
      position: relative;
      background: linear-gradient(to bottom, darken(#545559, 10%), saturate(darken(#3A4A7B, 2%), 20%), saturate(darken(#3A4A7B, 15%), 20%));
      &:before,
      &:after {
        display: block;
        content: '';
        width: 100%;
        height: 100%;
        position: absolute;
        z-index: 0;       
        background: var(--my-image) no-repeat center center/cover;
        opacity: 0.5;
        mix-blend-mode: lighten;
      }
    }

HTML:

<div class="card-profile_visual" [ngStyle]="{'--my-image': ' + offer.photo_url + '}"></div>

I hope it will help you.

Vikasdeep Singh
  • 17,777
  • 9
  • 70
  • 93
  • Hi,VicJordan ... i've did that and when I inspect the code, my html is :
    and my css : background: var(--my-image) no-repeat center center/cover;
    – Josseph Jul 24 '18 at 09:46
1

You can use url in content property

content: url(imageUrl);
mustafa bagwala
  • 261
  • 2
  • 12
0

Try this

<div class="card-profile_visual" [style.backgroundImage]="'url('+offer.photo_url+')'"></div>

To add background image in pseudo element in css you can do this way

.card-profile_visual:after{
  background: url(myUrl) no-repeat center center/cover;
}
jack
  • 479
  • 5
  • 17