How Tailwind is set up
All the styling in this project comes from Tailwind. It isn’t the main point of this tutorial, so we won’t go deep. This page explains what Tailwind is, how we’ve set it up, and then gives you a few things to play with.
What is Tailwind?
Tailwind CSS is a CSS framework made of small utility classes. Each class does one small job. For example:
font-boldmakes text bold.text-gray-400makes text light grey.rounded-lggives an element rounded corners.p-4adds some padding.
Instead of writing your own CSS in a separate file, you style an element by adding classes directly in your HTML:
<p class="font-bold text-gray-400">Hello</p>Tailwind finds the classes you used, and creates the CSS for them.
How we’ve set it up
We use Tailwind classes all over our templates, for example in home.html and partials/todo_item.html. Tailwind itself is loaded and configured in one place: todo/templates/todo/_base.html. Every page is built on top of _base.html, so the classes work everywhere.
Here is how it is set up:
1. Load Tailwind from a CDN
todo/templates/todo/_base.html
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
This loads Tailwind’s browser build. It runs in your browser, looks at the classes on the page, and generates the CSS on the fly.
2. Define our own colour
todo/templates/todo/_base.html
<style type="text/tailwindcss">
@theme {
--color-primary: teal;
}
...
</style>
type="text/tailwindcss"tells the browser build to read this block as Tailwind, not plain CSS.@themeadds our own values to Tailwind. Because this one is called--color-primary, we get classes liketext-primary,bg-primaryandborder-primaryfor free.
Now we can use our primary color wherever we need to, and if we want to change it we can change it in one place.
3. Give plain HTML elements a default style
todo/templates/todo/_base.html
@layer base {
h1 { @apply text-3xl font-bold mb-4 text-primary; }
h2 { @apply text-2xl font-semibold mb-3 text-primary; }
...
button {
@apply bg-primary text-white px-4 py-2 rounded cursor-pointer hover:opacity-90 self-start;
}
}
@layer baseholds the default styles for plain elements likeh1andbutton.@applylets us use Tailwind classes inside CSS. So everyh1getstext-3xl font-bold mb-4 text-primary, without us adding those classes to each one.
This keeps our templates short. We only add classes in the HTML when something needs to look different from the default, like each todo item’s card.
This isn’t how you would set up Tailwind in production. The browser build loads more data than it needs to, and makes the browser do extra work to generate the CSS on every page load. In many cases that’s fine. In a serious project, you would use the Tailwind CLI to generate only the CSS that the browser needs, ahead of time.
Play with it
The best way to get a feel for Tailwind is to change some classes and watch what happens. Make sure the development server is running and open http://127.0.0.1:8000/. The page reloads every time you save.
Change the primary colour
Open todo/templates/todo/_base.html and change teal to something else:
todo/templates/todo/_base.html
@theme {
--color-primary: crimson;
}
The headings, buttons, checkboxes and scrollbar all change together, because they all use primary.
Some colours to try:
- Any CSS colour name:
crimson,rebeccapurple,darkorange,royalblue,seagreen. - A hex code:
#e11d48,#7c3aed,#0284c7.
Change the text sizes
In the same file, find the h1 and h2 lines and change their size classes:
todo/templates/todo/_base.html
h1 { @apply text-6xl font-bold mb-4 text-primary; }
h2 { @apply text-sm font-semibold mb-3 text-primary; }
Tailwind’s font sizes go from small to big:
text-xs, text-sm, text-base, text-lg, text-xl, text-2xl, text-3xl, … all the way up to text-9xl.
Try making the p text bigger too, by adding a size to its line:
todo/templates/todo/_base.html
p { @apply mb-4 text-xl; }
Change the todo item cards
Open todo/templates/todo/partials/todo_item.html. The first line styles the card around each todo item:
todo/templates/todo/partials/todo_item.html
<div class="bg-white border border-gray-200 rounded-lg shadow-sm p-4">
Change rounded-lg to change how round the corners are. Some border radius options:
rounded-none: sharp corners.rounded-sm,rounded-md,rounded-lg: a little rounder each time.rounded-xl,rounded-2xl,rounded-3xl: very round.rounded-full: as round as it gets. Each card turns into a pill.
Change border-gray-200 to change the border colour:
border-gray-400: a darker grey. The number goes from50(lightest) to950(darkest).border-sky-500,border-amber-400,border-rose-300: any of Tailwind’s colours.border-primary: our own colour from@theme.
Hard to see? Make the border thicker by changing border to border-4.
When you’re done playing, undo your changes so your code matches the rest of the tutorial:
git restore todo/templates