ppk,
@ppk@front-end.social avatar

Aarghh! #css law: Thou shalt never use !important, except in very specific cases:

  1. To find out if you have a specificity problem you can temporarily add !important to a declaration. If it's now applied, you know you have a specificity problem

  2. .... the one I forgot. I posted this same question a while ago, and someone came up with a second legitimate use case for !important, but what was it?

Help!

simevidas,
@simevidas@mastodon.social avatar

@ppk

[hidden] {
display: none !important;
}

mia,
@mia@front-end.social avatar

@ppk important is designed to protect general-but-essential styles from otherwise higher-priority layers, origins, specificities.

Could be good to use in custom-elements for essential shadow-defined defaults, or important 'resets' like [hidden] or :focus, or in low broad cascade layers when a higher layer should not be able to override certain things.

The key is using it protectively, rather than aggressively.

ppk,
@ppk@front-end.social avatar

@mia ... yes ... I'm slowly starting to see that. Problem is that I can't give readers examples because they'd go deep into CSS architecture, which the book hardly treats.

mia,
@mia@front-end.social avatar

@ppk yes - hard to explain the cascade without talking about big architecture. I think that's one reason people have never fully learned either: how the cascade works in depth, or how to architect css well. They're the same issue.

ppk,
@ppk@front-end.social avatar

@mia Well, in your copious spare time ...

mia,
@mia@front-end.social avatar

@ppk Teaching a workshop tomorrow … 😅

vasilis,
@vasilis@social.vasilis.nl avatar

@ppk Snook wrote about one in his Smaccs book. Don’t remember the details.

castastrophe,
@castastrophe@front-end.social avatar

@ppk As a side note, just love these threads! So many great suggestions, ideas, and feedback and it's great seeing the community come together.

castastrophe,
@castastrophe@front-end.social avatar

@ppk I've recommended !important in web component stylesheets for :host styles in the past (but they're not going to win against external !importants so 🤷‍♀️).

a11yMel,
@a11yMel@front-end.social avatar

@ppk Sometimes it's necessary when:

  1. you're working on an application and don't have access to change the generated inline styles (usually some YAML-based foolishness) or even just don't have access to all of the stylesheets

  2. You're a user and want to generate your own stylesheets (for fun and/or accessibility)

jackah,

@ppk can be useful for enforcing reduced motion preferences:

@media (prefers-reduced-motion: reduced) {

  • { transition-duration: 0.01ms !important; }
    }

https://andy-bell.co.uk/a-modern-css-reset/

ppk,
@ppk@front-end.social avatar

@jackah That's a good one, but not what I meant. Still, will-mention.

sjorsrijsdam,
@sjorsrijsdam@mastodon.social avatar

@ppk Was going to say override style attributes but since you mentioned it was about author stylesheets I am going with overriding something like media/container queries?

verrukkelijke15,

@ppk was it for overwriting inline styles, maybe?

patrick_h_lauke,
@patrick_h_lauke@mastodon.social avatar

@ppk "utility classes" type things, that you may want to apply at a previously unknown level so can't (easily) sort out better selectors?

Krazov,
@Krazov@mstdn.social avatar

@ppk, I always stuck with no exceptions. For point 1, duplicating a class would usually work (.classname.classname { /* props */ }).

ppk,
@ppk@front-end.social avatar

@Krazov Certainly not! What if you have a conflict with an ID?

Krazov,
@Krazov@mstdn.social avatar

@ppk, it's been years when I worked directly with CSS (damn Tailwind), but I also enforced the rule to not style by an ID due to specificity issues. My rationale was that someone might add !important to the ID as well, and then I'm cooked. Hence, this conflict was not possible.

dbaron,
@dbaron@w3c.social avatar

@ppk User style sheets?

I think there may also be some legitimate use cases with web components (when interacting with the context concept in the cascade: https://drafts.csswg.org/css-cascade-5/#cascade-context ).

ppk,
@ppk@front-end.social avatar

@dbaron No, it was definitely in author style sheets.

And I'm going to come out and admit that I don't really understand the context step of the cascade. (Or the transition part of Origins and Importance.)

Schepp,
@Schepp@mastodon.social avatar

@ppk @dbaron I was once at a wonderful conference, I just don't remember its exact name... Something along the lines of Styling Day or CSS Yay! or similar. Anyways, there was a fantastic talk on that subject by @bramus on the CSS Cascade which did an amazing job at explaining it.

Maybe you meant how !important works in relation to the new @layer? My other guess is that only with !important you can override a keyframe animation.

dbaron,
@dbaron@w3c.social avatar

@ppk Not sure if this will clear anything up, but: The context step is relevant to the selectors defined in https://drafts.csswg.org/css-scoping-1/ (and also, if we ever get to standardizing pieces of user-agent shadow DOM, probably also to pseudo-elements that select elements in standardized user-agent shadow DOM). It works just like the origin step, but a level weaker.

1/2

ppk,
@ppk@front-end.social avatar

@dbaron And shadow rules have precedence over non-shadow rules?

In any case, this confirms my hunch that Context is mostly about Shadow DOM, which I'm not going to treat. So that's going to be an easy section.

dbaron,
@dbaron@w3c.social avatar
ppk,
@ppk@front-end.social avatar

@dbaron Ah, the shadow is sort-of treated as a browser style sheet. That makes sense!

dbaron,
@dbaron@w3c.social avatar

@ppk The transition part of origin and importance is the way it is because (a) transitions occur between two styles that we know already won the cascade and (b) we want the transition to be able to operate on any such style, even if it had/has !important on it. (What I'm not sure is whether all engines have caught up to the spec in terms of not implementing animations the same way; animations don't share property (a) so they don't work the same way.)

2/2

ppk,
@ppk@front-end.social avatar

@dbaron Thanks! Transitions are clear now - or at least, I now know how to test them.

As to animations, in my tests they sort-of behaved as I expected given their place in the origin, but I'll test them some more after I've understood transitions.

dbaron,
@dbaron@w3c.social avatar

@ppk I'm not sure whether animation cascading matches across implementations... so it may be worth testing in multiple engines.

ppk,
@ppk@front-end.social avatar

@dbaron Now it gets interesting ... The only test I wrote and really understand is here:

https://quirksmode.org/booktests/animations.html

Firefox and Chrome cancel the animation when you set an !important style. Safari/Mac does not.

Is this what you mean?

bramus,
@bramus@front-end.social avatar

@ppk @dbaron Regarding the context criterion of the cascade: this CodePen might be helpful to show what happens.

https://codepen.io/bramus/pen/wvmxwPE

ppk,
@ppk@front-end.social avatar

@bramus @dbaron Sorry, no. It's gibberish to me. Is there a good article on this topic where someone explains the Context step by step?

bramus,
@bramus@front-end.social avatar

@ppk @dbaron Note that the explanation is included as comments in the CSS, they’re not in the markup (I should fix that 😬)

castastrophe,
@castastrophe@front-end.social avatar

@bramus @ppk @dbaron Haha I was confused looking at this for a moment until I realized it had opened in my safari browser and was using custom built-ins. 🙃

blemmie,
@blemmie@mastodon.social avatar
ppk,
@ppk@front-end.social avatar

@blemmie This is interesting, and I didn't know that article, but it relies on people understanding what utility classes are, and I wasn't planning to delve that deeply into CSS architecture.

Note made, but I'm not sure if I'll add this.

blemmie,
@blemmie@mastodon.social avatar

@ppk I also once attended a talk where I believe they made the case for !important from a performance point of view, but my memory fails me on that one.

sil,
@sil@mastodon.social avatar

@ppk interrupting animations?

ppk,
@ppk@front-end.social avatar

@sil ... I don't think that was it, though I maybe should mention it.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • CSS
  • ngwrru68w68
  • rosin
  • GTA5RPClips
  • osvaldo12
  • love
  • Youngstown
  • slotface
  • khanakhh
  • everett
  • kavyap
  • mdbf
  • DreamBathrooms
  • thenastyranch
  • magazineikmin
  • megavids
  • InstantRegret
  • normalnudes
  • tacticalgear
  • cubers
  • ethstaker
  • modclub
  • cisconetworking
  • Durango
  • anitta
  • Leos
  • tester
  • provamag3
  • JUstTest
  • All magazines