CSS's !important was added because of laws about font size for some text
twitter.com
twitter.com
So “screw it, this works” seems to be an entirely justifiable methodology.
Edit: I've just founded the "Screw It, This Works"™ Alliance. Sign up today to become a certified SITW Master.
This is a sub-domain of Fuck Users, Ship Excrement, or FUSE-Dev.
Where is the conference and the website to download a frameable printable "Certified SITW Master" certificate?
Then you can have the SITH Alliance and give out SITH master certifications.
I've become more aware over the years that some developers, especially very senior ones, are under the fatally mistaken assumption that the problems start when the complaining starts, when it's closer to the truth to say that the real problems start when the complaining ends.
There can be bad developers on any team of course, but there are many developers who know or suspect what they are considering doing is wrong, and discover that asking someone about it is far more trouble than its worth. They either get yelled at or saddled with a bunch of homework because they've drawn attention to a can of worms and are being deputized to fix four things instead of the one they knew about.
Every project has problems. Even the ones that are making millionaires out of relatively new team members. If you aren't hearing about those problems, that's entirely, 100% your fault.
Oh, no, when the complaining ends, the problems didn't just start, they reached the magnitude where they are unsolvable long enough ago that no one even harbors any glimmer of hope of fixing or even meaningfully mitigating them.
I'm still trying to work out how I communicate to someone that the things I have stopped mentioning might still be bothering me, and so fixing the two things I'm actually harping on won't magically make me happy. Maybe that's just something people are supposed to work out for themselves. Mostly I seem to have figured out how to do this when three or more people are involved (basically hand all of my fodder to someone who is still engaged, and let them run with it), but not when it's one on one. Conversations with people who can't 'look at things' can be exhausting, and that seems to hold no matter which party you are.
I really like this quote, and I'm gonna reuse it in a slightly rewritten fashion. In SE teams, indeed, the amount of complacency is directly related to the amount of increasing technical debt.
Its often a pointless effort, especially if the problem boils down to "this architecture isn't pure enough"
There is a gradient I've seen with IT workers in general, that is doing IT as a passion and doing IT for a job. Disciplining someone for not knowing enough though is a bit much.
> real problems start when the complaining ends
By this point no one cares anymore and the technical debt is beyond fixing.
Nicely put. I've stressed over finding unused rules, restructuring, etc. but in the end it never matters as long as it displays correctly.
In addition, there is the difficulty of inspecting those sites which you can identify to verify that display is correct, which is hard to achieve programmatically.
- Avoid cascading (i.e. no nested selectors except when there's "no other way")
- Implement separate selectors for styling and page/component layout (separation of concerns)
- Using a good naming convention and stick with it
The first point can also be achieved with using style attributes if that floats your boat, although Svelte kind of gives you the best of both worlds just by compartmentalizing the CSS, not necessarily by preventing cascading.
I can't remember the last time I ever had to use !important except when I had to work with some ridiculous CSS "framework" my predecessors chose to use. At least !important was understandable back in the days when it was really hard to achieve some things with CSS, but these days I believe there's little to no excuse.
Importing HTML files as modules with localized CSS+JS as web components would be one such option for example.
Yep. I find of the usage of !important I see daily is failure of the programmer to understand precedence rules, which, as you pointed towards, is mostly due to cascading.
[1] Why would you want to do this? Well, what about layout? And what if you have a component that wraps or otherwise behaves like an HTML element?
[2] Of course Tailwind supports modifiers, which do cascade, but everything is still local to a single element.
I don't feel like I have any idea how to write maintainable legible changeable CSS. Like, it's not just that I can't afford the time -- I just don't know how to do it, despite all my efforts the CSS becomes monstrous, always.
I don't think this is true. The biggest problem is people who write CSS do it as if they want CSS to be something else and are trying to force it to be that.
Writing CSS using ITCSS (Inverted Triangle CSS) [1] solves so many of the issues devs complain about it. It's simple: create layers in specificity order. Don't fight the cascade; make it work for you.
Writing good CSS isn't about getting the syntax right; it's about getting the architecture right [2].
Harry Robert's video does a great job of describing all the pain points ITCSS solves [3].
[1]: https://dev.to/carlillo/understanding-itcss-real-case-using-...
[2]: https://www.xfive.co/blog/itcss-scalable-maintainable-css-ar...
I think it’s hard to hire lots (…hundreds) of developers who’s going to know CSS well enough to not screw it up.
THis is made significantly easier when you have designers working with this "design library" mindset also.
If you're explicitly trying to overwrite another rule that you do not control, `!important` is more idiomatic than the alternative. (If you've lucky enough to have never had to do this, the alternative is adding otherwise pointless qualifiers to your selector, since CSS gives higher priority to "more specific" selectors. e.g `html > body > div {...}` takes precedence over `div {}`)
If you explicitly want to change one element in every place, though, !important comes in handy.
is that it assumes the cost of changing the "whole cascade" is minimal. And that it would be easy to design an initial cascade which works perfectly for your needs without any changes needed as the design evolves. In other words it assumes the classical waterfall method where the spec and design are complete and never-changing once the "implementation" phase begins.
The suggestion is that there is nothing wrong with CSS, only in developers who can not come up with a perfect CSS-design before they start coding.
A big problem with CSS in my view is precisely its cascading nature. When you change anything anywhere it can break things in many other places, and there is no good tool as far as I know that would tell you what all can change because of any given change in a single CSS-rule. It is assumed that developers should have such a tool in their brain, and if you don't you are not a worthy developer. :-)
If there is a good justification for misuse, that would logically mean the original intent isn't applicable to real world scenarios -- which doesn't make the intent incorrect, but it does betray short-sightedness. To claim misuse strictly because it's outside of that extremely narrow original vision, is completely disregarding the actual logistics of front-end development.
A better claim would be "I don't see any good reason to use it". Just because one person doesn't see any good reason to use it doesn't mean there isn't one, or two, or three, or more.
It is always very hard to prove a negative ("There isn't any such that ...")
At one place I worked (a subsidiary of NTT), the effort to clean up the CSS and remove all the !important overrides was so time consuming management wanted to make sure it never happened again, so using !important at all would affect our performance reviews. We were expected to fix any bad CSS right away without overriding the default cascading rules.
True that, but sometimes one has already reached that point, and yet must make this change anyway somehow. But I don't recall using "important" in years, so I guess it's not that often.
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.I also think it's the wrong quick fix. What you want is a rule of increased specificity. Better use a unique id, maybe in conjunction with an element selector.
!important rules should be avoided like the plague
I am no web developer and I honestly don't like html/css, even if I use it nearly exclusively for visualization by now. There is so much baggage that you basically have to know to be able to create efficient solutions. Wanted to visualize some data where you basically color small cells in a larger table. I used a HTML table tag and styled it with CSS. Big mistake! It was incredibly slow for something very simple, I had perhaps 50,000 cells. Little did I know about the background calculation of html table cells and I also did not know why no browser version of Excel derivative uses that tag to implement their grid.
Of course you can say that everyone knows this. Doesn't change the fact of the severe baggage.
I want to be able to mark up my CSS sources with HTML tags, and style it with CSS, so it's beautiful self documenting literate code!
Imagine how easy and pleasurable to read and understand CSS would be you could simply use CSS in your CSS to format "font-weight: bold" in bold, and "font-style: italic" in italic!
Not that it should never be used. It’s like gotos in C code. They definitely have a purpose but if you find you’ve implemented more than half of your program control via gotos you’ve probably made your codebase into a horrific mess.
Well said.
There's also no mention about !important having that specific purpose in the CSS spec, nor the tweet discusses why other already existing solutions would've not fit that use case.
!important-10 > !important-9 > ...
we need more important stuff!
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.
Replace structure with users, environment or other factor you can't control and you'll realize you must hate every other aspects of development :)
but you also have instrumental goals, like you don't want to relearn how to reinvent the wheel every time. you don't want to figure out from first principles every time how to transmit style information next to HTML in a concise way, to apply that to the DOM, etc.
hence the tools of the trade.
and there's an endless iteration of this problem. that's why every new version of each tool helps to do certain things faster, in an easier way, with less code. usually tools focus on trendy things, and as a consequence other things usually become harder to accomplish with the tools (simply because the tools become more complex, their documentation becomes bigger, etc)
there are certain abstractions that are very powerful, work well, sort of simple to grasp and compose (Turing machines, loops, map-reduce, structural pattern matching, types / interfaces, distance vector routing & IP addresses)
and the cascade is somewhere along the spectrum of tools.
sure, it's far from perfect, but learning the basics is okay, it gets the job done, so it's a quite expressive tool.
- css BEM with class only selectors
- css modules
- functional librairies (like tailwindcss)
with those you first write your html, then "apply" css.
Nobody would change private properties in a js class from outside that class. Same to css, you should not change css attributes from parents. Those are private to their component they are rendering.
If you need that, provide a public attribute to the component and he'll change it's design.
Do this, and css is very maintainable.
https://www.w3.org/TR/css-cascade-5/#important
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."
The example or use case they describe doesn't even make sense to me. Nor does it even seem to line up with how CSS specificity or cascade works.
"Here's a paper to write on, oh wait why are you drawing, this is a writing paper, you're doing it wrong!"
This is how I felt reading it as well. IMO Steven should exercise a little humility.
It was bad design on his team's part. How is anyone supposed to know that there was only one super secret reason for "!important" to exist? Why not call it something like "!break-cascade-override"? They put an emphasize on making the language look "clean" over real world usability and this is the outcome.
> Alas not. I wish there had been an agreement that after some number of years the records would be made public.
https://twitter.com/stevenpemberton/status/15059585359763456...
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.
or the React way, "!enforce--only-use-this-for-legal-font-size-or-you-will-be-fired"
> This CSS feature improves accessibility of documents by giving users with special requirements (large fonts, color combinations, etc.) control over presentation.
> 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.
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.
If the originally purported intent had carried through to the specifications, it adds up in a sense. If !important overrides indicated an explicitly set formatting/sizing for compliance/legal reasons, implementers touching it without any historical context would know at a glance what it signified and that it shouldn't be touched lightly without explicit approval. And when the implementer updates the stylesheets to use 24px text on a page based on direction from the CEO/designer and the element with an !important override stays at 26px, the implementer can point to the !important override as the cause and confirm if the person requesting the change has confirmed legal/compliance approval to change that one as well. Even if the parties involved in the change (the designer, CEO, or implementer) aren't familiar with the compliance reason for the override's origins, the fact that it's there is a known flag that changes can lead to potential violations and costs/fines and need to be properly vetted for approval.
It would also make it easier to go in and update the stylesheets to comply with changes in compliance/legal requirements, as you can easily search through the styles for !important flags and find all the spots that need reviewed for potential change needs.
That said, its use in practice is completely different than this purported rationale for the flag's existence, and !important is sprinkled so cavalierly in existing codebases to crudely but effectively get a desired effect that the above situation is impossible at this point and the original rationale is moot.
Someone predicting "stylesheets all over the place" would have been exactly right, though - practically every single news page on the Internet has questionably-sourced ads, Facebook like buttons, Twitter embeds, and chat/discussion/engagement widgets, all of which have custom stylesheets. The only saving graces come from frequent use of iframes for isolated embeds (which will become less common now with cookie restrictions!), stylesheets well-engineered by well-funded corporations to have every rule prefixed by a custom and dedicated class name for compatibility, and preprocessors and CSS-in-JS that would make that latter approach viable.
Now imagine a world where you're a site operator, !important doesn't exist, and you cannot trust that a site component provider will not install a global CSS rule that will break your legal or branding requirements, with no real way for you to work around it if they do, without unmaintainable specificity boosters for your own components. You simply won't use that site component provider unless it's well-vetted. Which means that there's significantly less innovation on the Internet, because "nobody ever got fired for embedding IBM CSS" becomes an actual thing to think about. !important becomes the escape hatch that the web needs.
You are fundamentally unable to force the user to render the text you send them in a particular font size, !important or not. What if they use a text browser and small font size in the terminal?
The legal requirement explanation makes 0 sense.
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.
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.
So I doubt it has too many CSS issues in comparison? But haven't looked.
on edit: also lots of accessibility requirements of course.
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.
I think because frontend has a reputation for being easier, there's this assumption that if you can do the 'harder' thing, you should be able to pick up JS and CSS pretty easily.
But the truth is, what separates a great frontend engineer from an ok one is mostly knowledge of the history and the ecosystem. The web is a big stack of compromises. Knowing why things are the way they are is one of the only ways to make sense of the frontend. That and just actually doing it.
And here I thought it was related to getting the best result (most value for the user/customer) in a set of constraints such as requirements, team and time...
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.
Also, nobody really asks you to do complex things with CSS. Unless you use a huge framework (that I tend to avoid, I prefer writing everything from scratch) you don't need too much rules to make a page look good. In 200 lines of CSS you can get a good style for your site.
The problem is that people:
- think that frontend development is easy and have less dignity than other kind of software development - don't want to spend time learning CSS since it should be simple - don't know CSS and don't understand it - use a framework like Bootstrap because they don't want to learn CSS - complain that CSS is shit because they have to use a ton of !important to make things work
CSS is easy if you dedicate enough time to learn the basics of it, and start to make some stylesheets from scratch without Bootstrap or other shit.
Every document production library that handles HTML (a very popular feature) has had to implement HTML/CSS parsing.
I and many others are using HTML and CSS to declaratively lay out complex , dynamic documents (or trying to at least). Seems to make sense but we have ten more years to go before it's fully solved. Right now it costs serious coin to do it (unless you are releasing FOSS) and it ain't fun.
Turns out, it is. CSS is Turing Complete: https://notlaura.com/is-css-turing-complete/
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.
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.
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
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
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.
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.
"Global all the things" seems to clash with "component".
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?
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.
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.
We're only hearing about the "original intent" now, via a smug tweet in 2022. It doesn't say much about someone who insinuates people "don't understand the cascade" if they don't follow an undisclosed design spec.
And if they wanted it to be for font size only, they should have made it work only for fonts rather than any property.
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.
!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.
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. <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.https://www.reddit.com/r/uBlockOrigin/comments/ce4mok/how_to...
https://nitter.net/about offers a better experience IMHO, because it's fast.
Is this what the death of the open web feels like?
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/12ab8664a30538570d395...
[2] https://github.com/gorhill/uBlock/blob/12ab8664a30538570d395...
The law itself does not prescribe specific font sizes, but ensuring minimum font sizes is necessary to comply with the law.
Sounds made up
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.
NFC.
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?
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.
- 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.
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.
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.
Why isn't it "important!" ?!?
What kind of twisted soul decided to put the exclamation mark at the beginning?
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.
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.
>> PATH=/special myscript.sh
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.
So the exclamation mark basically takes on its 'linguistic meaning' that this particular definition should be given extra emphasis.
I was interpreting the "!" in this conversation as it is often used in tech, as a "not-" prefix. :)
Thanks.
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)
.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.
For reference, Tk, which was conceptually a decent GUI toolkit ( https://en.wikipedia.org/wiki/Tk_(software) ), was launched in 1991.
CSS was launched in 1996...
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
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.
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.
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.
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.
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).
So the argument doesn't quite work.
[1]: https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
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.
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
On a side note, CSS3 being accidentally Turing complete is probably one of the funniest (read: dumbest) things out there.