custom property choices
Hey, i've been seeing many ways to go about custom properties and i'm wondering what exactly the most beneficial layout method is. i'd really appreciate an explanation anyone may have with this, thank you in advance!
for example some people tie it in with utility classes like so-
and others don't utilize utility classes and primarily use custom properties, for example- (i know this one is used so it's easier to switch out properties depending on a section's design)
or could it be beneficial to use both perhaps?
8 Replies
As with almost everything:
It Depends™️
Custom properties follow the cascade, so you want them to be high enough that they're able to be used when and where needed. But your examples seem to be less "how to use custom properties" and more "should I use utility classes".
Ah gotcha, oh haha I guess you’re right, in which case in a scenario like I gave should I be using utility classes?
Using utility classes is a personal preference. That's what Tailwind is: nothing but utility classes! And people seem to love it. But for me, I prefer to do more scoped CSS: set some sane default stylings on most of the elements using the element selector, then use classes and/or Ids to "scope" certain stylistic choices. As an example, I'd label a certain section with an Id and use that to "scope" styles to that particular section.
I like to use utility classes for generic things like controlling visibility of an element, emphasizing text, etc. It also helps to have a few utility classes for common things like colors, which I then use to toggle things around using JavaScript, as opposed to add the styles directly.
The most common utility classes I use are for margin: mx-2, my-2, mt-2, mb-2, etc. I don't like to use margin directly in my CSS, and instead control the positioning of the elements externally.
As with most things, somewhere in the middle is a good place to be. Mostly "scoped" styles with the occasional utility class. Use the right tool for the right job, I always say!
Is scoped styling my second example? And when you say you give it ids, do you mean something like
button—primary
for example?
Ohh I see, thank you for sharing thatAn example of scoped styling for a testimonials section. It'll have images on the left with the user's quote on the right:
Ah gotcha, thank you for the example!