Tree-shaking helps remove unused CSS to make your website faster and smaller. Content configuration tells Tailwind where to look for your HTML and JavaScript files so it knows which styles to keep.
Content configuration for tree-shaking in Tailwind
module.exports = {
content: [
'./src/**/*.{html,js}',
'./public/index.html'
],
theme: {
extend: {},
},
plugins: [],
};The content array lists file paths where Tailwind looks for class names.
Use glob patterns like **/*.{html,js} to include many files in folders and subfolders.
src folder, useful for React or TypeScript projects.content: ['./src/**/*.{html,js,jsx,ts,tsx}']public folder and its subfolders.content: ['./public/**/*.html']content: ['./app/**/*.{html,js}', './components/**/*.{js,jsx}']
This simple HTML page uses Tailwind classes. The CSS file output.css is generated by Tailwind using content configuration to include only used styles.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Tailwind Content Config Example</title> <link href="./dist/output.css" rel="stylesheet"> </head> <body class="bg-gray-100 p-6"> <h1 class="text-3xl font-bold text-blue-600">Hello, Tailwind!</h1> <p class="mt-4 text-gray-700">This page uses content configuration for tree-shaking.</p> </body> </html>
Always update the content paths when you add new files or folders with Tailwind classes.
If Tailwind misses styles, check your content paths first.
Using too broad paths can include unused styles, making CSS bigger.
Content configuration tells Tailwind where to find your HTML and JS files.
This helps Tailwind remove unused CSS, making your site faster.
Keep your content paths accurate and updated for best results.
Practice
content configuration in Tailwind CSS?Solution
Step 1: Understand the role of content configuration
Thecontentsetting tells Tailwind which files to look at for CSS class names.Step 2: Identify the effect on CSS output
By scanning these files, Tailwind can remove unused styles, making the CSS smaller and faster.Final Answer:
To tell Tailwind which files to scan for class names to include in the final CSS -> Option AQuick Check:
Content config = file paths for class scanning [OK]
- Confusing content with theme colors
- Thinking content sets animations or fonts
- Ignoring the importance of accurate file paths
content array in tailwind.config.js for scanning HTML and JS files in the src folder?Solution
Step 1: Understand the glob pattern syntax
The pattern./src/**/*.{html,js}means all files ending with .html or .js in any folder inside src.Step 2: Check the array syntax
Thecontentproperty expects an array of strings, so it must be wrapped in [ ].Final Answer:
content: ['./src/**/*.{html,js}'] -> Option AQuick Check:
Array with glob pattern and braces = correct [OK]
- Forgetting the array brackets
- Using parentheses instead of braces in glob
- Missing the double asterisk for recursive search
tailwind.config.js snippet:module.exports = {
content: ['./app/**/*.html'],
theme: { extend: {} },
plugins: [],
}Which files will Tailwind scan for class names?
Solution
Step 1: Analyze the glob pattern './app/**/*.html'
The**means any subfolder insideapp, and*.htmlmeans all HTML files.Step 2: Determine which files match
So, Tailwind scans all HTML files inappand any nested folders.Final Answer:
All HTML files inside the app folder and its subfolders -> Option BQuick Check:
Double star + .html = all HTML recursively [OK]
- Thinking it scans only top-level files
- Assuming it scans all file types
- Confusing file extensions in pattern
content configuration?Solution
Step 1: Understand tree-shaking in Tailwind
Tailwind removes unused CSS by scanning files listed incontent.Step 2: Identify impact of wrong content paths
Ifcontentis empty or wrong, Tailwind can't find used classes and keeps all styles.Final Answer:
Thecontentpaths are missing or incorrect, so Tailwind scans no files -> Option CQuick Check:
Wrong content paths = no tree-shaking [OK]
- Blaming CSS directives instead of content config
- Ignoring the importance of file paths
- Assuming plugins affect CSS size directly
src/components and your HTML files in public. Which content configuration is best?Solution
Step 1: Include all React component file types in src/components
React components use extensionsjs, jsx, ts, tsx, so include these with a recursive glob.Step 2: Include all HTML files in public folder recursively
Use./public/**/*.htmlto cover all HTML files in public and subfolders.Step 3: Avoid overbroad or incomplete patterns
content: ['./src/components/**/*.{js,jsx,ts,tsx}', './public/**/*.html'] precisely targets needed files without extra unrelated files.Final Answer:
content: ['./src/components/**/*.{js,jsx,ts,tsx}', './public/**/*.html'] -> Option DQuick Check:
Specific extensions + recursive glob = best practice [OK]
- Using too broad globs that include unused files
- Missing some React file extensions
- Not using recursive glob for nested files
Ready to prove it?
Pass the test and this pattern moves unknown
