Discover how a few simple classes can save you hours of frustrating text alignment work!
Why Text alignment in Tailwind? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you are creating a webpage and want to center a heading, align a paragraph to the right, and keep a footer text on the left.
If you try to do this by adding spaces or using multiple characters, it becomes messy and inconsistent across different screen sizes and devices.
Text alignment classes in Tailwind let you easily set text to left, center, or right alignment with simple, reusable class names that work perfectly on all screen sizes.
This is a heading
This is a paragraph aligned right
Footer text<h1 class="text-center">This is a heading</h1> <p class="text-right">This is a paragraph aligned right</p> <footer class="text-left">Footer text</footer>
You can quickly and reliably control how text lines up on your page, making your content look neat and professional everywhere.
When building a blog post, you want the title centered, the author info aligned right, and the main text aligned left for easy reading. Tailwind text alignment classes make this simple.
Manual spacing for alignment is unreliable and hard to maintain.
Tailwind text alignment classes provide easy, consistent control.
They help create clean, responsive layouts quickly.
Practice
Solution
Step 1: Understand text alignment classes
Tailwind providestext-left,text-center,text-right, andtext-justifyto align text horizontally.Step 2: Identify the class for center alignment
The classtext-centeraligns text horizontally in the center of its container.Final Answer:
text-center -> Option CQuick Check:
Center aligned text = text-center [OK]
- Confusing text-right with center alignment
- Using text-justify for center alignment
- Forgetting to add any alignment class
Solution
Step 1: Recall Tailwind text alignment classes
Tailwind usestext-left,text-center,text-right, andtext-justifyfor horizontal text alignment.Step 2: Identify the correct class for right alignment
The correct class to align text to the right istext-right. The optiontext-align-rightis not a valid Tailwind class.Final Answer:
text-right -> Option AQuick Check:
Right aligned text = text-right [OK]
- Using text-align-right instead of text-right
- Mixing up text-left and text-right
- Forgetting the text- prefix
<div class="text-justify">This is a sample paragraph.</div>
Solution
Step 1: Understand the
Thetext-justifyclass effecttext-justifyclass makes text spread so both left and right edges align evenly, creating a block of text with straight edges on both sides.Step 2: Identify the visual result
Text inside the div will be stretched to fill the container width with even edges on left and right, not just aligned left, right, or center.Final Answer:
Text stretched to fill the container width with even edges -> Option DQuick Check:
Justified text = text-justify [OK]
- Confusing justify with center alignment
- Expecting text-justify to align left only
- Ignoring the effect of text-justify on spacing
<p class="text-center sm:text-left">Hello World</p>
Solution
Step 1: Understand responsive prefixes in Tailwind
Classes without prefix apply to all screen sizes. Prefixes likesm:,md:apply from that screen size and up.Step 2: Analyze the requirement and current code
We want left alignment on small screens and center on medium and larger. The current code aligns center by default and left on small screens, which is opposite.Step 3: Correct the classes
Usetext-leftfor default (small screens) andmd:text-centerfor medium and above to get the desired effect.Final Answer:
Replace with text-left md:text-center -> Option BQuick Check:
Default left + md center = text-left md:text-center [OK]
- Mixing sm: and md: prefixes incorrectly
- Not setting default alignment properly
- Assuming sm: applies only to small screens, not up
Solution
Step 1: Understand Tailwind responsive prefixes
Classes without prefix apply to all screen sizes (mobile by default).md:applies from medium screens (tablets) and up,lg:applies from large screens (desktops) and up.Step 2: Match alignment to screen sizes
We want left alignment on mobile (default), center on tablets (md), and right on desktops (lg). So the classes should betext-left,md:text-center, andlg:text-right.Final Answer:
text-left md:text-center lg:text-right -> Option AQuick Check:
Mobile left, tablet center, desktop right = text-left md:text-center lg:text-right [OK]
- Using sm: instead of md: for tablets
- Wrong order of prefixes causing override issues
- Confusing screen size breakpoints
