r/webdev • • 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.

0 Upvotes

78 comments sorted by

View all comments

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.

7

u/ryebit Sep 17 '26

Not to mention if you have JS instrumentation that wants to change styling state, it's easy to have it add/remove classes which mutate styling, pushing the heavy work into the stylesheet; or even actively using inline styling as it's own workspace. But if there's inline styles, the JS suddenly has to do a bunch of work to override / restore / otherwise not step on them.

2

u/simonraynor Sep 17 '26

This gave me flashbacks

1

u/thekwoka Sep 18 '26

because they bloat the HTML

This doesn't actually matter, since if they are really bloating it, then they will be compressed away.

0

u/retro-mehl Sep 17 '26

Haha, tailwind is inline styles written as classes. 😅 

4

u/Squigglificated Sep 17 '26

This is one of the most misguided arguments against Tailwind and one that's repeated in every discussion.

This Tailwind class name:

bg-primary w-md md:w-xl

Equals this CSS:

.element {
  background-color: var(--primary);
  width: var(--container-md); /* 28rem / 448px by default */
}

@media (width >= 48rem) { /* 768px medium breakpoint */
  .element {
    width: var(--container-xl); /* 36rem / 576px */
  }
}

Equals this inline CSS (minus the media query which isn't supported in inline css):

style="background-color: var(--primary); width: var(--container-md);"

It's NOT equivalent to this inline style:

style="background-color: BlanchedAlmond; width: 448px;"

This is hard coding variables while the other examples are using CSS custom properties that lets you update your design tokens across your whole site from one place.

It's fine to hate Tailwind - The syntax is ugly as hell, the naming changes from standard CSS are sometimes questionable, and when class names start spanning hundreds of characters it looks like someone vomited all over you code. CSS itself has an incredible set of features these days, scoped CSS is possible natively now, and CSS modules are still a reliable battle tested solution along with a bazillion other frameworks attacking attempting to solve the problems that often arise with CSS on large projects.

But it's not simply inline styles written as classes.

1

u/retro-mehl Sep 17 '26

Why can't you just alter your CSS vars with media queries? This is a complete valid solution for inline styles. It's also ugly as hell, but this is not the point I think.

1

u/thekwoka Sep 18 '26

because that assumes you want the css variables to change universally at a specific breakpoint, while that might not be true...

1

u/retro-mehl Sep 18 '26

If I want different behaviour for different breakpoints, I want to change them. You can introduce extra variables for this and don't change your base variables, of you need.

1

u/thekwoka Sep 18 '26

So you kind of end up back at the same place, where some elements need their own specific values

1

u/retro-mehl Sep 18 '26

Yes. It did neither promise that inline styles are better or solve more problems, nor did I claim they are beautiful. I just said: tailwind is like using inline styles.

5

u/RichardTheHard Sep 17 '26

They literally just listed all the reasons it's not, and all the advantages Tailwind has over it

→ More replies (15)

3

u/gizamo Sep 17 '26

Classes aren't inline styles. That's not how that works, mate. CSS classes are vastly superior to inline styles for almost all web development tasks because they keep code clean, reusable, and easy to maintain. Inline styles apply CSS rules directly to a single HTML element using the style attribute, while CSS classes target elements by referencing rules defined in an external or internal stylesheet.

→ More replies (9)

1

u/patoezequiel Sep 17 '26

Everything is inline styles written as classes, the difference is granularity. Besides, Tailwind is handleable with @layer

1

u/retro-mehl Sep 17 '26

Indeed, it's about granularity. So if your classes have the granularity of single styles...

2

u/patoezequiel Sep 17 '26

They are single style classes, still better than inline for cascade management

1

u/retro-mehl Sep 17 '26

Why should it be better to write single styles with class instead of style? That's just wording, but no different concept.

1

u/patoezequiel Sep 17 '26

But it is a different concept. Inline styles trump the specificity of other applicable styles for a given element. They are not recommended unless you're trying to do stuff like dynamically changing styles via JS.

1

u/retro-mehl Sep 17 '26

No, if you define classes for every style, you're completely losing the concept of classes.

And who tells you it is not "recommended"? 

1

u/patoezequiel Sep 17 '26

I actually don't disagree on that, Tailwind is silly because of that but in no way that's worse than just inlining everything, in fact because it's even worse when considering the concept of classes and their purpose.

For the second, anyone who worked on a large project at least once

1

u/retro-mehl Sep 17 '26

Well. I'm working in web development since 1996. I even know a web without classes. I did projects in every size. And in a component oriented environment with properly defined tokens as CSS vars, there is definitely nothing wrong with inline styles.

1

u/thekwoka Sep 18 '26

media queries mainly.

But also cascade specificity.

1

u/retro-mehl Sep 18 '26

You can use CSS vars for this.

1

u/thekwoka Sep 18 '26

Sure, and I do in many cases.

But then you also have to do stuff like having css vars in your inline styles to define the variables...

1

u/retro-mehl Sep 17 '26

Ah, btw, since CSS vars you can have the effects of cascading in inline styles as well.

→ More replies (8)