📖 Scenario: You are building a simple webpage that shows a welcome message and a button. To make the page load faster, you want to extract only the critical CSS needed for the above-the-fold content using Tailwind CSS.
🎯 Goal: Create a minimal HTML page styled with Tailwind CSS classes. Then configure Tailwind to extract only the critical CSS for the visible content.
📋 What You'll Learn
Create an HTML file with a <main> section containing a heading and a button.
Use Tailwind CSS utility classes for styling the heading and button.
Add a Tailwind configuration file with a content array that includes the HTML file path.
Configure Tailwind to generate only the CSS used in the HTML file (critical CSS).
💡 Why This Matters
🌍 Real World
Extracting critical CSS helps webpages load faster by sending only the styles needed for the visible part of the page first.
💼 Career
Web developers optimize site performance by configuring tools like Tailwind to generate minimal CSS, improving user experience and SEO.
Progress0 / 4 steps
1
Create the HTML structure with Tailwind classes
Create an HTML file named index.html with a <main> element. Inside <main>, add an <h1> with the text Welcome to Critical CSS and a <button> with the text Click Me. Use Tailwind classes text-3xl font-bold on the <h1> and bg-blue-500 text-white px-4 py-2 rounded on the <button>.
Tailwind
Hint
Remember to use the exact Tailwind classes on the <h1> and <button> as specified.
2
Add Tailwind configuration with content paths
Create a tailwind.config.js file. Inside it, export a configuration object with a content array that includes the path "./index.html". This tells Tailwind which files to scan for class names.
Tailwind
Hint
Make sure the content array includes exactly "./index.html".
3
Build Tailwind CSS to extract critical styles
Create a CSS file named input.css with the content @tailwind base;, @tailwind components;, and @tailwind utilities; each on its own line. Then run the Tailwind CLI command npx tailwindcss -i ./input.css -o ./dist/output.css --minify to generate the critical CSS file.
Tailwind
Hint
Write the three Tailwind directives exactly as shown, each on its own line.
4
Link the generated CSS and verify critical CSS extraction
Ensure your index.html file links to the generated CSS file at ./dist/output.css using a <link> tag inside the <head>. This completes the setup to use only the critical CSS for your page.
Tailwind
Hint
Make sure the <link> tag is inside the <head> and points to ./dist/output.css.
Practice
(1/5)
1. What is the main purpose of extracting critical CSS in a Tailwind project?
easy
A. To increase the size of the CSS file
B. To add more colors to the website
C. To disable all animations on the page
D. To load only the CSS needed for the visible part of the page first
Solution
Step 1: Understand what critical CSS means
Critical CSS means the styles needed to show the visible part of the page immediately.
Step 2: Identify the benefit of extracting critical CSS
Extracting critical CSS helps load only these important styles first, speeding up page load.
Final Answer:
To load only the CSS needed for the visible part of the page first -> Option D
Quick Check:
Critical CSS = Load visible styles first [OK]
Hint: Critical CSS loads visible styles first to speed up page [OK]
Common Mistakes:
Thinking it adds more styles instead of reducing initial load
Confusing critical CSS with full CSS bundle
Believing it disables animations
2. Which Tailwind CLI flag is used to extract critical CSS automatically?
easy
A. --extract-critical
B. --critical-css
C. --extract-css
D. --critical-extract
Solution
Step 1: Recall the Tailwind CLI flag for critical CSS extraction
The correct flag is --extract-critical as per Tailwind documentation.
Step 2: Compare with other options
Other options are incorrect or do not exist in Tailwind CLI.
Final Answer:
--extract-critical -> Option A
Quick Check:
Flag for critical CSS = --extract-critical [OK]
Hint: Remember the exact flag: --extract-critical [OK]
Common Mistakes:
Mixing flag words or order
Using similar but invalid flags
Forgetting the double dash prefix
3. Given this Tailwind CLI command: tailwindcss -i input.css -o output.css --extract-critical What will happen when you open the webpage using output.css?
medium
A. The CSS file will be empty
B. Only the CSS needed for the visible part loads first, improving speed
C. No CSS will load because the flag disables styles
D. The entire CSS loads at once, no speed improvement
Solution
Step 1: Understand the effect of --extract-critical flag
This flag extracts and includes only critical CSS for initial load.
Step 2: Predict the webpage behavior
The page loads faster because only needed styles for visible content are loaded first.
Final Answer:
Only the CSS needed for the visible part loads first, improving speed -> Option B
Quick Check:
--extract-critical improves load speed by loading visible styles first [OK]
Hint: Flag extracts visible styles first, speeding page load [OK]
Common Mistakes:
Thinking the flag disables CSS
Assuming full CSS loads immediately
Believing CSS file becomes empty
4. You ran tailwindcss -i input.css -o output.css --extract-critical but the page still loads slowly. What could be a reason?
medium
A. You forgot to include the generated output.css in your HTML file
B. The flag --extract-critical disables all CSS animations
C. Tailwind CLI does not support critical CSS extraction
D. You need to add --fast-load flag instead
Solution
Step 1: Check if CSS file is linked in HTML
If the generated CSS file is not included in the HTML, styles won't apply, causing slow or broken page.
Step 2: Verify other options
Other options are incorrect: the flag does not disable animations, Tailwind supports extraction, and --fast-load is not a valid flag.
Final Answer:
You forgot to include the generated output.css in your HTML file -> Option A
Quick Check:
Missing CSS link = no styles applied [OK]
Hint: Always link generated CSS file in HTML to see styles [OK]
Common Mistakes:
Assuming flag disables animations
Thinking Tailwind lacks critical CSS support
Using non-existent CLI flags
5. You want to optimize a large Tailwind project by extracting critical CSS only for the homepage visible content. Which approach is best?
hard
A. Manually copy visible styles into a separate CSS file without using Tailwind CLI flags
B. Include the entire Tailwind CSS file on all pages without extraction
C. Run Tailwind CLI with --extract-critical on homepage CSS and load it first, then lazy-load other styles
D. Disable Tailwind CSS and write all styles in plain CSS
Solution
Step 1: Use Tailwind CLI with --extract-critical for homepage
This extracts only the CSS needed for visible homepage content, improving load speed.
Step 2: Load extracted CSS first and lazy-load others
Loading critical CSS first ensures fast rendering; lazy-loading other styles avoids blocking.
Final Answer:
Run Tailwind CLI with --extract-critical on homepage CSS and load it first, then lazy-load other styles -> Option C