Vibe coding with AI course ยท Module 2: Next.js Setup
Tailwind CSS - Styling Without Writing CSS
In this lesson9
Time to learn Tailwind CSS - one of the most popular CSS frameworks (create-next-app installs it by default).
What is Tailwind CSS?
Tailwind is a utility-first CSS framework. Instead of writing CSS manually, you use ready-made classes.
Traditional CSS:
.button {
background-color: #dc2626;
color: white;
padding: 12px 24px;
border-radius: 8px;
font-weight: 600;
}<button class="button">Click me</button>Tailwind CSS:
<button class="bg-red-600 text-white px-6 py-3 rounded-lg font-semibold">
Click me
</button>Everything in HTML classes! You don't write CSS - you use ready-made classes.
Why Tailwind?
Speed - no switching between HTML and CSS Consistency - one system of colors, spacing Responsiveness - easy breakpoints (sm:, md:, lg:) No conflicts - no accidentally overriding styles Tree-shaking - only used classes in the final CSS Used by - OpenAI, Shopify, Reddit and NASA JPL, among others (list on tailwindcss.com)
Basic Tailwind Classes
1. Colors
Background:
bg-red-500- red backgroundbg-blue-600- blue backgroundbg-black- black backgroundbg-white- white background
Text:
text-red-500- red texttext-blue-600- blue texttext-gray-900- dark gray text
Colors have shades from 50 (lightest) to 950 (darkest).
2. Padding and Margin
Padding (inner spacing):
p-4- padding 16px on all sidespx-4- padding 16px left and rightpy-4- padding 16px top and bottompt-4- padding-top 16pxpr-4- padding-right 16px
Margin (outer spacing):
m-4- margin 16pxmx-auto- centering (margin left/right auto)mt-8- margin-top 32px
Scale: 0, 1, 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 32, 40...
p-1= 4pxp-2= 8pxp-4= 16pxp-8= 32px
See different paddings side by side and change the numbers in the classes:
Code for this lesson: index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tailwind Padding</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 p-8">
<!-- Padding example -->
<div class="bg-black text-white p-4">
This is a div with black background, white text and 16px padding (p-4)
</div>
<div class="mt-4 bg-blue-600 text-white px-8 py-4">
This div has horizontal padding 32px (px-8) and vertical 16px (py-4)
</div>
<div class="mt-4 bg-red-600 text-white p-0">
This div has padding 0 (p-0)
</div>
<div class="mt-4 bg-green-600 text-white p-8">
This div has padding 32px (p-8)
</div>
</body>
</html>3. Text
Size:
text-xs- extra smalltext-sm- smalltext-base- normal (16px)text-lg- largetext-xl- extra largetext-2xl,text-3xl,text-4xl- larger
Weight:
font-light- lightfont-normal- normalfont-medium- mediumfont-semibold- semi-boldfont-bold- bold
Alignment:
text-left- lefttext-center- centertext-right- right
4. Layout
Display:
block- display: blockinline- display: inlineflex- display: flexgrid- display: gridhidden- display: none
Flexbox:
flex- activates flexboxflex-row- horizontal direction (default)flex-col- vertical directionitems-center- vertical centeringjustify-center- horizontal centeringgap-4- gap between elements
This is what an element centered vertically and horizontally looks like (h-screen gives it the full screen height):
Code for this lesson: index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tailwind Flexbox</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<!-- Flexbox centered vertically and horizontally -->
<div class="flex items-center justify-center h-screen bg-gradient-to-br from-blue-500 to-purple-600">
<div class="bg-white p-8 rounded-lg shadow-2xl">
<h1 class="text-3xl font-bold text-gray-800 mb-4">
Centered element!
</h1>
<p class="text-gray-600">
This div is centered vertically and horizontally thanks to:
</p>
<ul class="mt-4 space-y-2 text-sm text-gray-700">
<li><code class="bg-gray-100 px-2 py-1 rounded">flex</code> - activates flexbox</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">items-center</code> - vertical centering</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">justify-center</code> - horizontal centering</li>
<li><code class="bg-gray-100 px-2 py-1 rounded">h-screen</code> - height 100vh</li>
</ul>
</div>
</div>
</body>
</html>5. Width and Height
Width:
w-full- width: 100%w-1/2- width: 50%w-screen- width: 100vwmax-w-md- max-width: 448pxmax-w-4xl- max-width: 896px
Height:
h-screen- height: 100vh (full screen)min-h-screen- min-height: 100vh
6. Border and Radius
Border:
border- border 1pxborder-2- border 2pxborder-red-500- red border
Radius (rounding):
rounded- border-radius 4pxrounded-lg- larger roundingrounded-full- maximum rounding: a pill, and a circle on a square
Responsiveness - Breakpoints
Tailwind has built-in breakpoints:
sm:- from 640pxmd:- from 768px (e.g. a tablet in portrait)lg:- from 1024px (e.g. a tablet in landscape, a small laptop)xl:- from 1280px (laptop, monitor)2xl:- from 1536px (large monitor)
Each prefix works from the given width up.
Example:
<div class="text-sm md:text-base lg:text-lg">
Responsive text
</div>- Mobile:
text-sm(14px) - Tablet:
text-base(16px) - Laptop:
text-lg(18px)
Mobile-first: you write for mobile, then add breakpoints for larger screens.
Change the width of the preview and watch the number of columns and the text size change:
Code for this lesson: index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tailwind Responsiveness</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 p-8">
<h1 class="text-3xl font-bold mb-8 text-center">
Responsiveness in Tailwind
</h1>
<!-- Mobile: 1 column, Tablet: 2 columns, Desktop: 3 columns -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
<div class="bg-blue-500 text-white p-6 rounded-lg">
<h2 class="text-xl font-bold mb-2">Card 1</h2>
<p>Resize the window to see responsiveness!</p>
</div>
<div class="bg-purple-500 text-white p-6 rounded-lg">
<h2 class="text-xl font-bold mb-2">Card 2</h2>
<p>Mobile: 1 column<br>Tablet (md): 2 columns<br>Desktop (lg): 3 columns</p>
</div>
<div class="bg-pink-500 text-white p-6 rounded-lg">
<h2 class="text-xl font-bold mb-2">Card 3</h2>
<p>Breakpoints:<br>sm: 640px<br>md: 768px<br>lg: 1024px</p>
</div>
</div>
<!-- Responsive text -->
<div class="mt-8 bg-white p-6 rounded-lg shadow">
<p class="text-sm md:text-base lg:text-lg xl:text-xl">
This text has different sizes:<br>
Mobile: text-sm (14px)<br>
Tablet: text-base (16px)<br>
Laptop: text-lg (18px)<br>
Desktop: text-xl (20px)
</p>
</div>
</body>
</html>Practical Example - Pirate Button
<button class="
bg-red-600
hover:bg-red-700
text-white
font-bold
py-3
px-6
rounded-lg
transition-colors
duration-200
">
Join the crew!
</button>What's happening:
bg-red-600- red backgroundhover:bg-red-700- darker on hovertext-white- white textfont-bold- boldpy-3- padding top/bottompx-6- padding left/rightrounded-lg- rounded cornerstransition-colors- smooth color transitionduration-200- 200ms transition
Using AI for Tailwind
Instead of memorizing all classes, ask AI!
Prompt:
Create a pirate hero section in Tailwind with a black background, red heading and gold buttonAI will generate the entire code with appropriate Tailwind classes!
Tailwind in Next.js
In Next.js, Tailwind is already configured if you chose the recommended defaults or Tailwind CSS when creating the project.
Configuration files:
app/globals.css- imports Tailwind and sets the theme (the@themeblock)postcss.config.mjs- connects the@tailwindcss/postcssplugin
At the top of globals.css you'll see:
@import "tailwindcss";This single line imports all Tailwind classes. This is Tailwind 4 syntax: the old @tailwind base, @tailwind components and @tailwind utilities directives and the tailwind.config.ts file come from version 3 and are not in new projects.
Dark Mode in Tailwind
Tailwind supports dark mode:
<div class="bg-white dark:bg-gray-900 text-black dark:text-white">
Auto dark mode!
</div>Summary
Tailwind CSS - utility-first framework Classes in HTML - no manual CSS writing bg-, text-, p-, m- - basic classes sm:, md:, lg: - responsive breakpoints hover:, focus: - interactive states AI helps - ask for specific styles
In the next exercise we'll create a landing page for "Kraken's Call"!
See you there!
Code for this lesson: index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pirate Button</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-900 flex items-center justify-center h-screen">
<div class="text-center">
<h1 class="text-5xl font-bold text-white mb-8">
Black Pearl of Code
</h1>
<!-- Pirate button with hover effect -->
<button class="
bg-red-600
hover:bg-red-700
text-white
font-bold
px-6
py-3
rounded-lg
transition-colors
duration-200
shadow-lg
hover:shadow-xl
transform
hover:scale-105
">
Join the crew!
</button>
<div class="mt-8 max-w-md mx-auto">
<p class="text-gray-400 text-sm">
Hover over the button to see the hover effect!
</p>
<div class="mt-4 bg-gray-800 p-4 rounded text-left text-xs text-gray-300">
<p class="font-mono">
bg-red-600 hover:bg-red-700<br>
text-white font-bold<br>
px-6 py-3 rounded-lg<br>
transition-colors duration-200<br>
shadow-lg hover:shadow-xl<br>
transform hover:scale-105
</p>
</div>
</div>
</div>
</body>
</html>Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What is Tailwind CSS?
2. Which Tailwind class sets a red background?
3. Which Tailwind prefix applies a style from a screen width of 768px up?
4. Which Tailwind class horizontally centers items inside a flexbox?
5. What does the hover:bg-red-700 class do in Tailwind?
6. Which Tailwind class sets the element's width to 100%?
7. What do you need to do to use Tailwind in Next.js if you chose Tailwind CSS (or the recommended defaults) when creating the project?
8. What does the dark: prefix do in a Tailwind class (e.g. dark:bg-gray-900)?
9. Which Tailwind class sets the height to the full screen (100vh)?
10. Which Tailwind class centers a block using automatic left and right margins?
11. Which Tailwind class hides an element (display: none)?
Hands-on tasks in the game
- Code editor
Create a div with a black background (bg-black), white text (text-white) and 16px padding (p-4). Put any text inside it.
- Click in order
Build the Tailwind class that sets white text color:
- Vertical ordering
Arrange the Tailwind padding classes from smallest to largest:
- Code editor
Create a div with the classes flex, items-center, justify-center and h-screen, and put an element with text inside it, e.g. <p>. The element should sit in the middle of the screen, vertically and horizontally.
- Vertical ordering
Arrange the Tailwind breakpoints from the smallest screen to the largest:
- Horizontal ordering
Arrange the code of a red Tailwind button (left to right):
- Code editor
Build a pirate <button>: red background (e.g. bg-red-600), white text (text-white), padding (e.g. px-6 py-3), rounded corners (e.g. rounded-lg) and a darker background on mouse hover (e.g. hover:bg-red-700).
- Click in order
Build the Tailwind class that rounds corners with a larger radius:
- Horizontal ordering
Arrange the parts of the Tailwind class for a red background (left to right):
- Vertical ordering
Arrange the Tailwind font-weight classes from thinnest to boldest:
- Vertical ordering
Arrange the Tailwind text-size classes from smallest to largest:
- Vertical ordering
Arrange the classes needed to center an element vertically and horizontally with Tailwind:
- Horizontal ordering
Arrange the parts of the Tailwind class for a 32px top margin (left to right):
- Vertical ordering
Arrange the steps for using AI in Cursor to generate Tailwind styles:
- Click in order
Build the Tailwind class for extra large text:
- Click in order
Build the Tailwind class that sets the vertical flexbox direction: