Adam Wathan: Hey, everyone. Welcome to designing with Tailwind CSS. I'm Adam Wathan, the creator of Tailwind.
In this course, I'm going to teach you everything you need to know to build beautiful custom designs from scratch. I've split this course in two parts.
First, I'm going to teach you about telling the framework. You're going to learn everything you need to know to get it set up and installed in your project, what the utility first workflow looks like, and how to use Tailwinds various features to do things like responsive design, or extract component classes.
In the second much bigger section of the course, we're going to build a real-world application UI from scratch. I'll teach you when, where, and why to use Tailwind's different utility classes to achieve professional-looking results even if you're not much of a designer.
You'll learn how to design and implement a ton of commonly-used component patterns, like how to build a responsive navbar, styling cards and forms, or even building a fully-responsive sidebar layout.
First things first, though, let's talk about how to set up your very first Tailwind project. The most important thing to understand is that Tailwind is a PostCSS plugin. This might sound intimidating, or like some new piece of technology that you have to learn at first, but if you're already using autoprefixer, you're using PostCSS and you might not even know it.
Depending on your text act of choice, PostCSS support might already be built in to the environment that you used to working in. For example, Laravel, Next JS, Vue CLI. All these tools support using PostCSS directly out of the box.
For the sake of keeping things as neutral as possible though, let's walk through setting up Tailwind in a completely vanilla, plain, empty HTML project so you can see just how simple it really is. Starting with this brand new, completely empty project, let's install Tailwind.
Since Tailwind is a package available on NPM, the first thing we need to do is create a package.json file. I'm going to run npm init-y to create a basic empty package.json file. Next, let's use npm install Tailwind CSS to actually install Tailwind.
We also need something to actually process our CSS through our chain of PostCSS plugins. I'm going to install PostCSS CLI as well, which is an incredibly lightweight way to introduce PostCSS into your project.
Finally, Tailwind doesn't include any vendor prefixes like -webkit or -ms in any of this CSS it generates. Let's also install autoprefixer, which is a PostCSS plugin for automatically adding vendor prefixes to your CSS. Great.
Now that our dependencies are installed, let's configure Tailwind in PostCSS. The first thing I'm going to do is run npx tailwind init. What this is going to do is create an empty tailwind.config.js file in our project root.
We're talking about this file in more detail later in the course. For now, all you really need to know is that this is where you would go if you ever wanted to customize Tailwind in any way for your project.
Next we need to create a PostCSS config files. I'm going to create a new file called postcss.config.js in the root of our project. Inside this file is where we specify what PostCSS plugins we want to use.
All we need to do is export an object here that contains a key called plugins, which is an array of plugins that should be used to process our CSS.
First, I'm going to require Tailwind CSS since of course we want to use Tailwind. Secondly, I'm going to require autoprefixer, so we can automatically add vendor prefixes to our CSS.
Next, let's actually create a CSS file that we can run through these plugins. I'm going to create a new file in a CSS directory called tailwind.css. You can put this file wherever you want. You can name it whatever you want. This will work perfectly fine for our use case.
This is where things start to get a little bit more interesting. The way Tailwind works is by looking through your CSS for these custom markers and replacing them with Tailwind's generated code. I'll show you what I mean.
First, let's add an @tailwind directive here with a parameter of base. When we compile our CSS through PostCSS, Tailwind is going to find this @tailwind base marker and replace it with all of Tailwind's generated base styles.
We can also add Tailwind components and Tailwind utilities. Similarly, these two markers are going to be replaced with all of Tailwind's component classes and all of Tailwind's utility classes, which is what actually makes up the bulk of the framework.
Now that we've created this CSS file, let's write a simple script to actually process this CSS through our list of PostCSS plugins. I'm going to head over to our package.json file. Here where we have this test script, let's replace it with a build script.
The way this is going to work is it's going to use the PostCSS CLI tool that we installed, which we can reference as just PostCSS. Then we just need to provide an input file, which is going to be our css/tailwind.css file.
Then we need to specify that we want to write this to an output file, maybe something like public/build/tailwind.css. If we run npm run build in our terminal, this should generate a new CSS file for us that's been processed through PostCSS.
Let's open up the public folder and take a look. Here you can see we've got public, build, tailwind.css. If we scroll up to the top of this file, you can see it starts with normalized.css.
You might have used normalized on a project before, it's just a set of simple resets that try to make all browsers behave the same way by default. This is what Tailwind uses as it's base global reset styles. The reason these styles appeared in the top of our file here is because Tailwind found @tailwind base in our source file and replaced it with all these global reset styles.
Next, if we look at for our class called container, which appears after this global reset styles, this is Tailwind's lone component class that was injected into our CSS wherever we added Tailwind components.
Finally, after the container class, you can see a bunch of classes like appearance none, BG fixed, BG local, just these really simple single-property utility classes. These are all the classes that Tailwind inserted into our CSS wherever it found the @tailwind utilities directive.
One other thing to note is that you can see in this appearance none class, there's this WebKit appearance property and this mose appearance property. These are the vendor prefixed properties that were added when we ran our CSS through autoprefixer.
Now that we've built our CSS, let's create a very simple HTML file so we can experiment with Tailwind and get a feel for what it's like to work with. I'm going to create a new file under public called index.html. I'm going to scaffold out a basic HTML template here.
Now I'm going to import Tailwind just using a regular old link tag like you would with any CSS file on any project. I'm just going to pull in our CSS from build tailwind.css. It's going to add maybe an H1 tag here. We can say hello world.
To preview this in the browser, I'm going to use this tool I really like called the Live Server, which you can easily install globally using MPM just something like mpminstall-gliveserver.
Live Server is basically a really lightweight browser sync alternative. You can point it out a folder, it'll create a little server, spin it up in the browser, and it'll automatically refresh anytime any of the files that it's watching changes.
We can start up a live server by just running live server public since we want it to run from the public folder. Now you can see here's our Hello World H1 completely unstyled by default. Let's head back to our HTML and add some styles to this element using tailwinds utility classes.
Let's head over to this H1. Maybe we can add a class attribute. Maybe we can make the text bigger using something like text 4XL to make the text four extra-large. Maybe you can make the font bold with font bold. Maybe we can center the text with the text center utility, and maybe color the text using a utility like text blue 500.
If we head back to the browser, you can see we have this big blue hello world headline.
Just a quick recap of what we did here. First, we installed a few dependencies. In our case, we installed autoprefixer PostCSS CLI, and Tailwind CSS. Next, we scaffolded out an nttailwind.config.js file. Then we created a post css.config.js file and specified Tailwind CSS and autoprefixer as the post CSS plugins that we wanted to use.
We created this file in our CSS directory called tailwind.css where we dropped in tailwinds three markers, @tailwind directives to tell Tailwind where to include its generated CSS.
Finally, we wrote a very simple build script using PostCSS CLI to take our tailwind.css input file and compile that into the public directory. Then we created this very basic index study HTML file where we pulled in our CSS using a link tag and styled this heading with Tailwind's utility classes.
Over the next few lessons, we'll be talking about Tailwind's utility first workflow and how you can actually use the framework to implement beautiful custom designs.