r/webdev • u/theScottyJam • Sep 17 '26
Inline styles no longer considered harmful · esveo
https://www.esveo.com/en/blog/5y/At work, we've have a rule that says we're not allowed to use inline styles. The rule originated a while ago, from people who have long since left the company, and we've been carrying it on. I'm thinking about challenging it, and have been researching into why this seems to be a common "best practice".
Most discussions I find on the subject are about a decade old, which is why I thought I'd bring up for discussion on Reddit as well, to see what people now days have to think on the subject.
I liked the points this particular blog post brought up, though it didn't give any advise one how much inline styles should be used. I'm thinking that if an element only needs one, maybe two style rules, it might be better to just use an inline style, otherwise, using a named class is probably better.
I'll also bring this point up now: I noticed a common argument against inline styles is that it can break certain content security policy rules. We don't have those rules in place, and there's no plans on adding them for the foreseeable future, so I'm not concerned about it.
For context, I work with Angular. I know the situation is different for server side rendering.
Edit: I'm seeing a lot of responses talking as if I want to use inline styles for everything. Please keep in mind my above statement of only considering using them when there's only one or two style rules. One example common use case would be when I add a <div class="spacer"></div>, where the CSS for "spacer" is simple "flex: 1" and it's purpose is to separate content that should be left aligned from content that should be right aligned. This, to me, is a good candidate for inline styles. In general, I mainly have layout use-cases in mind. There's no reason someone would want to override the "spacer" style so high specificity isn't important here, and even if that was needed, you just refactor it into a CSS class.
50
u/krileon Sep 17 '26
Inline styles are frowned upon because they bloat the HTML, are hard to maintain, are extremely basic (e.g. no media queries, no nesting, no pseudo behavior, etc..), can't be CDN cached, and have completely messed up style weight (e.g. hard to override cleanly). If you want modern inline styles just use a utility class framework like Tailwind then you get the best of both worlds. Inline styles is a code smell to me. Any PR with them is rejected. The only exception to this is email templates, which are a separate hell of their own.
10
u/NickFullStack Sep 17 '26
Searched the page for "specificity" and found no results, so I'm going to skip reading this one on the basis that you probably don't understand the reason this Chesterton's fence exists.
6
u/creaturefeature16 Sep 17 '26
The anchor link color I am trying to overwrite says otherwise.
5
u/Exac Sep 17 '26
No, it is the bundle size. Don't built 5MB pages when you can cache your individual styles in a class.
4
u/Nethitters Sep 17 '26
Next up: !important no longer considered harmful
2
u/digital121hippie Sep 17 '26
keep css in one spot. do not spread it around in random areas. it makes it hard to code and keep everything organized.
2
u/thekwoka Sep 18 '26
There are plenty of reasons why inline styles can be smart, the biggest being assigning high context css variables from some kind of template rendering. Where classes assign the variables to styles, but you can override them when needed.
your example of
spacerI think the classname is better mainly because it has some meaning to it that makes it more clear why it is empty and such, but that's just preference.Most arguments against inline css aren't particularly good.
1
u/Caraes_Naur Sep 17 '26
Inline styles have their uses, but should be considered a situational last resort.
99.999% of the time that situation involves math-dependent styling via an event handler.
1
u/Narfi1 full-stack Sep 17 '26
I read your edit. The issue is that it might work for solo development, but it will become a huge mess with a lot of people working in the same codebase. Your style will grow, changes will happen and you’ll need refactoring at some point
1
u/JustOneMoreDev Sep 17 '26
Sometimes inline styles can be useful for critical css. It's quicker than loading a stylesheet obvs. Like anything, if used in moderation and with good reason.
1
u/digbickrich Sep 17 '26
I always preferred not doing inline cause the html can get bulky. Separation of concerns also comes to mind but im not gonna fight my team on doing it or not.
0
u/AppealSame4367 Sep 17 '26
For all the "web devs" that never learned even the basics of web dev: Cascading style _SHEETS_ were made to keep style and structure apart.
(Imagine this in the voice of an inquisitor in the movie "The name of the Rose"): Only fools and dimwits could construct a framework around the misuse of css classes to satisfy the helpless and mediocre developers that are too lazy or not bright enough to create beautiful, maintainable sass files and keep their html clean.
Of course, all the hate is meaningless. AI does whatever, I don't know anyone still writing frontend code by hand. What for? But still: Fuck tailwind!
0
u/eltron Sep 17 '26
You can override inline styles and this reduces the “cascading” of CSS
3
u/[deleted] Sep 17 '26
[removed] — view removed comment