3

When providing a background image for an element, everything works fine:

<div [style.background-image]="sanitizedStyleValue"></div>

But how can I provide a mask-image for an element? This:

<div [style.mask-image]="sanitizedStyleValue"></div>

is not working. Also

<div [ngStyle]="{ 'mask-image': 'url( mymaskimage.svg )' }"></div>

does not work. I don't even get a warning in the console.

Is this due to DomSanitizer's ignorance of mask images' right for existance, or is there another way to achieve this?

Context

I want to create an IconComponent where I can Input() an icon name which will be used as part of an url for the mask image. Another Input() holds the background color. This way, I can combine different icons with different colors.

I would like to prevent creating CSS-classes for every possible icon.

4
  • try the answer below, if doesn't work provide stackblitz. Commented May 24, 2020 at 8:29
  • Would you be so kind as to tell us what the warning in the console is? A Cors warning maybe? Commented May 24, 2020 at 9:38
  • There is no warning at all in the console Commented May 24, 2020 at 9:51
  • 1
    Sorry, I misread your post about warnings. Try <div [style.-webkit-mask-image]="sanitizedStyleValue"></div> Commented May 24, 2020 at 10:16

2 Answers 2

2

According to the documentation, for CSS masks to work with chrome, you need to prefix the rules with -webkit-

So this shoud work

<div [style.-webkit-mask-image]="sanitizedStyleValue"></div>

Note: According to W3C recommendations, the images you are using for the mask must be returned with correct CORS headers.

User agents must use the potentially CORS-enabled fetch method defined by the [HTML5] specification for all , and values on the mask-image, mask-border-source and clip-path properties. When fetching, user agents must use “Anonymous” mode, set the referrer source to the stylesheet’s URL and set the origin to the URL of the containing document. If this results in network errors, the effect is as if the value none had been specified.

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

1 Comment

I can confirm this is working! Tried pretty much everything before that, thanks @David.
1

Try this :-

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

2 Comments

This is the CSS-only approach which would clearly work. But I want to dynamically set the mask image as a custom Input() of an IconComponent. Updated question. But thanky anyway.
what is in sanitizedStyleValue ?

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.