Jump to content

Tailwind CSS for ProcessWire Developers


Sergio

Recommended Posts

50 minutes ago, Krlos said:

Hello, I'm following your video, but apparently the code in the gist is not the same as the video?

In the gist the naming of the main css file is different (kickstart.pcss rather than main.css, the .pcss extension is optional) because at the time of the recording I had already made my previous guide, in which I've used that naming convention, so I prefer not to change it.
Feel free to name your files however you want, just remember to reflect those changes inside the "scripts" object inside package.json.

Link to comment
Share on other sites

  • 7 months later...
10 hours ago, pwired said:

I have been following tailwind css for a while. Can't help it but having so many classes in a tag really clutters up your html. What has become wrong with vanilla css ?

You can also combine corresponding classes, for this purpose there is the "@apply" function.
This makes it possible to keep the code relatively clean 😉 

Example vom the Tailwind Docs:
 

.btn {
  @apply px-4 py-2 rounded font-semibold bg-gray-200 text-black;
}

 

Link to comment
Share on other sites

12 hours ago, pwired said:

I have been following tailwind css for a while. Can't help it but having so many classes in a tag really clutters up your html. What has become wrong with vanilla css ?

Nothing wrong with vanilla css at all indeed 🙂
For me tailwind is just a tool that makes me quicker to write my css declarations and development in general.

Take the above example:

.btn {
  @apply px-4 py-2 rounded font-semibold bg-gray-200 text-black;
}

In plain css it would be:

.btn {
  padding: var(--margin-tb) var(--margin-lr);
  border-radius: var(--medium-radius);
  font-weight: 600; // and this might be a custom property too if needed
  background: var(--bg-lighter-gray);
  color: var(--text-black);
}

It's more verbose I think.
I've intentionally wrote css variables to slightly emulate what tailwind does: helping you to create and use a design system, with consistent values at disposal throughout the entire application.

Not to mention purgecss, which is included out of the box, and for me it's a no brainer. It drastically reduce the size of the css to be used in production.

That being said I always find myself in the situation where I have to write my custom css classes, where defaults values I set before are enough no more and need more control.
There is this new option where you can set arbitrary values directly on the html markup (https://tailwindcss.com/docs/adding-custom-styles#using-arbitrary-values) but I'm not really sold and I still prefer to organize them by myself. Having another tool in the box might be convenient from time to time.

  • Like 1
Link to comment
Share on other sites


1)
What about keeping the main layout styling in vanilla css and put only utility classes for final layout tweaking ?
That would give you the best of 2 worlds !

2)
Having said that, how many percent of all the available tailwind utility classes are really going to be used ?
Certainly not all of them in every website you make, let's say 40% - 50% - 60%  ?

3)
That brings me to the idea to sort out only the most useful ideas and classes from Bulma, Spectre and Bootstrap
and bring them in your own utility class framework. That would make things certainly more effective.
How much time would that take ? Sticking to only essential and useful classes, not that long.

 

 

Link to comment
Share on other sites

19 minutes ago, pwired said:

What about keeping the main layout styling in vanilla css and put only utility classes for final layout tweaking ?
That would give you the best of 2 worlds !

I've found myself that the most useful classes are the ones dedicated to wireframe the layout itself, like margins and paddings. Using them in early stages of the blocking phase seem to be the most effective way for me. So I tend to do the opposite of what you are suggesting: utility classes first and layout tweaking with custom rules later (if needed).

19 minutes ago, pwired said:

Having said that, how many percent of all the available tailwind utility classes are really going to be used ?
Certainly not all of them in every website you make, let's say 40% - 50% - 60%  ?

There is plenty of utility classes I never had to use, and that's ok. This is also true for vanilla css declarations as well, it all depends on the specific project. To me it's not about how many utility classes I would use, but how many of them I can reuse inside a single project, avoding inconsistencies all over the place. Having the ability to tweak them in one single configuration file is a bonus. Of course this is also true if you are smart enough to go with "pure" css custom variables inside the :root selector of your css.

19 minutes ago, pwired said:

That brings me to the idea to sort out only the most useful ideas and classes from Bulma, Spectre and Bootstrap
and bring them in your own utility class framework. That would make things certainly more effective.
How much time would that take ? Sticking to only essential and useful classes, not that long.

This is certainly an option, if you have found something inside Bootstrap, Bulma or whatever is missing from Tailwind.
I don't see the point anyway, because with the new jit mode from Tailwind you will have just the utility classes baked inside your "dist" css, without any bloating useless css.

  • Like 1
Link to comment
Share on other sites

On 12/12/2021 at 10:41 PM, pwired said:

I have been following tailwind css for a while. Can't help it but having so many classes in a tag really clutters up your html. What has become wrong with vanilla css ?

I've been using Tailwind for years, yet there are still both those days that I really enjoy using it, and others when I wonder if it really makes sense, so I get what you're saying. Also: even though we use it for just about all of our projects nowadays, we often combine it with "custom" CSS classes (mostly BEM style), simply because Tailwind isn't always the correct tool for the job 🤷‍♂️

Others have probably said it already, but for me/us the benefits of Tailwind boil down to...

  1. super fast development, particularly when mocking things up / designing in the browser,
  2. common set of tools and guidelines, which is very nice when working in a team (having to dig into an older project that doesn't use Tailwind is a bit of a pain), and
  3. superb efficiency — by which I mean that using Tailwind results in very small CSS files, in most cases much smaller than I would get by writing my own vanilla CSS.

There are a couple of things I'd view as downsides: a) markup becomes entangled with styles, and b) sometimes it feels like I'm just learning alternative ways to do the stuff I would already know how to do with vanilla CSS. First one is something that, in my opinion, can't be completely avoided, but using components for shared sets of classes helps a lot. Second issue becomes less of an issue day by day.

Other than that, I'm quite happy with Tailwind, and it suits my workflow very nicely. It's not the perfect fit for everyone (or for every project), and that's fine too 🙂

  • "What about keeping the main layout styling in vanilla css and put only utility classes for final layout tweaking?"
    • Well, we use a combination of custom CSS and Tailwind already, but the literal "layout" part is one that's actually so efficient to do with Tailwind that I wouldn't bother doing it in vanilla CSS anymore. In my experience changes to the layout are often easier to implement with Tailwind than with vanilla CSS, and the resulting markup is actually quite clean.
  • "Having said that, how many percent of all the available tailwind utility classes are really going to be used?"
    • It doesn't really matter how much of Tailwind I / we use. Thanks to the tree shaking logic the "compiled" result only includes what is necessary. There's zero overhead in having a large toolbox but not using each tool for every project.
  • "That brings me to the idea to sort out only the most useful ideas and classes from Bulma, Spectre and Bootstrap and bring them in your own utility class framework. That would make things certainly more effective."
    • Exactly how would this make things more effective? We're in the business of building custom sites and/or applications, so pre-built components offered by a front-end framework won't help much (if at all), at which point — from my point of view — all Bootstrap and likes can offer is a very limited set of utility-first tools. Sure, we use prebuilt tools elsewhere (Vuetify for PWA apps etc.) but for custom-built sites Tailwind is (IMHO) a better fit.
  • Like 7
Link to comment
Share on other sites

2 hours ago, Ivan Gretsky said:

Is tailwind suitable for theming somehow? Same markup, different styles? I would guess it is not. But maybe I am missing something?

I tried to understand your question but I'm missing something for sure. Can you elaborate a bit deeper please? 🙂
As a quick guess, is this page something you were looking for?

Link to comment
Share on other sites

51 minutes ago, Ivan Gretsky said:

Like you have your primary color as blue and the client wants to have it green.

That can be easily done changing colour variables inside tailwind.config.js as suggested on my previous post. In addition to that it might be useful, for theming purpose, to create a single preset for every theme you want to have, and switch between them per project-basis. More here https://tailwindcss.com/docs/presets

51 minutes ago, Ivan Gretsky said:

Or you want to  override color just for a part of the site.

Not something possible out of the box as far as I am aware of. Utility variables are top level and not tied directly to custom sections.

  • Like 1
Link to comment
Share on other sites

1 hour ago, 3fingers said:

Not something possible out of the box as far as I am aware of. Utility variables are top level and not tied directly to custom sections.

Maybe css vars could help.

Thanks for the insights. I finally understood that colors can be set as primary instead of blue in the config) But still all examples I've seen do use named colors. I guess it is a trade-off of using utility-first approach.

Link to comment
Share on other sites

10 hours ago, Ivan Gretsky said:

I finally understood that colors can be set as primary instead of blue in the config) But still all examples I've seen do use named colors. I guess it is a trade-off of using utility-first approach.

Not sure how common this sort of practice is, but we always define custom colours for each project — usually with generic names such as brand-1, brand-2, etc. (unless the client has named colours or a lot of colours, in which case more specific naming helps). In fact in most cases I only use two colours from the default palette: black and white. Everything else (including shades of gray) are either custom colours, or just black/white with specific opacity applied 🙂

Same thing goes with paddings: while we use the default spacing scale, there are always a few "project specific" values; half, single, double, triple, etc. Usually these are pretty straightforward (single = 1rem, etc.) but sticking to predefined, named values does make it a tad easier to make changes later.

11 hours ago, Ivan Gretsky said:

Or you want to  override color just for a part of the site.

I don't think there's a built-in way for this. Depending on the specific use case I'd probably pick one of two options:

  • If it's just a few colours etc. that need to change, add those to the shared theme config file for the site and handle the "colour theme" in code.
  • If there are a lot of changes and/or for some other reason you want to clearly separate styles for different parts of the site, create a separate Tailwind config file and adjust your build process accordingly. How that would work depends on what kind of build process is in use, but generally speaking it shouldn't be too difficult 🙂

If you google something like "tailwind multiple themes", you'll probably find other solutions as well. I've never needed this type of functionality, so haven't given it much thought.

  • Like 2
Link to comment
Share on other sites

  • 5 months later...

More than a year ago I said I would try TailwindCSS... now it finally happened and... and the result is disastrous!

 

Well... disastrous for my opinion back then and my thoughts about it until a few weeks back.

I am quite surprised and happy with the result, to be honest.

I did a rebuild of an ongoing side-project, with a full rebuild of the whole site structure all the way through to any breakpoints (CSS only, HTML was kept except additional classes). Yet some parts are still outside of TailwindCSS as those are really custom and are way better placed in an additional CSS file. Even though you can customize TailwindCSS to almost all of your needs but I didn't want to bother with it for now.

The change in file size is quite dramatic... or even super awesome.
However you want to see it.

I went from...

  • 59kb (the whole project)

down to

  • 17kb (structure - based on TailwindCSS CLI generated .css file) PLUS
  • 5kb custom CSS for colors, custom properties PLUS
  • 10kb custom CSS for icons, backgrounds, and fonts PLUS
  • unknown size of additional kb within HTML due to those TailwindCSS classes

tailwind-vanilla-compare-file-size.png.d8b74e7341fe2375540d2125f0cc9239.png

Ignore those other files as those are helpers or part of other instances within that setup.

Which is in total:

  • 32kb for the full project, all pages, all icons, all fonts.

Yet... the whole setup isn't ready for production right now as my setup only really works with 11ty as a middle-man for now and not within my PW workflows... but that's another story. There are probably even more things I could optimise in terms of fonts, icons and custom classes for colors.

But for now... WOW... I always was against those utility frameworks and didn't like them at all.
I am and always was a VanillaCSS Purist... didn't ever really like Bootstrap, UIKIT or anything like that, but TailwindCSS could and might have changed my mind. 

From now on... I might have to think and work different for some or another reason.
Right now my test was built on a project I really knew in and out with all its details.
Let's see how it works out in a real and new project.

Results based on: TailwindCSS 3.1.2 (latest - as of now, 2022-06-14. all file sizes unzipped, without plugins like Typography)

  • Like 3
Link to comment
Share on other sites

×
×
  • Create New...