by littlestymaar · 2y ago · 0 comments
`fixed flex items-center w-full max-w-xs p-4 space-x-4 text-gray-500 bg-white divide-x rtl:divide-x-reverse divide-gray-200 rounded-lg shadow top-5 right-5 dark:text-gray-400 dark:divide-gray-700 dark:bg-gray-800"` [1]
and then I really don't understand what you've gained compared to using CSS directly…[1]: this class is taken from a flowbite component I've worked with no latter than yesterday. https://flowbite.com/docs/components/toast/#simple-toast
I like the approach of tools like Perseid (https://perseid.dev/docs/ui/styling/classes), focusing only on layout design and providing a small set of built-in CSS classes (alignments, positions, margins, dimensions, …) for that purpose. All the rest (colors, shadows, fonts, animations and such) lying in good old CSS declarations.
Not here to argue that learning CSS properly isn't better, but I legit have no interest in doing so. I can copy-paste that code and be sure it'll look exactly the same on my website. I can't do the same with CSS.
I've met a fair amount of devs (specially younger, <30yo fullstack devs) who have not written raw CSS at all and just use Tailwind. They are employed and get paid decently well.
Tailwind is a massive time saver, and I can see the appeal.
You can think of Tailwind as the "APL of CSS." A single utility class can generate a lot of CSS for you, which is one advantage.
The key benefit of Tailwind is that it co-locates your CSS with your HTML, reducing cascading issues and improving maintainability.
Like any codebase, you can use your tools to add more structure and meaning.
For example, I like using a tw function like this:
export const tw = (...classes: (string | undefined | boolean)[]) => classes.filter(Boolean).join(" ");
Using the tw function, your example classes would look something like this: class={tw(
"fixed top-5 right-5 w-full max-w-xs p-4",
"flex items-center space-x-4 divide-x divide-gray-200 rtl:divide-x-reverse dark:divide-gray-700",
"rounded-lg shadow",
"bg-white text-gray-500 dark:bg-gray-800 dark:text-gray-400"
)}
I like splitting the classes into logical groups. In this case, I’m grouping by: 1) base positioning, 2) flex behavior, 3) effects, and 4) colors.Finally, the filter in the tw function allows for easy conditional classes:
class={tw(kind === "something" && "m-4 p-4")}
I also use Biome, which has a work-in-progress but already quite good class sorter for Tailwind [1]. This keeps the code tidy by applying a logical sort order to each string passed into the tw function.--
Thanks for sharing your thoughts.
To deal with the huge number of class lists like this
`fixed flex items-center w-full max-w-xs p-4 space-x-4 text-gray-500 bg-white divide-x rtl:divide-x-reverse divide-gray-200 rounded-lg shadow top-5 right-5 dark:text-gray-400 dark:divide-gray-700 dark:bg-gray-800"` [1]
We've created Flyon UI, to combine the best of both worlds: the aesthetic appeal of semantic classes and the powerful functionality of JS plugins.
With FlyonUI you can achieve this with semantic classes like:
btn btn-primary
for more details: https://flyonui.com/docs/third-party-plugins/notyf/
> btn btn-primary
That's obviously the better way, but then it's not Tailwind anymore. You're just using class and CSS directly.
- No external stylesheets. No need to think about optimising the serving of CSS assets (e.g. with CDN)
- Co-locality: work on styling right where the element is, you don't have to figure out which stylesheet affects a particular element.
- No naming headache, as there's no need to give class names to elements in order to style a particular (or set of) elements
These days HTML is served compressed, with gzip/brotli, so long class attributes won't bloat the resulting file size by much.
Stylesheets still has its place of course.
The big long lines of classes are often only written once anyway as web development usually makes use of components or partials for repeated elements.
Obviously others can continue to use CSS if they prefer to do it that way.
- fine-grained design tokens (p-63 doesn't exist)
- freedom from having to name things prematurely
- not having to jump back and forth between index.html and styles.css
> not having to jump back and forth between index.html and styles.css
Nobody prevents you to have your style directly in the HTML files (or in the JSX components, AKA CSS-in-js). Even though with split screen and keyboard shortcuts to switch I fail to see how this is an argument …
Why not just use Tailwind in your JSX components?
That just is out of place here: using tailwind means using inline style + having a CSS stylesheet (tailwind's stylesheet) at the same time. It's pure overhead.