Additive styling with Crayons
Tags: freshworks, crayons, css, tailwindcss
Published: 2022-05-20 05:27:59

In this post, we are going to take a look at the CSS utilities feature provided by the [Crayons Component Library](https://crayons.freshworks.com) from [Freshworks](https://freshworks.com). The CSS utilities is a bunch of CSS rules, especially class definitions, that are simplified for the developers to consume at ease with an intuitive and easy to remember, naming conventions. The most common example we can relate to, would be [Tailwind CSS](https://tailwindcss.com). 

Before diving into the main topic, let's have a brief introduction about the various technologies and tools surrounding the CSS utility class ecosystem.

## Utility Classes
In the front-end toolkit, CSS is a vital part, but it's increasingly becoming hard to manage, especially in large projects. The style definitions you write in your CSS files are usually written in a global scope, which is narrowed through complex selectors. And since this is an inherently problematic approach, we are faced with a lot of issues like specificity, redundancy, bloat, and maintenance can become a nightmare. 

And then came Atomic CSS.

> Atomic CSS is the approach to CSS architecture that favors small, single-purpose classes with names based on visual function 
-- John Polacek on [CSS Tricks](https://css-tricks.com)


The term Atomic CSS itself applies to the architectural philosophy, not any one particular variation like static or programmatic approach.  

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4993edm33fenpsynximj.png)

Utilities are simple HTML classes typically scoped to a single CSS property, like border-style or background-color. They can be used additively to style an HTML element from scratch or to override a style defined in component CSS. They are the static approach of an architectural philosophy referred by Atomic CSS.

They allow designers and developers to build and test new designs and components without abstracting their work into traditional semantic names or altering production CSS.

They also make it possible to create element-specific overrides without writing high-specificity variants into component CSS.


## Crayons
Freshworks Crayons is a library of UI components that are the building blocks to help create an intuitive and uniform user interface for all your apps. Crayons helps developers build apps that adhere to the UX standards set by the Freshworks Design System. The utility classes provided by Crayons will help you to style your HTML elements inside your [Marketplace](https://freshworks.com/apps) apps for Freshworks products.


## Installation
The CSS Utils library is available as a standalone CSS file, which you can include in your projects by simply using a link tag through a CDN like [unpkg.com](https://unpkg.com). No need for installing any npm packages and build or compilation required to use it.


```html
<link
  rel="stylesheet"
  href="https://unpkg.com/@freshworks/crayons@v3/css/crayons-min.css"
/>
```

## Usage
The below is a simple example of the utility classes provided by Crayons.

```html
<article class="fw-mx-16">
    <div class="fw-card-1 fw-p-20 fw-flex fw-flex-column" style="width: 200px">
      <section>
      <div class="fw-flex fw-flex-row">
        <p class="fw-flex-grow fw-type-h7 fw-mb-0">Booking ID</p>
        <span class="fw-type-xs"><fw-icon name="arrow-right"></fw-icon></span>
      </div>
      <a href="#">
        <span class="fw-type-h6 fw-color-azure-800">#1237483</span>
      </a>
      </section>

      <section class="fw-mt-20">
        <p class="fw-type-h7 fw-mb-0">Hotel</p>
        <a href="#">
          <span class="fw-type-h6 fw-mt-4 fw-color-azure-800">Leela Palace</span>
        </a>
      </section>

      <section class="fw-mt-20">
        <p class="fw-type-h7 fw-mb-0">Customer Name</p>
        <a href="#">
          <span class="fw-type-h6 fw-mt-4 fw-color-azure-800">David</span>
        </a>
      </section>
    </div>
  </article>


```

You can see the example in action in Codepen:

{% codepen https://codepen.io/rajasegar/pen/xxYdPzR %}

## Conventions
The Crayons CSS utils follows a standard convention for the class names. All the class names are prefixed with `fw-` which is a shorthand notation for Freshworks. Apart from this it follows a simple pattern for the class names like p for padding, m for margin and so on. It is something similar to other css utility libraries like [Tailwind](https://tailwindcss.com), [Tachyons](https://tachyons.io) and the others.


## Comparison with Tailwind
This is a short comparison between the Crayons CSS utils class names and Tailwind.

| Crayons            | Tailwind       | CSS Property                   |
|--------------------|----------------|--------------------------------|
| fw-p-16            | p-4            | padding: 1rem;                 |
| fw-type-xl         | text-2xl       | font-size: 1.5rem;             |
| fw-br-4            | rounded        | border-radius: 0.25rem;        |
| fw-flex            | flex           | display: flex;                 |
| fw-color-smoke-700 | text-slate-700 | color: #335155; /* Tailwind */ |


## Closing thoughts
The Team behind Crayons have put in great effort for the v3 release of Crayons by adding more components, bringing in CSS utility classes, standardizing documentation and many more improvements overall for the library. Please do try out the new v3 components and let the team [know](https://github.com/freshworks/crayons/issues) for any issues or feedback.

The Crayons is an open-source library, so anyone can contribute to the library. The source code is located in [Github](https://github.com/freshworks/crayons). The team has started doing Community Hours sessions from last month so that the developers and the outside world can get in touch with team to clarify any queries and provide feedback. You can find the session recording of the last Community Hours [here](https://community.developers.freshworks.com/t/community-hours-28th-april-2022-crayons-v3-release/5524).

{% youtube https://www.youtube.com/watch?v=Tvtm9eR-J9Y %}



## References
- [Utility First](https://tailwindcss.com/docs/utility-first)
- [Let’s Define Exactly What Atomic CSS is ](https://css-tricks.com/lets-define-exactly-atomic-css/)
- [Crayons](https://github.com/freshworks/crayons)
- [Freshworks Developer Community](https://community.developers.freshworks.com)