I WISH I Knew These Tailwind Tips Earlier

Theo - t3․gg
31 Jan 202309:14

Summary

TLDRThis video offers invaluable tips for efficiently using Tailwind CSS, a popular utility-first CSS framework. The speaker emphasizes the importance of utilizing cheat sheets, keeping things simple with copy-pasting and avoiding unnecessary abstractions, leveraging the Tailwind prettier auto-sorting plugin for better maintainability and debugging, and refraining from overriding Tailwind's default behavior through excessive config modifications. The advice, grounded in personal experience, aims to help developers embrace Tailwind's simplicity, expedite their learning curve, and avoid common pitfalls, ultimately enhancing productivity and ensuring a seamless Tailwind experience.

Takeaways

  • 📈 Use Tailwind CSS cheat sheets to quickly learn the syntax and convert CSS to Tailwind classes efficiently.
  • 🔧 Keep your code simple when using Tailwind. Avoid overcomplicating with unnecessary patterns or abstractions.
  • 📌 Embrace copying and pasting class names for similar elements instead of overly abstracting. Tailwind's simplicity makes it manageable.
  • 📝 Utilize editor and language features, like multi-cursor editing, to efficiently update multiple instances of Tailwind classes.
  • 🛠 Avoid using the @apply directive excessively as it can lead to complications and detracts from Tailwind's benefits.
  • 📍 Order your Tailwind classes thoughtfully. Use the Tailwind Prettier plugin for auto-sorting to improve maintainability and avoid CSS conflicts.
  • 💡 Recognize that the order of CSS classes in the stylesheet matters more than their order in HTML for styling precedence.
  • 📚 Do not fear duplication in Tailwind. Repeating class names can be more straightforward and maintainable than creating unnecessary abstractions.
  • 💾 Feel free to copy and paste Tailwind styles across different projects due to its consistent syntax, enhancing reusability.
  • 🔨 Minimize modifications in the Tailwind config. Focus on adding rather than altering existing settings to maintain simplicity and avoid issues.

Q & A

  • What is the first tip the speaker recommends for learning Tailwind CSS?

    -The first tip is to use cheat sheets, especially if you already know CSS. Cheat sheets can help you quickly find the corresponding Tailwind classes for specific CSS properties, making the transition to Tailwind syntax easier.

  • Why does the speaker recommend keeping things simple when working with Tailwind CSS?

    -The speaker encourages keeping things simple because Tailwind CSS is designed to be simple and straightforward. Instead of enforcing a complex system or specific architecture, Tailwind CSS allows you to copy and paste class names as needed, without worrying too much about reusing styles.

  • What does the speaker say about the 'apply' feature in Tailwind CSS?

    -The speaker recommends not using the 'apply' feature, which allows you to write Tailwind classes inside a CSS file. The creator of Tailwind CSS himself regrets adding this feature, as it causes the most issues and debugging problems.

  • Why is the Tailwind prettier Auto sorting plugin important?

    -The Tailwind prettier Auto sorting plugin is crucial because it ensures that the order of Tailwind classes matches the order in which they are applied in the CSS. This prevents issues related to conflicting styles and makes it easier to identify and debug problems.

  • What is the speaker's advice regarding copy-pasting code when working with Tailwind CSS?

    -The speaker encourages copy-pasting code when working with Tailwind CSS. Since Tailwind classes are consistent across projects, it's easy to copy and paste markup from one project to another without issues. This can be particularly useful when working on multiple codebases or within a company with different teams.

  • What is the final tip the speaker provides regarding the Tailwind config file?

    -The final tip is to avoid making extensive changes to the Tailwind config file. The speaker recommends using the config file to add new styles but not to change the existing behavior or functionality of Tailwind CSS.

  • What does the speaker say about using editor features like multi-cursor editing when working with Tailwind CSS?

    -The speaker recommends using editor features like multi-cursor editing to quickly change duplicated class lists simultaneously. The Tailwind team also endorses this approach, stating that it's often the best solution when you can edit all duplicated class lists at once, without introducing additional abstractions.

  • How does the speaker suggest handling styles for repeated elements like navigation links?

    -The speaker suggests that, for repeated elements like navigation links, you can either create a component with the desired styles and mount it multiple times or simply copy and paste the class names for each instance. The latter approach is often simpler and more maintainable, according to the speaker.

  • What is the potential benefit of using loops with Tailwind CSS, as mentioned by the speaker?

    -The speaker mentions that another option for reusing styles is to use loops. You can wrap your components in a loop, allowing you to apply the same set of classes to each instance without creating a separate component.

  • What is the speaker's overall message regarding the use of Tailwind CSS?

    -The speaker's overall message is to encourage embracing the simplicity and consistency of Tailwind CSS. Rather than trying to force it into more complex patterns or systems, the speaker advises following Tailwind's design principles of simplicity, copy-pasting when needed, and avoiding unnecessary abstractions.

Outlines

00:00

🔑 Key Tailwind Tips: Cheat Sheets, Simplicity, and Class Order

This paragraph provides tips for effectively using Tailwind CSS. It recommends using cheat sheets to quickly find the right classes for CSS properties, keeping things simple by copy-pasting styles instead of over-abstracting, and using the Tailwind Prettier Auto Sorting plugin to maintain consistent class order. The plugin helps identify conflicting classes, improves code review, and ensures CSS order matches the HTML order, preventing debugging nightmares caused by order mismatches.

05:01

⚡ Embrace Simplicity and Code Reuse with Tailwind

This paragraph continues the theme of keeping things simple with Tailwind. It advises against overusing the @apply feature, which can lead to technical issues. Instead, it suggests reusing styles through copy-pasting or using loops, as recommended by the Tailwind team. The paragraph also encourages code reuse by copying and pasting markup between projects, as Tailwind provides a consistent syntax. It cautions against over-customizing the Tailwind config and encourages using it for additive changes rather than modifying core functionality.

Mindmap

Keywords

💡Cheat sheets

Cheat sheets refer to quick reference guides that summarize the most important syntax or properties of a programming language or framework. In the context of the video, cheat sheets are recommended as a helpful tool for learning and quickly referencing the various utility classes in Tailwind CSS, which can be challenging for those transitioning from traditional CSS. The video suggests using cheat sheets to easily look up the corresponding Tailwind classes for specific CSS properties like padding or flex-grow.

💡Copy-paste

The video encourages not being afraid of copying and pasting code snippets or class names when working with Tailwind CSS. Unlike more complex styling solutions that promote creating reusable components or abstractions, Tailwind CSS is designed to be simple and straightforward. Copying and pasting Tailwind classes directly is often the easiest and most maintainable solution, especially for repetitive styles. The consistent and modular nature of Tailwind classes makes it easy to reuse code across projects or teams without issues.

💡Simplicity

Simplicity is a core principle advocated in the video when it comes to using Tailwind CSS. The approach discourages over-engineering or introducing complex abstractions unnecessarily. Instead, it promotes keeping things simple by directly applying Tailwind classes to elements or components. The video suggests that copying and pasting styles or making minor adjustments through editor features is often the best solution, rather than creating reusable components or abstractions prematurely.

💡Class sorting

Class sorting, specifically the Tailwind prettier Auto sorting plugin, is highlighted as one of the most important features for an optimal Tailwind experience. This plugin automatically sorts the Tailwind classes in a consistent order, making it easier to identify and debug conflicting styles, facilitate code reviews, and ensure that the order of class application matches the order in the generated CSS. The video emphasizes the significance of this feature in preventing nightmarish debugging scenarios caused by order-related issues.

💡apply directive

The apply directive is a feature in Tailwind CSS that allows developers to apply Tailwind classes within traditional CSS files. However, the video strongly advises against using this feature, as it negates one of the primary benefits of Tailwind CSS, which is its utility-first approach. Using apply effectively replaces Tailwind's modular classes with a traditional CSS abstraction, increasing the chances of technical issues and maintenance challenges in the future.

💡Component abstraction

Component abstraction refers to the practice of creating reusable components or abstractions to encapsulate styles and functionality. While this approach is common in many styling solutions, the video suggests that with Tailwind CSS, it is often unnecessary to abstract styles into separate components prematurely. Instead, directly applying Tailwind classes to elements or copying and pasting styles between components is encouraged, as it aligns with Tailwind's simple and modular design.

💡Tailwind config

The Tailwind config refers to the configuration file in a Tailwind CSS project, which allows developers to customize various aspects of the framework, such as adding new utility classes or modifying existing ones. The video advises against making extensive changes to the Tailwind config, as it can lead to deviating from the intended simplicity and modularity of Tailwind CSS. Instead, it recommends using the config sparingly, primarily for adding new utilities rather than modifying existing ones.

💡Utility classes

Utility classes are the building blocks of Tailwind CSS, representing low-level styles like padding, margins, colors, and typography. These classes are highly modular and composable, allowing developers to build complex user interfaces by combining multiple utility classes together. The video emphasizes the importance of understanding and utilizing utility classes effectively, as they are the core of the Tailwind CSS approach.

💡Code reuse

Code reuse is a significant advantage of using Tailwind CSS, as highlighted in the video. Because Tailwind utilizes a consistent and modular set of utility classes across projects, it becomes easy to copy and paste markup or styles from one codebase to another without issues. This promotes code reuse and sharing, particularly in larger organizations with multiple codebases or teams working on different projects.

💡CSS order

The order in which CSS rules are applied is a crucial concept discussed in the video. Unlike traditional CSS, where the order of styles in the HTML markup determines their precedence, in Tailwind CSS, the order of styles in the generated CSS file is what matters. This can lead to challenging debugging scenarios if the order of classes differs between development and production environments. The video emphasizes the importance of the Tailwind prettier Auto sorting plugin in mitigating these order-related issues.

Highlights

Cheat sheets make it easier to learn the Tailwind syntax and find the right classes for CSS properties quickly.

Don't be scared of copy-pasting and keeping your elements simple. Tailwind encourages a straightforward approach without enforcing complex patterns.

The Tailwind team recommends using editor features like multi-cursor editing to change duplicated class lists simultaneously, rather than introducing additional abstractions.

Avoid using the @apply feature in Tailwind, as it can introduce technical issues and goes against Tailwind's principles.

Use the Tailwind prettier auto-sorting plugin, as it makes it easier to identify conflicting classes, improves code review, and prevents order-related bugs.

The order of CSS classes in the HTML does not dictate the order they are applied, which can lead to debugging nightmares if the order differs between HTML and CSS.

Don't be afraid to copy and paste Tailwind classes between projects, as the consistent syntax allows for easy reuse without issues.

Use the Tailwind config to add new things, but avoid changing existing behavior, as it can lead to issues and goes against Tailwind's principles.

Transcripts

play00:00

if you've seen my thumbnails you might

play00:01

think I hate Tailwind if you've seen my

play00:03

videos you probably know I love it I

play00:05

hope these tips help you as much as

play00:06

they've helped me regardless of your

play00:08

familiarity with Tailwind the first one

play00:10

and this is one I really wish I knew

play00:12

about earlier because it helped me learn

play00:13

Taylor much faster is cheat sheets if

play00:15

you already know CSS the Tailwind syntax

play00:18

can be scary to learn and going from

play00:20

something you already know from CSS to

play00:22

the right Tailwind class isn't always

play00:24

easy cheat sheets make it a lot easier

play00:26

the docs are great but the docs are more

play00:28

focused on the what swears and whys not

play00:31

the witches and the witch is the class

play00:34

for padding is something you can find

play00:35

really quickly by looking here which is

play00:38

the class for Flex grow something you

play00:39

can find really quick here and having

play00:41

this one page you can just scroll up and

play00:43

down and find specific CSS properties on

play00:45

if you just Google Tailwind cheat sheet

play00:47

you should find some good options here

play00:49

this is the one I personally choose to

play00:51

use thing two it's keeping it simple

play00:52

this isn't like a trick I can show so

play00:55

much as encouraging you to not be scared

play00:58

of copy paste and generally making your

play01:01

elements simpler a lot of people come

play01:03

into Tailwind expecting a more complex

play01:05

system like styled components that

play01:07

prescribes a specific way of

play01:09

architecting your components or

play01:11

something like CSS modules with a

play01:13

specific abstraction pattern Tailwind

play01:15

doesn't have a pattern around how you

play01:17

should use it it is by Design very

play01:19

simple and generally the pattern for

play01:22

using Tailwind right is also keeping

play01:24

your things simple if you have a nav bar

play01:26

and that navbar has four links in it and

play01:29

you want each of those to be styled the

play01:30

same you can make a component that has

play01:33

these Styles applied and mount that

play01:35

component four times or you can copy

play01:37

paste the class name four times it's not

play01:40

that big a deal it really isn't and if

play01:42

you change your mind and you want to

play01:43

change that underline property to a

play01:46

bigger underline or you want to change

play01:48

the color from Blue 400 to Blue 500 yes

play01:50

it's annoying to change it in more than

play01:52

one place we can also select it once

play01:54

press command d a few times in vs code

play01:56

have all of them selected and change all

play01:58

of them at once I'm not the only one who

play02:00

recommends this the Tailwind team does

play02:01

too if you go to their docs they have a

play02:03

page called reusing Styles in this whole

play02:06

page is for the most part showing you

play02:08

tricks on how to not worry about reusing

play02:11

stuff as much the first thing it says

play02:13

here is use editor and language features

play02:16

like multi-kirks or editing and it shows

play02:18

you how to change things in multiple

play02:21

places at once when the text is the same

play02:23

so if you want to change this from font

play02:24

bold to font medium it's very easy to do

play02:27

you don't need to make a component and

play02:29

as they say here you'd be surprised at

play02:31

how often this ends up being the best

play02:33

solution if you can quickly edit all of

play02:36

the duplicated class lists

play02:37

simultaneously there is no benefit to

play02:39

introducing additional abstractions

play02:42

totally agree and I think people stray

play02:44

away from this a little too often

play02:46

because they're scared of repeating

play02:48

themselves it's not a big deal it's

play02:50

often the easiest and most maintainable

play02:51

solution don't reach away from this if

play02:53

you don't need to

play02:54

the next option they have here is

play02:56

putting it in a loop you can have all of

play02:58

your contributors and you can wrap them

play03:00

with a loop so that each one gets listed

play03:02

individually and then you have all the

play03:04

classes in one place so you have to make

play03:06

a component you just inline the for Loop

play03:07

that has the stuff that you need here

play03:09

you can also start abstracting

play03:10

components almost all the stuff we're

play03:12

talking about view react whatever you

play03:15

can break something into a component and

play03:16

then reuse it more trivially tip number

play03:18

three is somewhat related to this but

play03:20

it's very important and it's a feature

play03:22

in Tailwind I'm going to tell you not to

play03:24

use that feature is ADD apply add apply

play03:27

lets you apply a Tailwind class in a

play03:29

traditional CSS class so in a CSS file

play03:32

you can have Tailwind properties applied

play03:34

to a different class this is kind of

play03:36

useful for applying a Tailwind color to

play03:38

your body background on your application

play03:40

but if you're using this to write

play03:42

Tailwind inside of CSS and then use

play03:44

those CSS classes inside of your app

play03:46

you've now taken one of the biggest

play03:47

benefits of Tailwind thrown it away and

play03:49

replaced it with a bad abstraction that

play03:51

has a high chance of causing technical

play03:54

issues in the future Adam himself the

play03:56

creator of Tailwind has said that he

play03:58

lightly regrets adding apply and it's

play04:00

the feature that causes them the most

play04:01

issues and they spend the most time

play04:03

debugging by far he's estimated the cost

play04:06

of the out apply feature for the

play04:07

Tailwind business in the hundreds of

play04:09

thousands of dollars on the topic of

play04:11

where you put your Tailwind classes the

play04:13

order you put them in is important too

play04:15

if you take anything from this video as

play04:17

a Tailwind user

play04:18

I really hope it's this if you're not

play04:19

already using it

play04:21

the Tailwind prettier Auto sorting is

play04:24

one of the most important things to have

play04:27

as part of your Tailwind experience I

play04:29

would go as far as to say you're not

play04:30

really using Tailwind if you're not

play04:32

using this because of how big of an

play04:34

impact this plugin has had on my

play04:36

experience writing maintaining and

play04:38

shipping Tailwind

play04:39

this does three important things first

play04:42

off it makes it much easier to see if

play04:44

you have conflicting classes because

play04:45

they'll always be nearby each other

play04:47

which makes it much easier to identify

play04:49

debug and fix things when they're going

play04:51

wrong second it makes code review way

play04:54

way easier I now know what classes will

play04:56

be where so when I'm skimming through

play04:58

code I can quickly see where the padding

play05:00

properties are where the flex properties

play05:02

are where the display properties are

play05:03

just by being used to the order that

play05:06

makes me so much faster in code review

play05:08

the same way prettier itself does the

play05:10

consistent formatting makes it easier

play05:12

for my brain to process what changes are

play05:14

happening when I'm looking at code that

play05:15

has changed but the most important bit

play05:17

is that the way CSS classes are applied

play05:19

is not based on the order of their class

play05:22

names it's based on the order of the

play05:25

style sheet so here we have two

play05:27

different elements in the Dom we have a

play05:31

b and b a so this div has classes A and

play05:35

B applied and this diff has classes b

play05:37

and a applied now we're going to do

play05:38

something fun

play05:39

we're going to make

play05:41

the CSS for a

play05:43

in the CSS for a we'll set background

play05:46

color

play05:48

to Blue

play05:52

and now with B

play05:55

we're going to change it when I

play05:56

uncomment this what do you think is

play05:58

going to happen are both going to be

play05:59

blue are both going to be pink or is top

play06:01

going to be pink and bottom is going to

play06:02

be blue

play06:04

both are pink

play06:06

the reason is in the CSS B comes after

play06:09

it no longer matters what order it's in

play06:12

in the HTML

play06:14

your HTML order does not dictate the

play06:16

order the CSS applies therefore very

play06:19

importantly if it is possible for the

play06:22

order here to be different than the

play06:24

order here it will be very hard to debug

play06:26

when something happens it is so

play06:29

obnoxious to deal with problems that

play06:31

result in this or if this CSS in a

play06:34

different file than this one and there's

play06:35

a load in the browser at different times

play06:37

the amount of issues in the amount of

play06:39

years of my life I have lost to

play06:41

debugging issues related to the order of

play06:44

class names differing in production or

play06:46

Dev or any of the other things in the

play06:48

CSS specifically is insane and the most

play06:51

underrated benefit of the automatic

play06:53

class sorting is the order that this

play06:55

sorts your classes is the same as the

play06:58

order that they'll appear in your CSS

play07:00

I I cannot put into words how valuable

play07:04

this is

play07:06

if you haven't experienced these bugs

play07:08

yourself but trust me you do not want to

play07:11

debug something related to this just use

play07:14

their sort order it will keep you from

play07:16

having miserable nightmares in the

play07:18

future

play07:19

just do it it is better you will feel

play07:21

faster using it you'll feel faster

play07:22

reviewing it and you're getting yourself

play07:24

out of potential hell when you do that

play07:27

one last tip and this kind of touches on

play07:30

all the others

play07:32

don't be scared of copy paste a lot of

play07:35

developers are used to npm installing or

play07:38

abstracting their features making

play07:39

everything a reusable component or a

play07:41

piece or installing someone else's

play07:43

pieces

play07:44

the goal of Tailwind is to make writing

play07:46

styles very fast simple and reliable

play07:50

if you have a style that works because

play07:52

you found it in Tailwind UI you found it

play07:54

on someone else's site or you found it

play07:55

in your own code base you don't feel

play07:57

like you have to install or abstract it

play07:59

feel free to copy paste because the

play08:01

Tailwind classes are so consistent and

play08:03

work in every Tailwind project it is

play08:05

very easy to take markup from a

play08:07

different project and drop it in the one

play08:09

you're in right now and have no issues

play08:10

this is obviously useful if you're a

play08:12

developer that's touching multiple

play08:14

things or even just a developer that has

play08:16

access to other code bases that have

play08:18

code you might want to use it is even

play08:20

more valuable if you're at a company

play08:22

that has multiple different code bases

play08:24

and you want to be able to context shift

play08:26

between different teams and reuse code

play08:28

between them as well without even having

play08:30

a mono repo much less a component

play08:33

Library that's shared when you have

play08:35

Tailwind as the the syntax that defines

play08:37

how things look in your applications

play08:39

that syntax is a contract that you can

play08:43

copy paste between places and it's still

play08:45

honored in the same ways unless you go

play08:47

crazy with the tail and config which

play08:48

final final tip don't go crazy in the

play08:51

Tailwind config use it to add things

play08:53

don't use it to change things I know I

play08:55

went pretty ham with Helen when I

play08:56

started trying to make it do all the

play08:58

stuff it wasn't built to do trying to

play09:00

make Tailwind work like styled

play09:02

components or like other things

play09:04

the goal of this video isn't to convince

play09:07

you of tail end it's to show you what

play09:09

helped me get good fast and what has

play09:12

made me love Tailwind so much as a long

play09:13

time user

Rate This

5.0 / 5 (0 votes)

Related Tags
Tailwind CSSWeb DevelopmentProductivity TipsCSS UtilityFront-EndCoding EfficiencyDeveloper WorkflowOptimization TechniquesBest PracticesLearning Resources