Tailwind helps you style your web pages quickly using ready-made classes. This example shows how to create a simple component that says "Hello World" with nice styles.
First Tailwind component (Hello World)
Start learning this pattern below
Jump into concepts and practice - no test required
<div class="text-center text-blue-600 font-bold text-2xl">Hello World</div>Use class attribute to add Tailwind classes.
Classes like text-center and text-blue-600 control alignment and color.
<div class="text-center text-red-500 font-semibold text-xl">Hello World</div><div class="text-left text-green-700 italic text-3xl">Hello World</div>This is a complete HTML page using Tailwind CSS from a CDN. The body uses flexbox to center the message both vertically and horizontally. The message is styled with blue color, bold font, and larger text size. It also includes accessibility with role and aria-label.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>First Tailwind Component</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="bg-gray-100 flex items-center justify-center min-h-screen"> <div class="text-center text-blue-600 font-bold text-2xl" role="main" aria-label="Greeting message"> Hello World </div> </body> </html>
Tailwind classes are mobile-first and responsive by default.
Use semantic HTML and accessibility attributes for better user experience.
You can try changing the color or size by editing the class names and refreshing the browser.
Tailwind uses simple class names to style elements quickly.
Adding classes like text-center and text-blue-600 changes alignment and color.
Using Tailwind CDN lets you try styles without setup.
Practice
text-center do to an element?Solution
Step 1: Understand the class name meaning
The classtext-centerin Tailwind means text alignment is centered horizontally.Step 2: Compare with other options
Other options describe different styles like bold or color, which are not related totext-center.Final Answer:
Centers the text horizontally inside the element -> Option AQuick Check:
text-centeraligns text center [OK]
- Confusing text-center with text color classes
- Thinking it makes text bold
- Assuming it adds spacing
<h1> element to make the text blue and centered?Solution
Step 1: Check Tailwind class syntax
Tailwind uses classes liketext-centerandtext-blue-600exactly as written in<h1 class="text-center text-blue-600">Hello World</h1>.Step 2: Identify incorrect class names
Options B, C, and D use invalid or non-existent Tailwind class names.Final Answer:
<h1 class="text-center text-blue-600">Hello World</h1> -> Option BQuick Check:
Correct Tailwind class names =<h1 class="text-center text-blue-600">Hello World</h1>[OK]
- Mixing up class name order or spelling
- Using non-Tailwind class names
- Adding extra hyphens or missing parts
<h1 class="text-center text-red-500 font-bold">Hello World</h1>
Solution
Step 1: Decode Tailwind classes
text-centercenters text,text-red-500colors text red,font-boldmakes text bold.Step 2: Match visual description
A centered, bold, red heading saying 'Hello World' matches all these styles exactly.Final Answer:
A centered, bold, red heading saying 'Hello World' -> Option CQuick Check:
Classes = center + red + bold = A centered, bold, red heading saying 'Hello World' [OK]
- Confusing color codes with other colors
- Ignoring font weight class
- Assuming default alignment
<h1 class="text-center text-blue600 font-bold">Hello World</h1>
Solution
Step 1: Check Tailwind color class format
Tailwind color classes require a dash between color and number, e.g.,text-blue-600.Step 2: Verify other classes
text-centerandfont-boldare valid classes; uppercase is not required.Final Answer:
Missing dash in color class; should be text-blue-600 -> Option AQuick Check:
Color class format error = Missing dash in color class; should be text-blue-600 [OK]
- Omitting dash in color classes
- Thinking class names need uppercase
- Replacing font-bold with wrong class
Solution
Step 1: Understand Tailwind responsive prefixes
md:prefix applies styles at medium screen size and above.Step 2: Match desired styles
Small screens:text-center text-blue-600. Medium+:md:text-left md:text-red-600.Step 3: Verify
text-center text-blue-600 md:text-left md:text-red-600matches this exactlytext-center text-blue-600 md:text-left md:text-red-600uses correct order and classes for the requirement.Final Answer:
text-center text-blue-600 md:text-left md:text-red-600 -> Option DQuick Check:
Responsive classes with md: prefix =text-center text-blue-600 md:text-left md:text-red-600[OK]
- Reversing small and medium screen classes
- Missing md: prefix for medium styles
- Using wrong color or alignment classes
