NgStyle and style can not set `!important`
See original GitHub issueI’m submitting a … (check one with “x”)
[x] bug report => search github for a similar issue or PR before submitting
Current behavior Both options will fail:
[ngStyle]="{'color':'red !important'}"
[style.color]="'red !important'"
Expected behavior
!important
is valid css usecase.
Reproduction of the problem https://plnkr.co/edit/abOMwv3tcYnnjqjCe4ff?p=preview
- Angular version: 2.0.0
- Browser: [all ]
- Language: [TypeScript 1.8.11]
Issue Analytics
- State:
- Created 7 years ago
- Reactions:19
- Comments:33 (10 by maintainers)
Top Results From Across the Web
How to use ng-style with !important? - css - Stack Overflow
NOTE According to this question and this GitHub issue, the !important keyword is not supported within the ng-style directive.
Read more >Using ngStyle in Angular for dynamic styling - Ultimate Courses
In this article, we will look at how to style elements inline using the style attribute. Even more excitingly, we will explore how...
Read more >NgStyle & NgClass • Angular - codecraft.tv
The NgStyle directive lets you set a given DOM elements style properties. One way to set styles is by using the NgStyle directive...
Read more >What's the difference between [style] and [ngStyle] in Angular?
Now you can express as many different styles as needed. ngStyle is an Angular directive that gives you the flexibility to do this,...
Read more >NgStyle - Angular
An attribute directive that updates styles for the containing HTML element. Sets one or more style properties, specified as colon-separated key-value pairs.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
A quick advice for people trying to do this.
!important
is used to override any other declaration, incluing declarations set to the element itself using thestyle
attribute (and this is the only way to override them). You should almost never need to add!important
directly to the element, because it makes little sense to do so.The only time you need
!important
is when your CSS already has it.But
!important
is already a bad practice in the stylesheet. Are you sure you want to do this in the actual attribute?(It’s still a bug. But a bug that stops you from writing spaghetti styles.)
as a workaround you can do:
[attr.style]="('font-size:' + actions.fontSize + '% !important') | safeStyle"
where safeStyle is
@Pipe({ name: 'safeStyle' }) export class SafeStylePipe implements PipeTransform { constructor(private sanitizer: DomSanitizer) { } transform(value) { return this.sanitizer.bypassSecurityTrustStyle(value); } }