Reimagine Atomic CSS
antfu.me
antfu.me
If you're going to be doing things like
.m-0 {
margin: 0;
}
Then you may as well just do <div style="margin: 0"></div> .color-primary {
color: #f00;
}
Could easily become .color-primary {
color: #00f;
}
But beyond this case, I don't understand it either.It feels like the gravity has moved a long way from CSS Zen Garden and that the web isn't about semantics and documents anymore since the practical usage differs.
Any designers that can shed light on this tailwind trend?
What people often miss is that this is to be used for creating design systems. `m-0` doesn't necessarily mean `margin is zero`, it means the smallest margin available. You should also call it `m-xs` for example. Or if it does mean margin is 0 then it might be better suited to be called `m-none`.
These utility classes play very nicely with component-based design. Just like JSX puts your markup in the same file as your code, this does the same for styling/layout.
I wouldn't say it's necessarily "better for all" but it's absolutely a very viable way to work no matter how much some may cry heresy. It saves you from having to figure out what you're going to call everything upfront and let's you break things into components as you go. I haven't personally use it on a big project but I've found it a very clean way to work and have heard good things from folks using it on big projects.
If i need a little more spacing on alk my medium margins, i would end up with something like .m-8 { margin: 10px; } if i start editing these compiled styles
Bingo. The "documents" era of the web has given way to the "applications" era.
You don't have to sacrifice semantics in order to opt in to Tailwind's cutesy approach at expressivity. class="thumbnail m-0 md:m-4" works wherever class="m-0 md:m-4" does and isn't user-hostile.
> `class="m-0 md:m-4"` is more legible because it describes exactly what is happening
If by "what is happening" you mean "how it's styled", but that's extremely presumptuous. You might as well argue that there's no difference between Flash and an equivalently styled web page since if you took a screenshot of each they'd be visually identical—as if the whole thing begins and ends with getting the "rectangle full of little lights" to change in a particular way <https://xkcd.com/722/>. It's very narrowminded.
I'm not saying you have to be full on schema.org-conformant, but include something that actually makes sense instead of being as useless as the spew that CSS compilers put out.
If you’re using a component based framework (e.g., Vue), you create a named component.
If you’re not using a component-based framework, you create named classes with applied atomic styles.
With either approach, your styles are contextualized with a name and can be reused.
2. The code you write shouldn't be compromised by the tool you use to write it.
In your example, `.m-0` and `style="margin: 0"` aren't equivalent in this paradigm (even though they happen too be technically equivalent). `.m-0` really just means "no margin". If you look further down the article, you'll see:
.m-1 { margin: 0.25 rem; }
.m-2 { margin: 0.5 rem; }
/* ... */
.m-10 { margin: 2.5 rem; }
As you can see, the numbers don't match their values, the class names are meant to be relative to one other.A better way is to use constraint-based design and come up with a system where you would only have `.m-none`, `.m-xs`, `.m-sm`, `m-md`, `.m-lg`, `.m-xl` (for example).
Libraries like Tailwind give you WAY more than you're going to need and you should only really be using a subset.
.m-none.br-kinda-rounded.p-a-bit.bw-pretty-thicc.bc-old-mauve
The clever example in the article with using a loop to define values is not something I find particularly compelling either. The whole point with specifying relative values, e.g., percentages, ems, root ems, etc., is that they can change relative to an element they inherit from or from the root of the document.Furthermore, using class names is not the only way to achieve the effect in the example you pulled from the article. Sass (and just about any CSS superset from the past decade) can achieve the same with mixins, which would spare the resulting document from all the selector noise.
The amount of class names is off-putting at first, but you get used to it pretty quickly. (I'd also recommend using numbers or sm, md, lg etc for your size increments, but to each their own.)
Tailwind and friends aren't trying to solve the CSS Zen Garden problem. Their focus is on re-usable CSS rather than re-usable HTML. Depending on your needs that may be better or worse.
As stated in another thread, it’s not necessarily better but it works very well, and I personally find it nicer having everything my component needs in one file.
This seems preferential over a single string with various syntax. I could be wrong in that my linked approach is not the same.
I could see myself doing something like
.card {
@include margin(2);
@include borderRadius(10)
// etc
}
and so on, then using class="card" to tie it together, but adding a tree-shaking step, or a pre-crawling (!!) step just strikes me as entirely too hefty a cost for what amounts to parametric variants of a given style tokenSuch a great example of omission and inclusion when reporting the facts.
In either case, splitting atomic CSS is probably more complex to get right because of ordering, and possibly more inefficient for everyone as well.
n << 100
There are other solutions besides n=1 that satisfy this inequality. Serving 2 CSS files per page does not introduce substantial latency over 1, even on HTTP 1.1 user agents. Once you get to 4 you start to cause some problems, but 2 is barely noticeable.CI/CD can fight your CDN by reissuing every asset on your website on every single publish, causing all of your visitors to soak up bandwidth at the same time. If you name your JS and CSS bundles based on the hash of the contents instead of prefixed with a version number, then many publishes will reuse the same assets, except that doesn't scale so the bigger the website the more evictions you'll see. But a site bundle with a per-page or per-subject matter bundle does scale, because targeted fixes often won't affect your home page.
The Tailwind config file "downsides" were the most interesting. 5 minutes every time to find the config variable to change? Maybe 5 minutes the first time and then it's the exact same process to for every other attribute. Tailwind even renamed all of the options to mirror the JS counterparts.
Again, I think this is a cool idea, but not everything needs to be a pissing contest. Just state the features your thing provides and let users decide if it's worth switching.
Side note: some performance numbers on using attribute selectors vs css classes would be interesting. I'd assume it's negligible until tens-of-thousands of selectors, but I'm curious nonetheless!
In my work flows I typically keep my components/layouts small and my CSS scoped. So I write the semantic HTML (or JSX), add a few classes in there and then style the classes (often in the browser rather than in my editor).
People often complain about the web no longer being semantic, and I'd argue that CSS should also be semantic. The classes should fit the components. A button component should have a button class. A payment form should have a payment form class.
I always felt like the most useful thing tailwind actually brings is the preset based style system, as a dev u don't need to remember if the styleguide calls for 6 or 8 pixel space, it's just "m" to me and i can apply styles without worrying too much about design details.
If this approach compiles the atomic classes depending on arbitrary numbers, have i gained anything aside using the "class" instead of the "style" property? (aside specificity of course)
I was taught to use classes semantically and style from there.
A navbar would be a .navbar class for example and that would be styled in the stylesheet.
Istead of a bunch of classes in the html to define the styles for the navbar.
I'm old school though and this may be the better more modern way?
He discusses why he changed from this thought process & mentions the old CSS Zen Garden many of us grew up with as the "ideal" way to do CSS.
Of course your mileage may vary depending on the type of work/projects you do.
I've had to fight my zen garden conditioning the whole time but I have to admit this looks good.
Thanks for the link!
He is also one of the main vite contributors