📖 Scenario: You are building a simple webpage and want to organize your Tailwind CSS styles properly. Tailwind allows you to separate styles into three layers: base for default styles, components for reusable blocks, and utilities for small helper classes.This organization helps keep your styles clean and easy to maintain.
🎯 Goal: Create a Tailwind CSS file that uses the @layer directive to organize styles into base, components, and utilities layers. Add a base style for the body, a component style for a button, and a utility class for margin.
📋 What You'll Learn
Create a @layer base section with a style that sets body background color to #f0f0f0
Create a @layer components section with a class .btn-primary that has blue background and white text
Create a @layer utilities section with a class .m-7 that sets margin to 1.75rem
Use valid Tailwind CSS syntax and layering order
💡 Why This Matters
🌍 Real World
Organizing CSS layers is common in real projects to keep styles manageable and avoid conflicts.
💼 Career
Front-end developers often organize styles with Tailwind CSS layers to build scalable and maintainable user interfaces.
Progress0 / 4 steps
1
Create the base layer with body background color
Create a @layer base block and inside it write CSS to set the body background color to #f0f0f0.
Tailwind
Hint
Use @layer base { body { background-color: #f0f0f0; } } to set the base style.
2
Add a components layer with a primary button style
Add a @layer components block below the base layer. Inside it, create a class .btn-primary that sets background-color to #2563eb (blue) and color to #ffffff (white).
Tailwind
Hint
Use @layer components { .btn-primary { background-color: #2563eb; color: #ffffff; } } to add the button style.
3
Add a utilities layer with a margin utility class
Add a @layer utilities block below the components layer. Inside it, create a class .m-7 that sets margin to 1.75rem.
Tailwind
Hint
Use @layer utilities { .m-7 { margin: 1.75rem; } } to add the margin utility.
4
Complete the Tailwind CSS file with all layers organized
Ensure your Tailwind CSS file contains all three layers: @layer base with body background color, @layer components with .btn-primary styles, and @layer utilities with .m-7 margin class. The layers should be in this order: base, components, utilities.
Tailwind
Hint
Make sure all three layers are present and in the correct order.
Practice
(1/5)
1. What is the main purpose of the base layer in Tailwind CSS?
easy
A. To add small helper classes for margin and padding
B. To create reusable UI components like buttons and cards
C. To set default styles for HTML elements like body and h1
D. To write JavaScript functions for styling
Solution
Step 1: Understand the role of base layer
The base layer sets default styles for basic HTML elements to ensure consistent look.
Step 2: Differentiate from components and utilities
Components are reusable UI parts, utilities are small helper classes, so they differ from base.
Final Answer:
To set default styles for HTML elements like body and h1 -> Option C
Quick Check:
Base layer = default element styles [OK]
Hint: Base layer styles HTML elements directly [OK]
Common Mistakes:
Confusing base with components
Thinking utilities set default HTML styles
Mixing JavaScript with CSS layers
2. Which of the following is the correct way to add a custom component style in Tailwind's layer system?
A. Custom styles for buttons should be in the components layer, not utilities
B. You must use @layer base for all custom classes
C. The @apply directive cannot be used inside @layer
D. You forgot to add !important to the styles
Solution
Step 1: Identify correct layer for button styles
Buttons are reusable UI components, so they belong in the components layer.
Step 2: Understand layer purpose
Utilities are for small helper classes, not full component styles.
Final Answer:
Custom styles for buttons should be in the components layer, not utilities -> Option A
Quick Check:
Components layer = reusable UI parts [OK]
Hint: Put button styles in components, not utilities [OK]
Common Mistakes:
Putting components styles in utilities layer
Misusing @apply directive
Forgetting layer purpose
5. You want to create a custom utility class .text-shadow that adds a subtle shadow to text. Where should you place this style in Tailwind's layer system and why?