Hacker News new | ask | show | jobs
by ehnto 1547 days ago
First up, agree with the author. Now if I may, a rant.

It boggles the mind that we style bikeshed over CSS. Is it honestly, truly, and pragmatically worthwhile to spend all this engineering effort manipulating, culling, tree shaking, modularizing our CSS? Pre-load that, inline this, async-load that, and what have we got for it? A web full of wiggling content, slow CDNs, dozens of requests, and an experience of a mis-timed, slow loading web of bullshit.

Deliver less CSS in the first place, be pragmatic, load it as one file when the user hits the website, from the same server they got the HTML from. Bam, lightning fucking fast, like it was before we invented all these new problems.

The brief flash of your half-styled website is useless to me. Block rendering until the CSS is done. I don't enjoy looking at half an interlaced video frame, or 100px of a 200px image, just show me the finished product.

The content wiggles and asynchronous bullshit is what makes your website -feel- slow, even if you managed to trick Google into thinking it loaded quickly.

6 comments

> worthwhile to spend all this engineering effort manipulating, culling, tree shaking, modularizing our CSS?

You don't have to ever think about this stuff if you use a component-driven frontend framework like Vue w/ Vite (or Webpack).

The general design: you use one global .css file for your shared styles (app.css) and then each component has their own inline CSS.

Vite will automatically extract each component JS and CSS in tiny files and only load them when you visit a page with the specific component.

This all happens automatically and you can cleanly organize your project by keeping the styles close to the view template and logic (often the same file has the template, <style> and <script> which each get extract automatically into bundles). The .css/.js filenames match the component name which makes debugging easier too in dev console.

HTTP2 handles loading 20-100 small files like nothing and with large SaaS apps usually only have tons of components that rarely get used, so no need to load them all for every page.

If you had to think about this stuff, manage the tooling, and customize then it would be overhead, but otherwise with frameworks it's pure benefit.

> automatically extract

That's such a tool complexity that most people don't need and probably shouldn't bother with for what is often a microoptimization.

How is it a micro-optimization to only load content that you need?

Like I said for large SaaS products you might never see 50% of the components on random settings and other less used pages. Some users only load a few index pages the first few times they visit so making it load faster is always a win.

Frameworks handle way more complex stuff that this. It's not overhead.

The way SPAs work is very performant but are also poor for indexing. Critical Path CSS is mostly used in user facing sites which one wants to get indexed by Google. As page speed is one of the factors considered in SEO, and rightly, one tries to optimize that. Vue, React, etc. are poor at SEO as they do not even try to do solve the problem as they are used to build apps and not sites.
I see you haven't used these libraries in at least a few years, because they all have frameworks that work with SEO by server side rendering content. React has NextJS while Vue has NuxtJS.
I have from its days of bleeding edge. They still can't match the speed unless you have huge caching budgets; in my experience.
What speed differentials are you seeing? Since they are server side I don't see any speed differences at all since they just transmit raw HTML and CSS, same as if you would write it by hand.
No one is using non-SSR SPAs for content that needs to be SEO'd? That doesn't stop them from using much of the same toolset.
>* is what makes your website -feel- slow, even if you managed to trick Google into thinking it loaded quickly.*

I assume this is why a lot of pages do it: SEO over UX. If they cared that much about user experience the ads and tracking would be gone before worrying about CSS in this way.

>The brief flash of your half-styled website is useless to me. Block rendering until the CSS is done.

I have good news for you: https://web.dev/critical-rendering-path-render-blocking-css

CSS is already a render blocker.

Which I think is the correct way to handle a CSS file, but it's not render blocking if you dynamically load it in with JS. That is what Critical path CSS is combatting against, the "FUIC" or flash of unstyled content, which of course is because people are loading in CSS asynchronously. A problem we created for ourselves.

A lot of new approaches atomically separate the CSS and JS into dozens of smaller files that load in as the parts of the website that need them load in. Critial CSS is likely also a response to the issues that this approach creates.

As the article alludes to, they're ideas that makes total sense clinically, but falls short really quickly in practice.

The more CSS you load the slower some rendering becomes. Even when the styles aren't used.

If you use a static page this isn't as relevant. It can become significant for single page apps, especially if you dynamically load CSS.

An SPA has all the tools in their tooling chain necessary to minimize delivered CSS, yet they're often still slower than a static page web application with a 5000 line CSS file. The issue isn't the CSS rendering, it's in the timing of the resources and their delivery. If you load CSS in 12 different files, especially if you're doing it as the user interacts with the site, then the browser has to combine, reprocess and repaint every line of CSS it has been given so far, each time it gets new CSS. That's after waiting for the network request.

I know I'm talking against industry opinion, but I don't believe browsers are the right platform to be loading things on the fly as the user needs them. It's 50-300+ms absolute best case, plus processing time. It's going to feel bad.

This is the reason I love tailwind. There's no bloat, no headache of naming conflicts (in fact they completely did away with naming), no orphan css. CSS for a full blown website can be less than 5kb. It takes away the pain of thinking about CSS architecture, naming things, conflicts etc and you can just focus on styling.
I did not expect to like tailwind, and it still irks my past-self's approach to CSS. But I can't argue that as a wholistic approach to CSS, it is very capable and once you learn the lexicon, very fast to develop with. I really did enjoy it.
Tailwind goes against all old CSS best practices, so I was strongly against it. But I eventually tried it after reading everyone recommending it and it’s great.
It's also one of the very few frameworks that cares about accessibility by default in their examples: no buttons-as-links, no divs-as-lists, clear contrast everywhere.
We have to have Tailwind because we got CSS instead of DSSSL.
Any system using PurgeCSS (like Tailwind) would have the same bloat removed right? (minus the bloat Tailwind adds to your HTML)

Is there a replacement for CSS Modules' "composes" keyword? Tailwind has @apply, but that defeats the purpose of a utility framework somewhat.

I'm a huge fain of Tailwind but if you're using react you can also consider styled-system:

https://styled-system.com

It seems to have a lot of the benefits of Tailwind but better integration with Typescript.

Tell us a bit more about Tailwind. Last I looked at it a few years ago it seemed a conglomeration of antipatterns. Quick googling is not convincing.

Assume that I know _nothing_ about Tailwind, sell it to me. I'd appreciate it. Thanks.

Been developing websites and apps since the days when we are worrying about the y2k bug. Tailwind is my favorite thing. Almost everything that I disliked about CSS is solved by it. Once you really kick the tires, you become way more productive:

Naming is hard. No more naming.

Switching between your markup and your CSS added friction to your flow. No more friction.

Avoiding problems caused by overly aggressive rules / cascade/ whatever is mostly a thing of the past.

I could go on, but if it’s not obvious, I absolutely love it. And the Tailwind UI project is 100% money we’ll spent, too.

I feel the same way about Tailwind, but haven't taken the plunge on Tailwind UI. Though I kinda want to buy it just to support the team. Do you feel like it constrains you too much, or no?
> Though I kinda want to buy it just to support the team.

That's kind but they have earned shedloads already (over $2m USD as of Jan 2021 [1]). That's on top of the $2.3m for the Refactoring UI ebook [2].

1. https://www.smalltechbusiness.com/monetizing-open-source-tai...

2. https://twitter.com/adamwathan/status/1289702466754211842

Ha, fair enough. Though now I'm aware of the book and kinda want to buy it :p
I don’t use anything other than the design ideas. When I’m building a feature, the first thing I do is find a design pattern from Tailwind UI. For that, it’s been well worth the few hundred bucks.
Makes CSS faster to write in the short term, and harder to read in the long term. Also makes complex markup abysmally dense and unwieldy. I understand why people like it but I avoid it when I can. As a Svelte user, my markup is sacred and each component has an automatically scoped style tag so Tailwind is an anti-pattern in any non-trivial project.
> Assume that I know _nothing_ about Tailwind, sell it to me. I'd appreciate it. Thanks.

Tailwind removes (or wilfully ignores) the "cascading" from css. It breaks all good, old css design patterns on purpose - in favor of "widget-level" styling.

It also effectivly builds its own (extendable) style language via named classes, ending up somewhere between "semantic" and "micro-styling" - but allowing for refactoring as needed. It reduces CSS to an implementation detail.

For example, in "Agile Web Development With Rails 7"[1], a number of form fields are initially styled as:

   ... class="block   shadow   rounded-md   border   border-green-400
       outline-none  px-3   py-2   mt-2   w-full " 
Later reactored to:

  ... class="input-field" via tailwind:


     .input-field {  @apply 

         block   shadow   rounded-md  
        border   border-green-400   outline-none 
         px-3   py-2   mt-2   w-full 

    }
On the surface similar to defining a css class - but effectively scoped to your own code.

In a small project, where you're publishing documents - tailwind might not be great - but in a large project, with many widgets it is a good fit.

Similar to how "movable code" (js on the client) might be a better fit than REST for web applications (as opposed to web pages).

[1] https://pragprog.com/titles/rails7/agile-web-development-wit...

The author of Tailwind wrote a blog post explaining his rationale just after the very first version was released: https://adamwathan.me/css-utility-classes-and-separation-of-...

It may be a longer read than you were looking for, in which case I’d say just try it on a small project next time you get the chance. It looks like it wouldn’t work, but it really does.

Aside from was being said, for me, at least when developing component based app, it the better choice IMHO, most css classes contains just one css rule, so you when reading your component class already can visualize how the component looks like. If you are developing a old school web, I would say it could be a pain if you mid project want to change the style of one element (because you are not using components) you need to find and replace every occurrence of the elements classes.

Also not to mention, you skip all the nonsense of the CSS in JS and the need of tools to extract the CSS from JS. Also other big win is that you can use with other projects not based on JS, I'm toying with a WASM app and use tailwind without issues, just run the tailwind bin on my app change (takes a second or less to generate the css file), the only downside here is the need of nodejs (you might be able to download the binary directly and run that, like I do on CI).

I hated the idea of Tailwind when I first saw it. Decided on a whim last year to give it a solid try after hearing others say "you just have to experience it for yourself". I tried it, and now it's literally painful going back to anything else. I can't explain it, it's just magical.

In my opinion, Tailwind is to CSS what jQuery was to Javascript. It's just the right level of abstraction that you need.

But you have to just try it for yourself and experience it. I'd recommend using it with VSCode's intellisense plugin.

Since you use the term "antipattern," what is the respectable pattern (methodology) that Tailwind stands opposed to?
Semantic classes.
How is a class with a single rule, that does exactly what it's supposed to, not "semantic?" What do you think "semantic" means, in this context?

Maybe you mean classes organized with a high level of abstraction that have multiple rules and are meant to be related to the document structure in a specific way?

They might mean a system like BEM, where they are semantic in that they represent concepts tightly coupled to the HTML and what it's used for.

    <div class="product">
        <button class="product__add-to-cart">
        ...etc
BEM is a good example of what Tailwind is a counterpoint to.
> Is it [...] pragmatically worthwhile

YES! It affects e-commerce conversion rates measurably.

Great point by the author: "make sure it’s the right thing to focus on." Hobby site? Don't care about FOUC. E-commerce? It's a game-changer.

> The brief flash of your half-styled website is useless to me

Not to the rest our users though! Are you on a 3G connection in a third-world country? If not your experience may be not be shared but every user.

I am confused, are you for or against FOUC? Or for optimizing against it?

My core industry is e-commerce, have been doing it for over a decade. Our fastest sites are the fully cached, single CSS file sites. You get sent a small amount of CSS and HTML, and you are off to the races. Javascript comes later, and is not necessary to operate the site until checkout.

What kills a sites speed for us is usually shitty CDNs and bloated assets, we don't have those. There isn't a single e-commerce platform popular today that I feel does this well, but that is because they are all enterprise platforms full of enterprise features.

I'm against FOUC when it matters, and for optimizing CSS when it's the right thing to focus on.

On hobby sites it doesn't matter. On high volume e-commerce it does.