📖 Scenario: You are building a simple webpage using Tailwind CSS. To keep your CSS file small and fast, you want to remove unused styles from the final build.
🎯 Goal: Create a basic HTML page with Tailwind CSS classes, then configure Tailwind to purge unused styles by specifying the correct content paths.
📋 What You'll Learn
Create an HTML file with Tailwind CSS classes
Add a Tailwind configuration file
Specify the content paths in the Tailwind config for purging
Build the final CSS with purging enabled
💡 Why This Matters
🌍 Real World
Purging unused CSS styles helps websites load faster by reducing file size, improving user experience especially on slow connections.
💼 Career
Web developers often optimize CSS delivery using tools like Tailwind's purge feature to create efficient, maintainable websites.
Progress0 / 4 steps
1
Create a simple HTML file with Tailwind classes
Create an HTML file named index.html with a <main> element that has the classes bg-blue-500 and text-white. Inside <main>, add a <h1> with the text Welcome to Tailwind and the class text-3xl.
Tailwind
Hint
Use the class attribute to add Tailwind classes to your HTML elements.
2
Create the Tailwind configuration file
Create a file named tailwind.config.js and add a module.exports object with an empty content array.
Tailwind
Hint
The content array tells Tailwind where to look for class names to keep.
3
Add content paths to Tailwind config for purging
In tailwind.config.js, add the path './index.html' inside the content array to tell Tailwind to scan this file for used classes.
Tailwind
Hint
Put the exact string './index.html' inside the content array.
4
Build the final CSS with purging enabled
Run the Tailwind CLI command npx tailwindcss -i ./src/input.css -o ./dist/output.css --minify to generate the final CSS file with unused styles removed. Assume your input CSS file ./src/input.css contains @tailwind base;, @tailwind components;, and @tailwind utilities;.
Tailwind
Hint
Use the exact CLI command to build your CSS with purging.
Practice
(1/5)
1. What is the main purpose of purging unused styles in Tailwind CSS?
easy
A. To remove unused CSS classes and make the site load faster
B. To add more CSS classes automatically
C. To change the colors of the website
D. To create new HTML elements
Solution
Step 1: Understand what purging means
Purging means removing things that are not needed. In Tailwind CSS, it removes unused styles.
Step 2: Know why removing unused styles helps
Removing unused styles makes the CSS file smaller, so the website loads faster.
Final Answer:
To remove unused CSS classes and make the site load faster -> Option A
Quick Check:
Purging = Remove unused styles = Faster site [OK]
Hint: Purging means removing unused CSS to speed up loading [OK]
Common Mistakes:
Thinking purging adds styles instead of removing
Confusing purging with changing colors or layout
Believing purging creates new HTML elements
2. Which Tailwind config option tells Tailwind where to look for used CSS classes to purge?
easy
A. theme
B. content
C. plugins
D. variants
Solution
Step 1: Identify the config option for purging
The content option lists files where Tailwind looks for class names to keep.
Step 2: Understand other options
theme controls design tokens, plugins add features, variants control responsive states, so they don't control purging.
Final Answer:
content -> Option B
Quick Check:
content = files to scan for classes [OK]
Hint: Remember: content = where Tailwind looks for classes [OK]
Which files will Tailwind scan to keep CSS classes?
medium
A. All files in src folder and subfolders with .html extension
B. Only files named index.html in src folder
C. All JavaScript files in src folder
D. No files will be scanned
Solution
Step 1: Understand the glob pattern './src/**/*.html'
This pattern means all .html files inside src and any subfolders.
Step 2: Match options with pattern meaning
All files in src folder and subfolders with .html extension matches the pattern exactly. Options B and C are too narrow or wrong file types. No files will be scanned is incorrect.
Final Answer:
All files in src folder and subfolders with .html extension -> Option A
Quick Check:
./src/**/*.html = all .html files in src and subfolders [OK]
Hint: ** means any folder depth, *.html means all HTML files [OK]
Common Mistakes:
Thinking only top-level files are scanned
Confusing file extensions scanned
Assuming no files scanned without explicit mention
4. You set the content option incorrectly as ['./src/*.js'] but your classes are in HTML files inside subfolders. What problem will occur?
medium
A. Tailwind will include all styles correctly
B. Tailwind will generate duplicate CSS classes
C. Tailwind will throw a syntax error
D. Tailwind will purge needed styles causing missing styles on the site
Solution
Step 1: Analyze the content pattern './src/*.js'
This pattern only scans JavaScript files directly inside src folder, not HTML or subfolders.
Step 2: Understand impact on purging
Since HTML files with classes are not scanned, Tailwind thinks those classes are unused and removes them, causing missing styles.
Final Answer:
Tailwind will purge needed styles causing missing styles on the site -> Option D
Quick Check:
Wrong content path = missing styles after purge [OK]
Hint: Content must match all files with classes to avoid missing styles [OK]
Common Mistakes:
Expecting Tailwind to scan files not listed
Thinking wrong content causes errors instead of missing styles
Assuming duplicate CSS classes will be generated
5. You want to purge unused styles but keep some dynamic classes added by JavaScript like bg-red-500 and text-xl. How can you ensure these classes are not removed?
hard
A. Write these classes directly in your HTML files only
B. Remove the content option entirely
C. Add these classes to the safelist option in Tailwind config
D. Use a plugin to disable purging
Solution
Step 1: Understand dynamic classes and purging
Classes added by JavaScript are not in HTML files, so Tailwind may remove them during purge.
Step 2: Use safelist option to keep classes
The safelist option lets you list classes to always keep, even if not found in scanned files.
Final Answer:
Add these classes to the safelist option in Tailwind config -> Option C
Quick Check:
Safelist = keep dynamic classes safe from purge [OK]
Hint: Use safelist to protect dynamic classes from purging [OK]
Common Mistakes:
Removing content option disables purging but is not recommended
Assuming writing classes in HTML fixes dynamic JS classes
Thinking disabling purging plugin is best solution
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.