Tailwind CSS: How to Optimize Performance with Shruti Balasa

Tejas Kumar
10 Sept 202411:03

Summary

TLDRThe discussion delves into the evolution of Tailwind CSS, addressing criticisms about its large CSS file size due to its atomic approach. It explains how Tailwind's utility-first methodology generates only the CSS needed for a project, optimizing file size. The conversation highlights the shift from version 2's post-CSS PurgeCSS plugin to version 3's Just-In-Time (JIT) compiler, which reads source code to generate only the necessary CSS classes, enhancing performance. The JIT compiler also enables dynamic class generation, such as custom color utilities, without increasing the stylesheet size. The talk emphasizes the importance of proper class naming for the JIT compiler to function effectively, ensuring developers don't inadvertently deoptimize their code.

Takeaways

  • πŸ”§ Tailwind CSS has evolved to improve upon older practices like using the PurgeCSS plugin, which is no longer necessary for many projects.
  • 🎯 One common critique of Tailwind is the large size of its CSS file due to the one-class-per-rule approach, but this is mitigated by the fact that it only ships the CSS that's actually used in the project.
  • πŸš€ Tailwind's utility-first approach can result in a more efficient delivery of styles compared to traditional CSS, where the same rule might be repeated multiple times across different classes.
  • πŸ“ˆ The Just-In-Time (JIT) compiler introduced in Tailwind CSS version 3 optimizes performance by generating only the CSS that's used in the source code, eliminating the need for PurgeCSS.
  • 🌐 The JIT compiler allows for a more extensive range of utility classes without significantly increasing the size of the CSS bundle, which was a limitation in earlier versions of Tailwind.
  • πŸ’‘ The JIT compiler's method of reading the source code before generating CSS is a significant innovation that streamlines the development process and enhances performance.
  • πŸ“ Developers should ensure that class names are written as complete strings in their source code for Tailwind to correctly identify and generate the necessary styles.
  • πŸ› οΈ Conditional class names or those generated dynamically should be concatenated into full class names to be recognized by Tailwind's JIT compiler.
  • 🌟 Tailwind's evolution showcases the benefits of open-source development, where community feedback and innovation lead to significant performance optimizations and usability improvements.
  • πŸ“¦ The shift from tree shaking to JIT compilation in Tailwind CSS represents a paradigm shift in how CSS is managed and optimized in web development.

Q & A

  • What was the purpose of the PurgeCSS plugin in the earlier versions of Tailwind CSS?

    -PurgeCSS was used to remove unused CSS from the final build in earlier versions of Tailwind CSS. It would scan the project's source code and remove any classes that weren't used, optimizing the production build by reducing its size.

  • What is the criticism against Tailwind CSS regarding class names and file size?

    -The criticism is that Tailwind CSS generates a large CSS file because it creates one class name per rule, which could lead to megabytes of CSS being shipped to users. However, this is a misconception because Tailwind CSS only ships the styles that are actually used in the project.

  • How does Tailwind CSS handle the generation of CSS for a large number of elements using the same style rule?

    -Tailwind CSS generates the style rule once in the stylesheet, and it's applied to all elements that use that class. This is more efficient than traditional CSS where the same rule might be repeated for each element in the stylesheet.

  • What is the significance of the 'Just-In-Time' (JIT) compiler introduced in Tailwind CSS version 3?

    -The JIT compiler in Tailwind CSS version 3 generates only the CSS that is used in the source code, eliminating the need for PurgeCSS. This means the development and production stylesheets are the same, and it optimizes performance by not generating unused styles.

  • How does the JIT compiler in Tailwind CSS improve the efficiency of CSS delivery?

    -The JIT compiler reads the source code first and generates only the class names that are used, which is an additive process. This approach reduces the amount of code shipped compared to traditional CSS where the same rule might be shipped multiple times.

  • What is the impact of Tailwind CSS's JIT compiler on the ability to use custom colors?

    -With the JIT compiler, developers can use a wider range of utility classes, including custom colors, without having to add them manually to the configuration. This is because the compiler generates the necessary styles on the fly, reducing the initial bundle size.

  • Can you explain the concept of 'tree shaking' in the context of Tailwind CSS and how JIT compiler changes this?

    -Tree shaking is a technique to remove unused code. In Tailwind CSS version 2, this was done by generating a large CSS file first and then removing unused classes. With the JIT compiler in version 3, the process is reversed; it reads the source code first and generates only the necessary CSS, making tree shaking unnecessary.

  • What is the benefit of shipping less code with Tailwind CSS compared to traditional CSS?

    -Shipping less code with Tailwind CSS can lead to faster load times and reduced bandwidth usage for users. It also means that the browser has less CSS to parse, which can improve rendering performance.

  • How can developers potentially deoptimize their code when using Tailwind CSS with JIT?

    -Developers might deoptimize their code by splitting class names across different lines or using variables that don't form complete class names. Tailwind CSS requires full class names to be discoverable for JIT compilation.

  • What advice can be given to developers to optimize their use of Tailwind CSS with the JIT compiler?

    -To optimize the use of Tailwind CSS with the JIT compiler, developers should ensure that class names are written as complete strings without being broken up. This allows the compiler to discover and generate the necessary styles efficiently.

Outlines

00:00

🌐 Tailwind CSS Evolution and Performance

The paragraph discusses the evolution of Tailwind CSS, highlighting the improvements over time. It mentions the use of a plugin called PurgeCSS to optimize CSS in older versions. The speaker counters criticisms about the size of Tailwind's CSS file by explaining how utility classes work and how they can actually reduce the amount of CSS shipped compared to traditional methods. They provide an example of how using 'display: flex' in 100 elements would be more efficient with Tailwind CSS, as the rule is included only once in the stylesheet. The paragraph also touches on the Just-In-Time (JIT) compiler introduced in Tailwind CSS version 3, which generates only the necessary styles, thus optimizing both development and production environments.

05:01

πŸš€ JIT Compiler and Tailwind CSS Innovations

This section delves into the Just-In-Time (JIT) compiler's impact on Tailwind CSS, contrasting it with the previous approach of generating a large CSS file and then purging unused styles. The JIT compiler reads the source code first and generates only the necessary styles, making the development and production stylesheets identical. The paragraph also discusses the ability to use more utility classes without bloating the CSS file, thanks to JIT. It mentions the convenience of using direct color values in class names, enabled by JIT, and reflects on the overall innovation and optimization in Tailwind CSS's approach to CSS generation.

10:03

βš™οΈ Best Practices for Tailwind CSS Usage

The final paragraph emphasizes the importance of proper class name usage in Tailwind CSS to ensure optimal performance with the JIT compiler. It warns against breaking class names across multiple lines or using variables that concatenate class names, as this can prevent Tailwind from recognizing and generating the necessary styles. The paragraph reinforces the need for full class names to be present in the source code for JIT to function correctly, ensuring that developers are aware of how to avoid potential pitfalls and maintain the efficiency of their CSS.

Mindmap

Keywords

πŸ’‘PurgeCSS

PurgeCSS is a tool used in the context of the video to remove unused CSS from a project. It's part of a process to optimize the final build of a website by eliminating unnecessary styles that are not actually applied to any elements. This is crucial for performance optimization, as it reduces the size of the CSS file that needs to be loaded by the browser. In the video, it's mentioned as a plugin used in the past with Tailwind CSS to ensure that only the CSS classes actually utilized in the project are included in the final stylesheet.

πŸ’‘Tailwind CSS

Tailwind CSS is a utility-first CSS framework that allows developers to build custom designs without writing CSS rules from scratch. It provides a set of classes that can be applied to HTML elements to style them directly. The video discusses how Tailwind generates one class name per rule, which can lead to criticism about its file size, but also how it can be optimized with techniques like PurgeCSS and Just-In-Time (JIT) compilation.

πŸ’‘Atomic CSS

Atomic CSS refers to a methodology where CSS is broken down into small, reusable units or 'atoms'. Each class in Atomic CSS represents a single styling property, which can be combined in various ways to create complex designs. The video discusses how Tailwind CSS operates on this principle, generating one class for each styling rule, which can lead to a more efficient use of CSS in large projects.

πŸ’‘Just-In-Time (JIT) Compiler

The Just-In-Time (JIT) compiler in Tailwind CSS is a feature introduced in version 3 that generates styles only for the classes used in the source code. This is a significant optimization over the previous approach where a large set of possible styles was generated and then purged down to the used ones. The JIT compiler ensures that the development and production CSS are the same, reducing the need for PurgeCSS and improving performance.

πŸ’‘Tree Shaking

Tree shaking is a term borrowed from JavaScript optimization, referring to the process of removing unused code. In the context of the video, it's mentioned in relation to how Tailwind CSS's JIT compiler works, by 'reading' the source code to determine which styles are needed and only generating those, thus 'shaking the tree' of unused styles.

πŸ’‘Performance Optimization

Performance optimization in web development involves improving the speed and efficiency of a website. The video discusses how Tailwind CSS, through its utility-first approach and JIT compilation, can contribute to performance optimization by reducing the amount of CSS that needs to be shipped to the user, thus decreasing load times and improving user experience.

πŸ’‘Class Names

In the context of the video, class names refer to the specific strings used in HTML to apply styles defined in CSS. Tailwind CSS uses a naming convention for its utility classes, such as 'bg-green-500' for a green background. The video emphasizes the importance of using full class names consistently, as the JIT compiler relies on these to generate the necessary styles.

πŸ’‘CSS File Size

The size of a CSS file is a concern for web performance, as larger files take longer to download and parse. The video discusses criticisms of Tailwind CSS for potentially generating large CSS files due to its utility-first approach. However, it also explains how techniques like JIT compilation can mitigate this by ensuring only used styles are included.

πŸ’‘Development vs Production

The video contrasts the development and production environments in web development. Development is where code is written and tested, often with more comprehensive tools and features. Production refers to the final, optimized code that is deployed to users. Tailwind CSS's JIT compiler ensures that the development environment can be as close as possible to production, with minimal differences in the CSS.

πŸ’‘Utility Classes

Utility classes in CSS are predefined classes that apply specific styles. Tailwind CSS is built around the concept of utility classes, offering a wide range of classes for quick and efficient styling. The video discusses how Tailwind's approach to utility classes can lead to more efficient CSS usage compared to traditional CSS development.

Highlights

The necessity of using a post CSS plugin called Purge CSS in the past to optimize Tailwind CSS.

Criticism of Tailwind CSS for generating one class name per rule, leading to a large CSS file.

Explanation that in large projects, using Tailwind CSS can actually reduce the stylesheet size compared to traditional CSS.

Tailwind CSS's utility classes only generate the styles that are used, optimizing the amount of code shipped.

The introduction of the Just-In-Time (JIT) compiler in Tailwind CSS version three, which improves performance.

How JIT compiler works by reading the source code first and generating only the necessary styles.

The ability to use more utility classes without impacting the development speed due to JIT compiler.

The impact of JIT on allowing dynamic class names like text-[#hexcode] in Tailwind CSS.

The shift from tree shaking to reading source code before generating styles in JIT compiler.

The importance of having complete class names for Tailwind CSS to discover and generate styles.

The potential for developers to deoptimize their code by breaking class names in conditional statements.

Advice for developers to ensure class names are not broken by spaces or different lines for JIT compiler to function properly.

The historical context of Tailwind CSS's evolution from version two to three and the improvements in performance and usability.

The significance of the JIT compiler in making Tailwind CSS more accessible and efficient for developers.

The practical benefits of JIT for developers, such as not needing to purge unused styles for production.

The innovative approach of JIT in optimizing CSS generation based on actual usage in the source code.

The potential for JIT to change the way developers think about and implement CSS in their projects.

Transcripts

play00:00

there was also a plugin that you had to

play00:01

use back with back in the day and this

play00:03

is obviously thank God has has been

play00:05

fixed or improved upon um you would have

play00:08

to use a post CSS plugin called Purge

play00:11

CSS um to do something and also a big

play00:15

criticism that I've heard from Tailwind

play00:17

haters which I think they're all wrong

play00:20

um but you know whatever let's be

play00:22

objective about it is um is that

play00:25

Tailwind generates one class name per

play00:28

rule right so like Flex is only display

play00:31

Flex there's nothing more in that

play00:33

declaration um grid is display grid and

play00:35

so on and so forth so it's like one

play00:36

class so some people um who maybe aren't

play00:39

as familiar with the concept of atomic

play00:41

CSS or Tailwind or utility classes it

play00:44

they they criticize Tailwind saying

play00:46

that's a massive CSS file it's meab how

play00:49

how can you ship megabytes of CSS to

play00:51

your users um what's an appropriate

play00:53

response to that uh the response to that

play00:56

is the larger projects that you're using

play01:00

right now you're literally using Flex on

play01:03

all your elements okay so let's say you

play01:05

have a 100 elements where you're using

play01:08

Flex which is which is not huge 100 is

play01:11

because these days you know we use flex

play01:12

for everything back in those days I

play01:14

don't know how we worked without it but

play01:16

now that it's available you know then

play01:18

for vertical elements we use flex call

play01:20

you know for icons for everything so

play01:22

let's say for 100 elements you're using

play01:24

the class Flex okay and that rule is in

play01:29

stylesheet it is generated only once

play01:33

okay but let's say you have 100 elements

play01:35

in your stylesheet you have 100 elements

play01:38

and there's that display Flex rule 100

play01:41

times so how do you how do you say that

play01:44

uh you know that is okay and this is not

play01:47

So eventually if you see if you look at

play01:49

the entire project and um you look at

play01:52

the stylesheet it kind of evens out and

play01:57

uh most huge projects also have uh CSS

play02:01

files which are less than 10

play02:03

KB that's because um you know of this

play02:07

this is the the way I explained I don't

play02:09

know how to explain it better than that

play02:11

but uh it's not how you think it will

play02:13

happen like even if it's one rule you're

play02:16

using that rule in multiple places so

play02:18

you're eventually reducing the Styles

play02:21

sheet itself not increasing it I love

play02:24

this because it makes the case um for

play02:26

tailin CSS to be also like outside of

play02:29

just convenience we already talked about

play02:31

for example context switching and naming

play02:34

U and the base reset Styles outside of

play02:36

these developer conveniences there's

play02:37

also enduser conveniences because it um

play02:40

in theory or actually not in theory in

play02:42

practice ships less code than

play02:45

traditional CSS because of what you

play02:47

mentioned because typically in an

play02:48

application if you have 100 different

play02:50

classes that have a rule display Flex

play02:52

then you ship that line display Flex 100

play02:55

times more than you need to whereas with

play02:57

tailor and CSS and utility classes that

play02:59

rule display Flex is shipped once that's

play03:01

a what is that's like a um 100x

play03:05

Improvement um in how much code you ship

play03:08

not exactly you're still writing Flex in

play03:11

HTML but still yeah you

play03:14

know right um Fair yeah you're right so

play03:17

it's four bytes for every how many ever

play03:20

um display Flex yeah good thank you for

play03:21

correcting that um there's also this

play03:25

thing that Tailwind used to do where it

play03:27

would as as part of the post CSS process

play03:30

remove like it would read your source

play03:31

code and look at the class names you're

play03:33

using and then from that big final

play03:35

generated CSS file would just like

play03:36

remove classes you're not using as a as

play03:38

a build optimization um is that still

play03:41

the case how how does that impact

play03:43

performance I mean obviously positively

play03:45

probably right because it removes a

play03:47

bunch of unused stuff so that was back

play03:50

in version 2. something Tailwind CSS had

play03:55

these uh I don't know like you know tens

play03:58

of thousands of uh class names fixed

play04:01

class names like every single class name

play04:03

that you see in the you know the

play04:06

documentation all of this was there so

play04:09

you would in your development you would

play04:11

take that entire thing and just use the

play04:14

ones that you need and you had to

play04:17

actually purge them for production so

play04:20

your production stylesheet would be

play04:21

different from your development one

play04:24

right and that was back then then came

play04:27

the just in time compiler so what just

play04:30

in time does is it doesn't ship I mean

play04:33

it doesn't give you that entire

play04:34

stylesheet when you

play04:36

develop the only Styles it generates is

play04:39

are the one that it that are there in

play04:41

your source code from the beginning in

play04:43

development itself so your development

play04:45

stylesheet is same as the one as

play04:47

production and then this Justus in time

play04:49

uh compiler became the default from

play04:51

version three onwards so people who are

play04:54

using Tailwind version three onwards

play04:56

don't even know what the spurge thing is

play04:58

so if they come this yeah they don't

play05:01

need to if they come across this and

play05:02

block post or videos they might really

play05:04

wonder oh do I have to do this uh that

play05:07

means you know because they see the

play05:08

discussion right and the discussion is

play05:10

like you know T you know if you don't

play05:12

Purge you get like this huge style sheet

play05:15

and they probably are still under that

play05:17

impression but no tailin generates only

play05:20

those styles that are there from the

play05:23

beginning this is so cool this is I

play05:26

think there's a big um engineering

play05:29

optimization performance idea here that

play05:32

a lot of developers can extract and take

play05:34

away this is a really I and I want

play05:36

everybody to sort of listen to this

play05:37

right and you can tell me Shy if this is

play05:39

um appropriate or accurate but what I'm

play05:41

hearing is so in version two Tailwind

play05:44

did this thing where they um generate a

play05:46

bunch of CSS step one like a big CSS

play05:49

file um and then read the source code

play05:52

the classes you you use and then

play05:55

basically remove the classes that are

play05:57

not used from the spig Cs that was that

play05:58

was version It's called and then with

play06:00

the just in time compiler in version

play06:02

three right Tre exactly and in in

play06:05

version three what they do with this jit

play06:07

or justtin time compiler is instead of

play06:09

that flow they start with not generating

play06:12

a big bundle but they start with reading

play06:13

your code because they're going to read

play06:14

your code anyway and in version two they

play06:16

were doing it after but now if you do it

play06:18

first you can look at all the class

play06:20

names and then only generate the class

play06:21

names that are used so it's not removing

play06:23

anything it's actually an additive

play06:24

process is that accurate yes that is

play06:27

cool um and

play06:30

if I may add something here that gave

play06:32

them the so in in version two what uh if

play06:36

you needed these extra colors like uh

play06:39

the default was gray and then there

play06:42

there there was red and there was orange

play06:44

and all of those but now you can use all

play06:46

the colors like slate and stone Amber uh

play06:50

fuchsia all of these by default you

play06:53

don't have to add them in your config

play06:55

earlier you had to add them in your

play06:57

config all these to use all these new

play06:59

colors because I mean imagine shipping

play07:02

that huge a bundle right I mean they

play07:04

can't you can't download I mean you

play07:06

can't put that entire thing in

play07:08

development it'll slow down your

play07:09

development right so now because of this

play07:12

jit thing they are able to give us a lot

play07:16

more utility classes uh without the

play07:19

developer having to even know that you

play07:21

know they have to add them or something

play07:23

like

play07:23

this that's so cool there should be a

play07:26

class like Amber but Amur and it's just

play07:28

the color of biryani oh my gosh that

play07:30

would be so cool

play07:32

um I I think another consequence of this

play07:35

J JW JW jit that that we're talking

play07:38

about is um the ability to do something

play07:41

like text Dash square brackets hex code

play07:44

right and that's because it's just in

play07:46

time it can literally just generate that

play07:47

on the

play07:48

Fly is so

play07:51

cool amazing I it's just the Innovation

play07:55

blows my mind um to go from tree shaking

play07:58

to look we're going to read the source

play08:00

code anyway so instead of reading the

play08:01

source code after generating you just

play08:03

read the source code before I man that's

play08:06

just it it blows my mind how cool that

play08:08

was um and again I hope this is open and

play08:11

that discussion would be fun to even

play08:12

follow after the fact like we've had it

play08:14

for I think years now but to see how

play08:17

it's done right and I think that's the

play08:18

beauty of um open source so cool um so

play08:23

in code you can still like so for

play08:26

example JavaScript is is super super

play08:28

optimized like the engine are um they're

play08:31

optimized to the point where it's very

play08:33

difficult often times to like deopt

play08:36

yourself and JavaScript meaning to do

play08:39

something that's going to make the

play08:40

engine do more work than necessary and

play08:42

be slower um but people still do it also

play08:46

in react for example there's they try to

play08:48

make it easy to make performance

play08:50

applications but sometimes you in an

play08:53

effort to optimize your code you end up

play08:55

de optimizing your code meaning you'll

play08:57

cause like unnecessary memory allocation

play09:00

um in the case of imports sometimes you

play09:02

can import modules in a way that doesn't

play09:04

allow tree shaking so you like deopt

play09:06

yourself you deoptimized um in Tailwind

play09:09

with the jit with all of this um are

play09:11

there still ways that developers should

play09:13

know about that they may be de

play09:15

optimizing their code and if yes what

play09:17

can they do to to help

play09:19

that um maybe not de optimizing but one

play09:23

thing they need to know is their class

play09:25

names will not be discovered if they are

play09:28

broken

play09:29

uh for example let's say you add a BG

play09:32

somewhere in your let's say you have a

play09:34

react component or something and then

play09:36

you're saying that you know class name

play09:38

is BG hyphen and then if uh this is

play09:42

Success then green 500 or you know else

play09:47

let's say red 500 something like that so

play09:49

those classes are broken there in the

play09:51

code in the source code right Tailwind

play09:53

will not be able to discover them they

play09:56

have to be completely BG green 00 BG red

play10:00

500 because Tailwind hases that pattern

play10:02

matching so for Tailwind green 500 is

play10:05

not a class name BG green 500 is so if

play10:09

you're going to uh have your class names

play10:12

like conditional classes or something or

play10:14

wherever else have you can totally have

play10:17

them in your JS files but they have to

play10:19

be full they have to be the entire class

play10:22

name don't split them anywhere uh

play10:25

probably that's the only thing they need

play10:26

to know there's no way you can uh you

play10:30

know maybe under like whatever de

play10:32

optimize I I don't think there's a way

play10:35

Yeah Yeah by by Broken you mean broken

play10:38

by space so they have to be like one

play10:40

consistent string right yeah not just

play10:43

space like in different lines like you

play10:45

know you you put a variable like class

play10:46

equals BG and then you append green 500

play10:51

and you append something so in the you

play10:54

know yeah they scans the file and it

play10:58

can't find the entire class name so then

play11:01

it would be available yeah

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Tailwind CSSJIT CompilerWeb DevelopmentFrontend OptimizationCSS FrameworksPerformance TipsAtomic CSSUtility ClassesCode EfficiencyDevelopment Practices