Learn how to performatically use tailwind css tokens.
h-[15px]
or p-[3px]
. Opt for Tailwind tokens
such as h-8
and p-2
instead. Also, steer clear of specifying colors in your
tokens (e.g., bg-[#fd429a]
). Instead, use theme colors for better
maintainability.
.tsx
files in your codebase. Subsequently, it extracts CSS
tokens, merging them into a single styles.css
file. This file is then served
to the browser, styling your components. This process repeats whenever code
changes occur.
However, the consequence is a larger-than-necessary styles.css
file containing
styles for all components, impacting performance metrics. Consider a scenario
with a home page (Home.tsx
) and a product page (Product.tsx
):
styles.css
p-4
is not used in the home page, it’s included in styles.css
,
increasing CSS payload and affecting Web Vitals metrics (FCP).
In the previous example, both flex
and justify-center
tokens are re-used. In
big projects, this kind of class re-utilization tends to grow. Re-usability is
key for having a performatic tailwind project. However inline tokens may break
this trend. Examples of inline tokens include p-[3px]
, h-[4rem]
, and
bg-[#fdb43a]
. These classes are unlikely to be reused loading to an increased
styles.css
size and hindering Core Web Vitals.
To avoid inline syntax when replicating styles, follow a simple rule of thumb:
use nearby tokens. For instance:
p-[3px]
-> p-2
h-[4rem]
-> p-8
bg-[#fdb43a]
-> bg-primary