DEV Community

loading...
Cover image for Rapid Prototyping with Utility-First CSS

Rapid Prototyping with Utility-First CSS

hrishikesh1990 profile image hrishikesh1990 Originally published at blog.flexiple.com ・3 min read

I am the co-founder of Flexiple and Remote Tools. In this post, I describe why I chose to use a utility-first CSS framework to build my website’s UI in a fast, robust and low-maintenance way.

Here's a short demo & twitter thread (if you prefer a quick read) -

Why Utility-First CSS

1. Rapid prototyping without worrying upfront about abstraction

I figured that utility classes are extremely handy when doing rapid prototyping, especially in the initial stages of app development.

My first priority is always to rapidly build custom UI elements on the page without having to worry about building CSS components from the word go. The latter approach often leads to premature abstraction i.e. building a component for a certain use-case and ending up never using it.

2. Extracting components when I need to

Once I am done adding these utility classes to multiple elements across pages and if I see a common set of utilities used, I can always extract them like this primary button on my website –

.btn-primary {
    @apply font-bold py-2 px-4 border rounded border-azure-radiance-500 hover:border-silver-500 bg-azure-radiance-500 hover:bg-silver-500 transition duration-300 ease-in-out transform hover:-translate-y-1 hover:scale-105;
}
Enter fullscreen mode Exit fullscreen mode

3. Customization to the core

A couple of days into building the website, I realize that the shades of gray across the pages are a little too dark.

I have an entirely new palette for gray and all I have to do is override the gray default in Tailwind.

theme: {
 extend: {
  colors: {
   'gray': {
     100: '#F9F9F9',
     200: '#F0F0F0',
     300: '#E7E7E7',
     400: '#D6D6D6',
     500: '#C4C4C4',
     600: '#B0B0B0',
     700: '#767676',
     800: '#585858',
     900: '#3B3B3B',
    }
   }
 }
}
Enter fullscreen mode Exit fullscreen mode

It’s not just colors. I can practically override or extend any default in Tailwind, be it margins, padding, font, you name it.

4. Easily responsive

Tailwind provides four breakpoints (sm, md, lg, xl) and I like the fact that the breakpoints are mobile-first by default. So, unprefixed utility classes take effect on all screens while prefixed ones apply only at specified breakpoints and above.

For example, this code is for pages with a right sidebar –

<div class="w-full sm:w-1/4 sm:order-2">
  <div class="sm:mt-16 mt-4">
    <%= render "shared/right_sidebar" %>
  </div>
</div>
<div class="w-full sm:w-3/4 sm:order-1">
..
</div>
Enter fullscreen mode Exit fullscreen mode

So, each of the divs occupy full width on all screens except 640px and above (sm) where the sidebar occupies 25% and the main content takes up the rest.

Alt TextAlt Text

5. No need to name classes except for extracted components

One thing that always irritates me whenever I look back at the code for Flexiple is the naming of classes. Names like ‘dont-take-our-word_text-content‘ and ‘u-margin-bottom-big‘ should be deemed inventions.

Tailwind’s naming convention is intuitive. More than that, I don’t have to scratch my head to come up with tons of new names.

How’s this different from inline styles?

This is something that bothered me at first. Using inline styles is a taboo and I was worried if I am ending up doing the same here.

But there’s a stark difference between the two. Inline styles allow you to do pretty much anything you want. On the other hand, Tailwind provides you a design system with constraints and all you are doing is choosing styles from that constrained design system.

Atomic classes, no matter how atomic they are, will always remain a finite list.

Thanks for reading! If you liked it, connect with me on Twitter - I write there regularly about tech, products & startups.

Discussion (7)

pic
Editor guide
Collapse
xyn profile image
Mydrax • Edited

I kinda disagree with the "inline styles are taboo" statement. Inline styles can be useful for situations where you just want to change a couple style properties. If you are already using Tailwind it makes sense to call it taboo, because there's probably a class that does that already.

PS: You could use codeblocks for when you write code. Just gotta cover the code in three backticks.

Collapse
hrishikesh1990 profile image
hrishikesh1990 Author • Edited

I use inline styles quite often too and you're right, they are handy when you want to have something changed super quick. But they pose a big challenge when I come back months later & want to re-style the element(s).

I actually can't think of any +ve for inline styles except that they are hacks you put in when there's extreme time crunch.

Thanks for the tip - just changed it to code blocks :)

Collapse
michaelandreuzza profile image
michael-andreuzza

Been building sites with Bulma. Tested Tailwind, ain't going back...

Collapse
hrishikesh1990 profile image
hrishikesh1990 Author

💪

Also, love colorsandfonts.com - used it a lot while building :)

Collapse
michaelandreuzza profile image
michael-andreuzza

Thanks man how nice that was.

Really happy ypu pike it, I am soon going to redesign it.

Collapse
jjokah profile image
John Johnson

Also a fan of Utility-First css (tailwind). Love the post.

I'm understanding css better as I use tailwind unlike bootstrap where most things seems to be magic.

Collapse
hrishikesh1990 profile image
hrishikesh1990 Author

Haha, well said about Bootstrap. Glad you liked the post :)