22 ms·
CSS's !important was added because of laws about font size for some text
- ZYinMD 5y ago"!important" means "not important", I guess the engineers didn't like the law makers so they made this an insider joke.
- randomsearch 5y agoThis is how I read it in my head. This also confirms to me that the CSS designers made at least one very obvious error, not appreciating that ! means negation to anyone with a moderate amount of coding experience, and undermines my confidence in the whole language.
- iambateman 5y ago“Anything else is probably misuse” is simultaneously true and brushes away all the realities of front end development.
- Minor49er 5y agoA lot of people have been pointing this out in the responses. The author has been defending himself for people misreading his tweet when he should have simply left out that last sentence
- rectang 5y agoIt requires discipline beyond the capabilities of many organizations to structure CSS so that it's maintainable. The ubiquity of `!important` is not so much "a sign you may not understand the cascade properly" as a sign that the cascade is a flawed design that falls apart in practice.
- nawgz 5y agoStep 1: Write component-based UIs Step 2: Give each component a unique name Step 3: Repeat this principle even for building routes Step 4: your CSS will struggle to clash, no matter how hard you try The cascading is pretty nice actually. Think about using CSS vars... you can dynamically theme apps because of the cascade, by changing just 1 class. there's other nice attributes, and it's true it requires discipline. Just not nearly as much as you imply
- rectang 5y agoWhen hunting down a function you want to deprecate/remove in your private imperative language codebase, you can probably find all call sites just by grepping — and even if you miss some, in most cases you'll get a hard error of some kind (either compile time or runtime) after removal. But with the cascade, call sites are implicit — and removal, for better and worse, doesn't cause a hard error. All it takes is a single rogue engineer or product manager to mess things up. And then cleaning things up later is difficult because of all the places that have to be inspected.
- shadowgovt 5y agoStep 2 demands there be a way for one developer to control the global namespace. That's completely impractical[0] given inclusion of 3rd-party code and no enforceable standard for imposing step 2 on other developers. (Web components now exist to facilitate this, but of course, web components are relatively recent; most of CSS's history did not support this 4-step process). [0] To be fair, maybe completely is too strong an assertion. I've seen something like it done... A compiler rewrites the CSS selectors and the CSS classes to create a sort of top-level namespace by prepending some garbage per module so that no component can accidentally reference another component. It's not great for debuggability (about as much fun as having to deal with the actual entrypoint names in the binary for constructors and overloads when debugging C++), but it gets the job done.
- nawgz 5y ago> Step 2 demands there be a way for one developer to control the global namespace I mean, I just prepend all my component class names with my library name or a shorthand acronym, so 100% of my styles are like `.NawgzInput {}` or `.NawgzGraphNode {}` I have never seen the limitation to this approach (i.e. the ends of its ability to scale), so long as you don't pick a super generic prefix. So to hear you assert this approach is "completely impractical" to someone who has used it to build their own component library that every application they build is entirely comprised of.. rings hollow
- BitwiseFool 5y agoVery few clients care about how "proper" the CSS is behind the scenes. I'd also venture that they would not want to pay for proper CSS (and the expense of expertise that comes with it) when a single attribute would deliver the same end result. !important actually seems like a practical and valuable attribute in this regard.
- datavirtue 5y agoIt comes in real handy when the css hits about 300k lines. True story.
- deleted 5y ago[deleted]
- asjdflakjsdf 5y agoI hate that the general conversation around !important is simply: "Important is bad. You should never use it". Its more of a belief rather than knowledge at this point. I'd be interested to hearing a good answer as to why one wouldn't use !important on a modifier class such as ".padding-20" or even something like ".full-width". Surely the problem lies with your html composition if either of these classes are being used on an element that does not need them?
- iambateman 5y agoIt’s a developers version of a “wive’s tale.” Over time, accrued wisdom has turned into a rule of thumb such that Grumpy Old Developers sit around the fire with Young Energetic Developers and say “ya know, child, never use !important, it’s bad design.” The web development industry is getting mature enough to even have this kind of wisdom, which is wonderful, even if (like “wive’s tales”) the principle is overstated.
- 10000truths 5y agoI remember having to use !important statements to override styling when making custom CSS themes for subreddits, back when old Reddit wasn’t old.
- savanaly 5y agoEngineering is doing what you can with what you've actually got. If what you've actually got is an inherited tremendous ball of spaghetti css, a need to change just one thing on the page, and scant prospects for future changes in the area, then it may be good engineering to use "!important" to make your change over a more complex solution. This is in fact the situation I find myself in at work somewhat often, although luckily I can't recall the last time I had to use "!important". But I wouldn't issue a blanket disendorsement of it.
- soperj 5y agoYou should probably use specificity first, then important, just in case you need to over-ride it in the future.
- calibas 5y agoThis, if you understand the CSS rules for specificity, you almost never need to use !important.
- thrwy_ywrht 5y agoOn the other hand, using ever-increasing specificity to emulate !important is about as elegant and semantic as using the hypothetical properties !importanter, !importanterer and !importantererer
- calibas 5y agoEver-increasing specificity is how CSS works, to call that "emulating !important" is misleading. I'm suggesting using CSS as intended, the cascading part of cascading style sheets, instead of relying on a troublesome override. Edit: Take this example: div {font-size: 60% !important;} If I can't control the order the CSS is loaded, and I can't change that line directly, how do I modify it? Like this: html div {font-size: 70% !important;} The use of !important isn't some simple way of bypassing ever-increasing specificity. In many cases it just makes things more complex.
- politelemon 5y agoWhat's the source for this, I do see they're the CSS co-designer, was this requirement originally written somewhere? I'm looking at the description of important, and I don't see this intention carried through. https://www.w3.org/TR/css-cascade-5/#important https://www.w3.org/TR/css-cascade-5/#important https://www.w3.org/TR/css-cascade-5/#importance https://www.w3.org/TR/css-cascade-5/#importance If the intention is indeed as described, then the CSS designers failed to communicate it properly, and not "a sign you may not understand the cascade properly."
- sfblah 5y agoIf the intention had been to comply with laws, why use “!important” as the keyword?
- akersten 5y agoFor the same reason we call it "accessible entrance" and not "government-mandated alternative door with ramp." Making the phrase more general is just good design.
- bigyikes 5y agoBut nobody is saying that accessible entrances should only be used for disabled persons and nothing else. Here, the author is asserting that there is only one valid use case of !important, and that it shouldn’t be used in the general case. If it shouldn’t be used generally, having a general name is bad design.
- jraph 5y agoIt should have been "!legally-required", or "lawful-font-size:" or the React way, "!enforce--only-use-this-for-legal-font-size-or-you-will-be-fired"
- isitmadeofglass 5y agoLikely because they felt the law is “not important”.
- iforgotpassword 5y agoI agree with that tweet there: > If you need to set font-sizes with !important you also don't understand the cascade If law requires you to make certain text a certain size, just do so. Without any more background information, this explanation for !important doesn't make any sense.
- zerocrates 5y agoI don't doubt the explanation but agree it doesn't totally make sense: an !important-set font-size is not really any more "guaranteed" than another one, as it can just be overridden with another more-specific or later-appearing !important style. So you still have to look at all the styles to be sure you're complying, and if you're going to do that then you might as well just not use !important at all. Though I'll admit that once you have your "mandatory" size style as !important you'd only have to look at the other !important ones to be sure, so there's something to it. Of course there's lots of perfectly reasonable uses of !important now: dealing with other peoples' uses of !important.
- amalcon 5y agoI wasn't there, but putting myself in the headspace of someone who was and may have had to comply with that law: There are things outside of the individual control of the designer that can override everything except !important. The most obvious example is non-!important rules in user stylesheets. Today it's obvious that overriding a font-size with a user stylesheet is a bit of an extraordinary measure. You're unlikely to have any legal issues based on somebody shrinking your text using one. This may not have been obvious in the meetings in which CSS was being designed: it's plausible that I might think user stylesheets would be all over the place and have unpredictable effects, including shrinking disclaimers or whatever. On the other hand, putting !important in a user stylesheet could've seemed like an extraordinary measure even then. But again, I wasn't there. I'm just making semi-educated guesses.
- tshaddox 5y agoStill, it doesn’t make sense why US law makes it necessary to have this feature, but any other business or design requirement wouldn’t make this feature necessary. Surely if the CEO or designer says “this text must be 24px” that’s not any different from the implementer’s perspective as a law which says the same thing. This whole thing doesn’t really add up.
- dredmorbius 5y agohttps://nitter.kavin.rocks/stevenpemberton/status/1505839184287870981 https://nitter.kavin.rocks/stevenpemberton/status/1505839184...
- deleted 5y ago[deleted]
- jiffygist 5y agoWhy is css of nitter.net broken?
- dredmorbius 5y agoSorry? NFC.
- JoelEinbinder 5y agoThe most common use for !important is to override something else marked !important. It’s an interesting situation when getting rid of a feature simultaneously removes most of the need for it.
- silvestrov 5y agoIt should have been added to the selector instead of the attribute: div.legaltext:important { font-size: 12pt; } instead of current: div.legaltext { font-size: 12pt !important; } so it would have been a more general "increase selector priority" syntax, like the opposite of the recent :where(xxx) selector.
- zagrebian 5y agoWhat would :important match?
- sfblah 5y agoMaybe the html tag could include an important flag
- Izkata 5y ago<html id="root"> #root#root#root div.legaltext { font-size: 12pt; } That gives you a specificity that should override everything else. Would only be a problem if some other rule has 3 or more IDs, in which case you can add more here.
- asddubs 5y agocss truly is a beautiful language I'm thankful that I only ever do it in contexts where I control all the CSS and don't have to do silly stuff like that
- zagrebian 5y agoThen it’s no longer CSS and you have to edit the HTML code to apply the important styles.
- rat9988 5y agoYou already have to if you want your css selector to select it.
- null_object 5y agoIf this guy was one of the designers of CSS then I have no idea why he’s acting so smug in that twitter thread.
- TonyTrapp 5y agoTo be fair, I find many of those replies (by other people, that is) painful to read. Completely missing his point. I'd probably also get a bit annoyed after the fifth person pointed out something because they completely misunderstood what I said.
- LudwigNagasena 5y agoWhat is his point? It just reads as blame-shifting. Introduction of an unidiomatic feature into the standard for a particular use case is an acknowledgment of deficiency or incompleteness of the whole standard.
- TonyTrapp 5y agoI read his point being that !important used for anything but the original intent is "code smell", and I'm fully behind that. He doesn't imply that you mustn't use it for anything but its designed purpose, I'm sure he is enough of an engineer to know that sometimes you cannot stick to your ideals. He's probably well aware that some people have to rely on frameworks which already use !important, so they have to work around that by using !important as well. None of that contradicts what he says, but it's still a misuse of the feature.
- Dylan16807 5y ago> I read his point being that !important used for anything but the original intent is "code smell", and I'm fully behind that. It doesn't explain why using !important for the original intent isn't equally a "code smell". Unless that is smelly too, in which case there are even more questions to answer about why this is the workaround and why you're supposed to use it for the original intent despite that.
- AtNightWeCode 5y agoNot really.
- zagrebian 5y ago[hidden] { display: none !important }
- MonaroVXR 5y agoOne the best CSS tricks out here, thank you.
- heavenlyblue 5y ago
- hutrdvnj 5y agoWhat happens in case of multiple contradicting !important statements?
- maxbaines 5y agoIn-line css takes priority.
- cxcorp 5y agoCSS has a concept of "specificity" [1] which is applied also in the case of !important. Basically the most specific rule wins. [1]: https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
- bdlowery 5y agoThe same thing can be said for a lot of things with CSS… The @media rule was added for one reason only: provide different styles for different media types. It wasn’t intended to be used to make responsive websites. Is using it to make responsive websites an incorrect usage?
- theknocker 5y agoShould have called it "mandatory" then.
- akersten 5y agoAnyone have an idea of which part of US Code would require this? I have a hard time imagining it's that prescriptive about font sizes (especially given that different fonts have different glyph sizes, DPI on monitors results in different physical rendering sizes etc.) Only thing that comes to mind is that Apple court case many years back where the judge told them they had to have a disclaimer on their page in a certain size and color as part of the judgement.
- snowwrestler 5y agoMost likely Section 508 of the Rehabilitation Act, which requires U.S. federal agencies to make electronic documents accessible. The law itself does not prescribe specific font sizes, but ensuring minimum font sizes is necessary to comply with the law.
- syspec 5y agoTotally agree, if there is such a law - how come now one seems to have heard of it. CSS was written before ADA, so we can rule that out. Sounds made up
- 5350-uiop-1130 5y agocss "co" designer. that explains a lot. horse by committee. i find many uses. sometimes you need to override a style coming from an external or to apply a rule globally, you can just use !important and know that it will work regardless of individual specificity in all instances of that selector. yes, you could override it with specificity but why bother, a hack is a hack
- omnicognate 5y agoThe only reason !important was added was to troll javascript programmers, who read it as "not important".
- pistoriusp 5y agoisn't ! a fairly common negating symbol in programming languages?
- omnicognate 5y agoYes, it's by far the most common use of "!" in programming (though newer languages are increasingly using it for various flavours of null/nothing checking too). Javascript just seems particularly relevant here.
- LeifCarrotson 5y agoC programmers, and those who write in languages derrived from/inspired by C.
- adzm 5y agoShould have been !!important instead.
- throw0101a 5y agoCan anyone give an ELI5 to us curious non-webdevs about why !important seems to be A Thing? (As a sysadmin I'll probably never have a practical need for this, but it's always fun learning about the various niches in 'tech'.)
- kradeelav 5y agoNot a webdev by profession, but hand-coded a few hobby sites - !important is used as a duct-tape "just do the dang thing" hack if you need to overrule a bunch of the current styling rules and don't want to think too hard about it.
- cxcorp 5y agoCSS stands for Cascading Style Sheets. The cascading part refers to the fact that when you have a HTML element which is targeted by multiple style rules (e.g. browser defaults, style sheets, inline `style=""` attributes, inherited from a parent element's rules, etc.), there's a well defined priority order which decides which style "wins" and is in effect. For example, you might define that all body text is black, but then also have a rule that says that if it's a link (an `<a>` element), it's blue. Suppose you want other links to be blue, but the links in your navigation bar should also be black. Can do, just specify `nav a { color: black }` - now rest of the links are blue, except inside your navbar. This is neat, because with relatively concise notation, you can specify styles for the majority of your site, but can also easily specialize styles when you need to. Compare that to manually setting `style=""` attributes on every single HTML element! So what's the problem with !important then? !important is #1 in the priority order I mentioned previously, so when you add !important to a CSS declaration, you throw out the first C from CSS. You're saying "I don't care what any other stylesheet or style rule says - ignore them and use this rule". To beat !important, you need to also add !important, and additionally make your CSS rule more "specific" than the other, e.g. `nav a { color: black }` beats `a { color: black }` because it's more specific - "target <a> inside a <nav>". This becomes very problematic if e.g. a third party component uses !important in its rules. Let's say that third party component's stylesheet overrides all <a> elements to be red. Great! Every single link is now red - including the ones in your navbar. You'll now need to go back to your own rules and add !important there - meaning now you need to use !important in every single declaration to get the C back into CSS.
- beardyw 5y agoHow can a font size be subject to legislation in anything CSS controls? It won't absolutely define the size on screen or on print, will it?
- francisofascii 5y agoCSS !Important is a nice fail safe or safety valve for emergencies. It is good design to have safety valves in a system. Sure, you should not be encouraged to use it under normal circumstances. As with other fail safe features, it is meant for extreme scenarios you can sometimes predict, but more importantly for reasons you CANNOT predict. So this tweet arguing that it is meant for one reason is invalidating why it is good design.
- meerita 5y agoFor those tired of dealing with any OOCSS variant, you can go full functional css http://minid.net/2019/04/07/the-css-utilitarian-methodology/ http://minid.net/2019/04/07/the-css-utilitarian-methodology/ and say goodbye to !important
- pajko 5y agoAfter blocking popups on some sites, the scrollbar is dtill hidden, making it impossible to scroll (like on twitter, for example). With the help of !important, the scroll bar can be forcibly turned on by adding uBlock cosmetic filters. Works on per-site basis, but can be added globally, though some sites break a bit, like the top header on Wikipedia starts behaving strangely. https://www.reddit.com/r/uBlockOrigin/comments/ce4mok/how_to_reenable_scrolling_after_blocking_element/ https://www.reddit.com/r/uBlockOrigin/comments/ce4mok/how_to...
- iicc 5y agoIf you zap enough elements on Twitter the scroll bars come back anyway - so !important must be unnecessary for that site. https://nitter.net/about https://nitter.net/about offers a better experience IMHO, because it's fast. Is this what the death of the open web feels like?
- gorhill 5y agoZapper is a special feature, entirely unrelated to cosmetic filtering. When you zap an element, the element is removed from the DOM, and the zapper code will also look at the DOM to find out if any of the parent elements are scroll-locked, and if so will defuse it with an inline style using `!important`.[1] As for cosmetic filtering, it's is the hiding of elements using `display: none !important`, so `!important` is also key to ensure page styles are overridden.[2] --- [1] https://github.com/gorhill/uBlock/blob/12ab8664a30538570d3952e0a61f4b6863d2dc68/src/js/scriptlets/epicker.js#L967 https://github.com/gorhill/uBlock/blob/12ab8664a30538570d395... [2] https://github.com/gorhill/uBlock/blob/12ab8664a30538570d3952e0a61f4b6863d2dc68/src/js/cosmetic-filtering.js#L1079 https://github.com/gorhill/uBlock/blob/12ab8664a30538570d395...
- stefan_ 5y agoInvoking the cascade to explain why there are 15 ways to center a box, of which 10 don't work one time or another.
- oblio 5y agoI can't find if Tk had this in the original version, but I suspect that it did. For reference, Tk, which was conceptually a decent GUI toolkit ( https://en.wikipedia.org/wiki/Tk_(software) https://en.wikipedia.org/wiki/Tk_(software) ), was launched in 1991. CSS was launched in 1996...
- thorum 5y agoCSS Cascade Layers are now supported by all major browsers and provide a good, standard alternative to !important in cases where you need to override CSS that you can’t easily modify, like stylesheets from a third party CMS or framework, or inherited spaghetti code from an earlier version: https://css-tricks.com/css-cascade-layers/ https://css-tricks.com/css-cascade-layers/
- wvenable 5y agoIt's getting hard to know everything that is in CSS now.
- ilaksh 5y agoYou just decided that in 2022? Lol.
- oefrha 5y agoNow supported by the latest version of all major browsers. Seriously, only one version of Safari, Edge, and Android Chrome support it. Expect to use it in a few years when the older iPhones are no longer relevant. https://caniuse.com/css-cascade-layers https://caniuse.com/css-cascade-layers
- jansc 5y agoI always thought of important as the sudo of styling.
- JoeyBananas 5y agoI've used !important plenty of times when I had no idea what it actually does just because some random tutorial suggested it
- jstummbillig 5y agoThat's fine. It's really not that important what a designer of CSS thinks about how any part of CSS is being used. A guitar player does not confer with the guitar builder about how to play the guitar.
- human 5y agoSometimes a tool has been invented to do X but people end up using it for Y because it’s so useful. I think "!important" is a good example. Like duct tape being used for much more than attaching ducts.
- tfvlrue 5y agoThe ironic thing here is that traditional duct tape should _not_ be used for attaching HVAC ducts together. The adhesive doesn't withstand the temperature extremes, and it has undesirable properties if it catches on fire. UL-listed foil tapes are used for this instead. It seems like cloth-backed tape started as "duck" tape (as in duck cloth), somehow turned into "duct" tape, and then turned back to "duck" for marketing purposes. It has quite an odd history.
- dvt 5y agoBragging about "designing CSS" is a bit like bragging you captained the Titanic. I mean, I guess it's impressive, sure, but it's also pretty embarrassing. I've used `!important` many times, as I'm sure many folks did, because CSS just absolutely sucks and it's a huge pain the butt to get it to do what you want. Using `!important` is the equivalent of throwing your hands up in the air and saying "screw it, I wanted to go home 3 hours ago, I'm done." On a side note, CSS3 being accidentally Turing complete is probably one of the funniest (read: dumbest) things out there.
- vlunkr 5y agoI don't read the tweet as bragging
- jmchuster 5y agoIf the requirement was truly only to satisfy ADA laws, then it seems like you should have implemented it as: window.ADA_MIN_FONT_SIZE = 16; This seems more like you were given the requirement and then went: 1) we could implement this is as a custom global variable for this specific use case 2) maybe what we need is to be able to override a single property 3) maybe what we need is to be able to override a group of properties 4) maybe what we need is to be able to define multiple levels of priority and they ended up stopping at (2) when we really needed it to go to (4)
- ivraatiems 5y agoAre you referring to "window" the JavaScript object? If so, bear in mind that JavaScript is not a requirement for CSS to work on a page, and CSS creating a dependency on JS would break a lot of things.
- francisofascii 5y agoEspecially back when CSS was created, disabling JavaScript was much more prevalent.
- jmchuster 5y agoSure, then make it a meta attribute. Just needs to be a global somewhere.
- rplnt 5y agoRandom offtopic rant: Twitter had decided some time ago that it would show me one notification each time I open the website or app. Probably so I feel like there's something happening. It's always a completely random tweet from an account I don't follow and it is so annoying I'm soon going to log out and just live without the few interactions I do at the site.
- lsbehe 5y agoOne of the reasons I installed an add-on to redirect twitter links to https://nitter.net/ https://nitter.net/
- dankwizard 5y agoIf this is a Chromium add-on, do you mind linking to it? Wouldn't mind trying it out
- deleted 5y ago[deleted]
- LudwigNagasena 5y agoI don’t understand the sentiment. If there is a functional requirement that can only be reliably achieved with that hack, doesn’t it suggest that there are other possible functional requirements that can only be reliably achieved with that hack?
- __del__ 5y agoyes. the moralistic sentiment seems to be that you should only resort to the hack when faced with legal consequences.
- deleted 5y ago[deleted]
- lnxg33k1 5y agoI have the feeling that a lot of these people who are very smart and design things sometimes live outside of the world and don't know mere mortals issues, I might be understanding cascading and important (I don't like to use it either and avoid it), but we need to make sure that we know that we work with some legacy code, as part of a team, some decisions made before us, tight deadlines and pushy managers, I won't use !important, I might understand cascade, but I also don't want to refactor everything everytime I can't use a shortcut, when its simple changes it's fine, when it's not maybe !important + a ticket to refactor in an allocated time is a better approach
- nathias 5y agoit's very useful when you are hacking on top of existing code that you don't have access to, like custom skins ux for websites you use
- Lapsa 5y agoRecently had to unify looks of pagination rendered by 2 incompatible approaches. There is a chance it could be done in more sane ways but I ended up just spamming some very important css rules and be done with it.
- micromacrofoot 5y ago> a sign you may not understand the cascade properly What a load of self-important malarkey, this is like telling someone a hammer should only be used on nails. You failed to consider abuse when you designed the spec. That's not on everyone, that's on you.
- paulodeon 5y agoThis confirms my suspicion that CSS was designed in an ivory tower. Despite being a "good developer" cut my teeth in C++ etc always had difficulties with CSS. The mental hurdles required to understand various concepts bore no relation to anything else I had encountered in CS. I'm sure a notable percentage of my grey hairs are attributable directly to its incredible user-unfriendliness.
- replygirl 5y agoit's not programming, it's markup for a renderer. it's just a different skillset--people who fiddle with shaders in a blender GUI are better prepared to work with CSS than most programmers. i was great at css when i was 12 but wasn't a half-decent programmer until i was 25
- datavirtue 5y agoExactly. CSS is doing too much. All the decisions seemed to make sense at the time but it all ends up being a clusterfuck. Part of it has to do with everyone having to rebuild HTML/CSS parsers from scratch and the ever changing and evolving nature of the markup. Just when you think everything is going to be OK you are faced with feeding your document into another parser that isn't at parity with the browsers. A lot of people struggle to build PDFs from HTML where the parser (itext) only understands a certain subset. That nice grid layout you designed? ...throw that out. You need page breaks with common headers and footers? Get ready to commit some really dirty hacks. Should this data be in a div table or a semantic table? Better predict the future.
- saos 5y agoHTML email enters chat
- soheil 5y agoWhat? no, you can override !important many ways, for example # rules take precedence over . rules, so even if you have important in your . it could get overridden. If you obviously add inline style that will also override it, etc.
- deleted 5y ago[deleted]
- pdimitar 5y agoDamn, there are still people believing that you can just express what the intent behind an engineering tool was, and people will only ever use it if it matches that intent? That would be crazy naive and I hope this person doesn't imply that. Everybody is on a schedule. People reach for whatever is available. Doesn't matter what was the reason `!important` was added, at all. If it solves a real problem -- especially quickly -- then people will use and abuse it.
- deleted 5y ago[deleted]
- deleted 5y ago[deleted]
- Waterluvian 5y agoI think one concept that's slowly coming into focus for me is just how many engineers want you to experience _their_ experience and spend your time reveling at how clever _their_ stuff is. As if they think you wake up and want to do CSS work. CSS is not the goal. The website I'm building isn't even the goal. The goal is to solve some problem. Everything that I have to do to solve that problem is an unfortunate (but inevitable) reality. But let's be clear: the goal is to minimize as close to zero, how much time you have to spend doing these things. CSS, like all code, is a total liability. I want to spend as little time designing and writing it as possible. Which is why I hate CSS: it demands you know the exact current and future (hahahaha) structure of your webpage. `!important` is not some panacea, but I cackle at the ignorance of suggesting that the user is wrong for misusing it.
- calibas 5y agoMisuse literally means "the wrong or improper use of something", so your statement also suggests the user is wrong...
- deleted 5y ago[deleted]
- edgyquant 5y agoBoy this is exactly how it felt to me as a junior. I was just sure it was the fault of everyone else’s configuration that was the problem, not mine. The hardest lesson a junior has to learn is that the customer is always right. No one cares if your program runs perfectly on your machine, because of course it does, it has to run perfectly on every machine.
- ahmedalsudani 5y agoSome machines are configured by the devil himself.
- relscholar 5y agoThis is the concept of Byzantine faults
- saghm 5y agoI don't do any frontend development other than sometimes tweaking custom stylesheets for sites for use with the Stylish extension. I use `!important` all the time when doing this; a surprising number of sites I try to modify end up having some stuff with in-line CSS on HTML attributes, and that's the easiest way to override it. I don't pretend to think this is an intended use case of `!important`, but I think being able to customize the way sites look in ways that I personally prefer is at least in the spirit of the open web.
- oblib 5y agoTo honest, I don't have the time to become a CSS expert so I do what works and I don't worry much about it. I use "!important" quite a bit because I let users made some choices in the color schemes of the app and using "!important" makes that easy for me to do.
- lmc 5y agoPlease, put an id on the HTML element, and a CSS rule targeting that id, rather than use !important. !important really was a mistake and should only be used to counteract other uses of !important you don't have control over.
- asddubs 5y agoalso inline styles you have no control over
- oblib 5y ago>> !important really was a mistake and should only be used to counteract other uses of !important you don't have control over. That's mainly when I use it. Mostly to override colors Bootstrap CSS has defined with it. For example a nav background color set in the Bootstrap file "/scss/mixins/_background-variant.scss": .bg-light { background-color: #f8f9fa!important; } By default I use that, but I also let users select a CSS file to override that with a color they chose from a list of "themes" I've made. My themes only change a small bit of what Bootstrap CSS has described in their themes. It wouldn't make any sense for me to do anything more than that because it works works and it's easy for me to implement the change.
- partiallypro 5y agoThe times I use important is generally to override a framework that is taking precedent, even though the sheet I'm editting is loaded after the framework css. That is 90% of my useage of !important. It may not have been intended for this, but a lot of things in CSS and HTML were never intended for certain use cases that they are now used for. That doesn't mean it is now misused, it just means things have evolved.
- replygirl 5y ago"'gif' is pronounced ____" energy
- bryanrasmussen 5y agoWell, my !important story - at the beginning of 2014 I joined a project that was being done for the Danish government, the JavaScript implementation of NemID which unless you're in Denmark you probably don't know what it is. Styling in this project was done with a sass compilation to CSS, and it had hundreds if not thousands of !important declarations inside it. It seems the guy who was there before me had set up the import order of the Sass files incorrectly from more specific to less specific to some really super specific couple things on top of it all (most of the code base was in Java using GWT to compile to JavaScript, so as a consequence most of the programmers were Java guys and did not notice this), basically I cut 800kb of CSS out of the build in the first few weeks by restructuring and getting rid of !important declarations (for anyone who knows NemID you can probably see how this was definitely way too much CSS for what you need to render!) It was my first time consulting, I sometimes thought, man if I could find the guy who did this (who was I'm told a pretty good designer) I could just follow him around and make money cleaning this stuff up! But alas, I ended up having to work for a living.
- fredsted 5y agoSomeone should do the same with the new MitID implementation! :-)
- bryanrasmussen 5y agoI mean what I see when I use MitID at my bank is basically 2 screens, I can't remember how many screens we had to make for NemID, I think 16 (at the most) and of course they had to be responsive and support Danish, English, and Inuit (Greenlandic) and work the same across a wider number of platforms than most applications. So I doubt it has too many CSS issues in comparison? But haven't looked. on edit: also lots of accessibility requirements of course.
- incrudible 5y agoI would never take advice on CSS by someone who was involved in designing CSS.
- deleted 5y ago[deleted]
- lmc 5y ago> important was added for one reason only: laws in the US that require certain text to be in a given font-size. !important stops the cascade from changing it. But it doesn't. !important begets more !importants. Nice to hear the reasoning behind it, but it's a PITA and should never have been added.
- trinovantes 5y agoOne nice feature modern frontend frameworks have nowadays is automatic component specific css rules IIRC in Vue, <style scoped> allows each element of the component to have an data-{id} attribute and every CSS rule is appended with [data={id}] selector so you're guaranteed no CSS rule leaks out and is contained in the same file
- no_wizard 5y agoDo keep in mind that this does come with a performance cost, especially in larger applications, as attribute selectors are slower than pure CSS classes, at least last time I ran some benchmarks.
- lopis 5y agoThe important statement is also useful is you're creating something to slap on some other code you have no control over. Examples of this from my experience are: - debug tools that hijack the pages style - quickly testing styles on the browser while coding - widgets that are injected into client's websites; client's websites can be a mess, and might contain important statements themselves... - browser themes or custom styles Sometimes it really doesn't matter that! Important is bad practice, if the alternative would be 5x more verbose and confusing. And honestly I don't care what the father of CSS thinks.
- edgyquant 5y agoIt works nicely if you are debugging CSS via the inspector. But if you start slapping important all over the place you eventually converge towards a code base where a majority (or large number) of classes are using important which takes you back to square one.
- oblio 5y agoThe main thing that bugs me about "!important" is... Why isn't it "important!" ?!? What kind of twisted soul decided to put the exclamation mark at the beginning?
- lmc 5y ago¡Sí, que cabrón!
- MartinCron 5y agoI always read it as "NOT important" until I remember that CSS is its own kind of crazy-town.
- bmacho 5y agoBorat voice: True !
- efficax 5y agooh i understand the cascade, but i also have 25k lines of css to deal with and deadlines. Yes, I can track down the full cascade and craft a selector that will specify a rule for just this or that element, or I can slam a !important on there and move on with my life
- deleted 5y ago[deleted]
- sequoia 5y agoIf this was the intention, it's a bit curious the creators of CSS waited a quarter century to share this information. I wait with bated breath for the true purpose of text-decoration to be revealed.
- MartinCron 5y agoI actually remember it from the time. It was part of the discussion why the page's stylesheet would "win" over a user specified stylesheet when there were conflicts... sometimes things needed to be that way for legal reasons.
- kokanee 5y agoCascading only works as intended if you don't have third party styles and if the styling requirements of your application never change. What's funny is that CSS itself is subject to the same problems. It was created to help people add emphasis and layout customizations to text-based technical documents. Now it's used for everything from interactive maps to control interfaces on spaceships. I get why people hate CSS, but I think the underlying issue is that it's impossible to create software that can gracefully do whatever has been asked of it and will be asked of it in the future. It relates to the whole culture of continuous deployment and rapid iteration... a few people are talking about the downsides, but we haven't really reckoned with them as an industry yet.
- nick__m 5y agoIMHO, there is another valid usage: client side css, when a rule is as specific as it can be the !important attribute might be the only way to locally override it.
- nojs 5y agoI think it’s fair to say at this point that the “semantic classes with reusable cascading CSS” experiment has lost to local, component-based solutions. Overuse of !important is just another sign of that. Having “clean” CSS doesn’t have the same benefits as clean, well structured code in other areas. The underlying assumption that you can change the visuals without major surgery to the HTML is basically never true, so if you do put the time in to structure the CSS perfectly it’s often wasted when the design changes a little. Also (as someone said in another comment) the lifetime of CSS is short compared to other application code and certainly compared to backend data structures and business logic, meaning the technical debt isn’t a huge issue. Iteration speed is often more important.
- cphoover 5y ago" The underlying assumption that you can change the visuals without major surgery to the HTML is basically never true," I would disagree. I think an amazing amount can be done with CSS alone, but developers never take the time to really leverage the power of CSS. I think perhaps it's worth making a distinction between web app development, and content, (e.g. marketing, articles, weblogs, personal websites). While there is often crossover between the two I think that CSS was originally designed for styling content, which was the primary use of the web at the time. It fits less well at styling complex interactive component based web applications.
- alwillis 5y agoHaving “clean” CSS doesn’t have the same benefits as clean, well structured code in other areas. I disagree. There are huge benefits to clean, well structured CSS, including avoiding specificity wars and general fragility of making changes. See my other comment with more details: https://news.ycombinator.com/item?id=30761462 https://news.ycombinator.com/item?id=30761462
- Fnoord 5y ago> Anything else is probably misuse, and a sign you may not understand the cascade properly. I use it in userChrome.css in Firefox to hide tabs because I use vertical tabs. I copied that snippet from some FAQ and it Just Works (tm).
- RandyRanderson 5y ago"co-designer of CSS": right up there with "I invented anti-personnel mines".
- dpcan 5y agoSeriously tho, it comes down to dollars and cents and the fact that !important is an option (and not just for font-size). A client says they’ll pay for 30 minutes, the boss won’t let you go over 15, it’s not like there’s enough time to actually wiggle your way through a CRM to drop the proper CSS, so a little !important in the override inline CSS is what happens time and time again. Good developers would love to take the time to do things right, but we don’t always have control over these situations and it stinks.
- taejavu 5y agoIf you find yourself reaching for `!important`, try doubling up the selector instead: .someclassname.someclassname { That will make it one level more specific than just .someclassname, which is usually enough to override styles with a higher specificity, or the same specificity but were loaded afterwards. If it's still not working, add another one! It's a hack for sure, but definitely the lesser evil.
- zigzag312 5y agoIf a simple condition like this requires some special solution, isn't that a sign that there are probably some a fundamental issues with your system?
- s-xyz 5y agoOh my, I used to have stylesheets with more than 500 line, where every single line ended with !important;
- cr__ 5y agoGiven that “the cascade” has been a worthless anti-feature since the beginning, I feel he could have said this in a less condescending way.
- robotburrito 5y agoIs that reason to randomly put it somewhere while under a deadline, find it works, put a //TODO to remind future you to change it later and then forget about it for a year or so?
- asddubs 5y agowell hopefully you'd be putting a /*TODO*/
- dordoka 5y agoIf that was the intended use of !important, I reckon that info would have been an awesome addition to the CSS spec.
- deleted 5y ago[deleted]
- wly_cdgr 5y agoNice, blame the users for your design mistake
- mootzville 5y agoThat tweet is ridiculous. The World Wide Web was created for one purpose only...serve formatted documents across a network for scientists. Using !important beyond it's intended purpose is called hacking, and has been around since before there were computers. People found use beyond the main purpose of !important, and that's ok...it was a dead end, but that's beside the point. Never use !important in your production css at all costs. In development, experiment with it all you want...maybe you'll uncover something amazing...in this case, probably not...but better to hack on things than accept what you're given.
- sytelus 5y agoTweets like this proves why CSS is so badly designed. It is a bad idea to add a feature only to satisfy random government mandate. The committee should have reflected more and abstracted out and implemented something that was more general and widely usable. Instead they implemented hacks and then required people to do more hacks on the top to get things done.
- d--b 5y agoSure man, blame the user. How many hours did humanity lose trying to center things in CSS? How many hours did humanity lose trying to understand if the size of a box is set by the parent or the child? Why not admit that CSS has been a giant shit show since inception, and that !important is just another crappy band-aid feature in that mess of a toolbox? I have been designing APIs for 15 years and I know people misuse them all the time. And I know it’s frustrating. I also know when I have been wrong, and if I were in the man’s shoes, I’d rather be sorry that I created that monstrous piece of technology rather than blaming the millions of developers who just didn’t find any other effing way than to set !important on a random property.
- phasersout 5y agoThe only problem with CSS is that a lot of people use it without understanding the basic rules of the cascade. !important is just one thing another thing is that you shouldn't use ids as in id="myId" for styling because an id has too much weight compared to tags or classes. The only way to override a styled id is with another id - and at this point everybody should realize that no thing should have two identification-tags! CSS is beautiful and extremely logical. I can very much recommend the book "CSS in depth" by Keith J. Grant - by reading this you go from asking CSS questions on Stackoverflow to answering them. edit: just to make clear I made the same mistakes for years.
- DeathArrow 5y agoIf you can find a way to fix other properties without using !important, then for sure you can find a way to fix text size without using !important. So the argument doesn't quite work.
- slim 5y ago!important is the new <blink>
- id02009 5y agoI feel slightly annoyed TBH. If it was supposed to be used only for fonts why does the system allow using it for (almost?) everything else. I'd say people understand CSS very well, but sometimes have deadlines and legacy code and don't have time for redoing the system perfectly (just as Steven and his CSS yeah did not)
- farseer 5y agoIf you are working within another framework such as Wordpress, the !important tag is a lifesaver that allows you override styles you simply cannot change without a lot of effort and understanding.
- randomsearch 5y agoI don’t know if I’m just stupid. I might be below average. I’ve got a PhD in CS, in a fairly easy area. I’d say I’m about an 8/10 coder. I definitely have a very bad memory and I don’t like big languages as I forget their syntax. Things like C++, Scala. But… I’ve been writing CSS on an almost daily basis for three years now, after coming to it cold. And it is without doubt what I spent most time on during frontend dev. I use Vue. Occasionally I Google to check some Vue syntax I haven’t used for a while. But I am endlessly googling CSS. The first thing I look for in a language (probably due to my goldfish memory, or because I’m actually a bit stupid compared to other coders) is a really small set of rules that I need to know to write any program. Lisp or Python easily fall into this bracket. But with CSS, it just seems endless. There are so many many ways to do the same thing, I find myself making arbitrary choices all the time. Without doubt though, the cascading precedence part of the design causes the biggest problems. A single component can have its styling defined across many an unlimited number of files. And each bit of styling might be defined with a different selector, meaning when I change something the specificity rule I use might “work” for some properties and not others. The often subtle interactions between parent and child properties are maddening. Lots of folks might say - well you’re using it wrong, but most decisions are not made by me but rather other people designing frameworks, and given that these problems seem to be everywhere, it seems no one understands how you’re supposed to use CSS correctly. And if that’s the case - well, it has to be a failure to design a good language. In other places in CS (eg some configuration file systems) where similar specificity and overriding mechanism can be found, the exact same confusion, mistakes, frustration, and time wasting occur. Anyone who has spent a lot of time working with those sort of mechanisms would have said - no, this doesn’t work, we have to simplify this or it will lead to a big mess. For a long time I gave CSS the benefit of the doubt and assumed I was a complete idiot. After three years, I’m still spending a lot of time “debugging” CSS in Chrome’s dev tools and whilst I may not be super smart I’m fairly sure that the language has failed me. I’ve learnt some tricks, like only use flex box, and delegate absolutely everything you possibly can to a framework, but I’m not much wiser than day one. I’d love to see someone who really understands PL design to write a replacement for CSS. An elegant design system language. I’m not a PL expert but the sort of characteristics I’d expect would be: concise, not spreading definitions across many files, a very simple or no precedence hierarchy, more akin to a programming language than a configuration syntax, with a type system that enables excellent dev tools and in particular a debugger and sandbox. One telling feature would be that adding new features would almost never mean extending the language, which would remain stable. The !important syntax is either a bad joke or a tell, because no one with any serious background in language design would choose that syntax.