question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NgStyle and style can not set `!important`

See original GitHub issue

I’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:closed
  • Created 7 years ago
  • Reactions:19
  • Comments:33 (10 by maintainers)

github_iconTop GitHub Comments

34reactions
lazarljubenoviccommented, Oct 14, 2016

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 the style 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.

<style> div { color: blue } </style>
<div style="color: red">I'm red, not blue! No need for !important</div>

The only time you need !important is when your CSS already has it.

<style> span { color: blue !important } </style>
<span style="color: red !important">I'm red now.</span>

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.)

25reactions
vytautas-pranskunas-commented, Apr 11, 2019

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); } }

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found