Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Top, right, bottom, left offsets with Tailwind CSS
📖 Scenario: You are creating a simple webpage with a colored box. You want to position this box inside a container using offsets from the top, right, bottom, and left edges.
🎯 Goal: Build a webpage with a container and a colored box inside it. Use Tailwind CSS classes to set the box's offsets from the container's edges: top, right, bottom, and left.
📋 What You'll Learn
Create a container with relative positioning
Add a box inside the container with absolute positioning
Use Tailwind CSS classes to set the box's top offset to 4rem
Use Tailwind CSS classes to set the box's right offset to 2rem
Use Tailwind CSS classes to set the box's bottom offset to 1rem
Use Tailwind CSS classes to set the box's left offset to 3rem
Make sure the box has a visible background color and fixed size
💡 Why This Matters
🌍 Real World
Positioning elements precisely inside containers is common in web design for layouts, modals, tooltips, and more.
💼 Career
Knowing how to use CSS positioning and Tailwind offsets helps you build responsive and well-structured user interfaces efficiently.
Progress0 / 4 steps
1
Create the container and the box
Create a <div> with class relative named container. Inside it, add another <div> with class bg-blue-500 and fixed width w-24 and height h-24 named box.
Tailwind
Hint
Use relative on the container to position the box absolutely inside it later.
2
Add absolute positioning to the box
Add the Tailwind CSS class absolute to the box<div> to position it absolutely inside the container.
Tailwind
Hint
The absolute class lets you use top, right, bottom, and left offsets.
3
Set the top and right offsets
Add the Tailwind CSS classes top-16 and right-8 to the box<div> to set the top offset to 4rem and the right offset to 2rem.
Tailwind
Hint
Use top-16 for 4rem and right-8 for 2rem offsets.
4
Set the bottom and left offsets
Add the Tailwind CSS classes bottom-4 and left-12 to the box<div> to set the bottom offset to 1rem and the left offset to 3rem.
Tailwind
Hint
Use bottom-4 for 1rem and left-12 for 3rem offsets.
Practice
(1/5)
1. Which Tailwind class moves an element 1rem down from the top edge when it has relative positioning?
easy
A. right-4
B. bottom-4
C. left-4
D. top-4
Solution
Step 1: Understand Tailwind spacing scale
In Tailwind, 4 equals 1rem spacing.
Step 2: Identify offset direction
bottom-4 moves the element 1rem down from the top edge when positioned.
Final Answer:
bottom-4 -> Option B
Quick Check:
Bottom offset 1rem = bottom-4 [OK]
Hint: Bottom offset uses bottom- plus spacing number [OK]
Common Mistakes:
Confusing top with bottom classes
Using offset classes without positioning
Mixing left/right with top offsets
2. Which is the correct Tailwind class to move an element 2rem to the right from its container edge?
easy
A. left-8
B. right-2
C. right-8
D. top-8
Solution
Step 1: Know Tailwind spacing values
Spacing 8 equals 2rem in Tailwind.
Step 2: Match direction and spacing
left-8 moves element 2rem to the right from its container edge.
Final Answer:
left-8 -> Option A
Quick Check:
Left offset 2rem = left-8 [OK]
Hint: Left offset uses left- plus spacing number [OK]
Common Mistakes:
Using right-2 which is 0.5rem, not 2rem
Confusing left and right classes
Using top or bottom classes for horizontal offset
3. What will be the visual effect of this HTML snippet?
A. The inner box stays at the top-left corner with no offset.
B. The inner box moves 1.5rem up and 1rem left inside the relative container.
C. The inner box moves 6rem down and 4rem right inside the relative container.
D. The inner box moves 1.5rem down and 1rem right inside the relative container.
Solution
Step 1: Understand Tailwind spacing scale
top-6 means 1.5rem down, left-4 means 1rem right offset.
Step 2: Positioning context
The parent is relative, so absolute child offsets from parent's edges.
Final Answer:
The inner box moves 1.5rem down and 1rem right inside the relative container. -> Option D
Quick Check:
top-6 = 1.5rem down, left-4 = 1rem right [OK]
Hint: Absolute inside relative uses offsets from parent edges [OK]
Common Mistakes:
Thinking top-6 moves element up
Ignoring relative positioning on parent
Confusing left offset direction
4. Identify the error in this Tailwind usage:
<div class="absolute top-4 right-4">Hello</div>
when the parent has no positioning class.
medium
A. No error; offsets work relative to viewport by default.
B. The classes top-4 and right-4 are invalid.
C. Offsets won't work as expected because parent is not positioned.
D. The element must have relative class instead of absolute.
Solution
Step 1: Understand positioning context
An absolute element positions relative to nearest positioned ancestor.
Step 2: Check parent positioning
If parent has no position (static by default), offsets relate to viewport, which may cause unexpected placement.
Final Answer:
Offsets won't work as expected because parent is not positioned. -> Option C
Quick Check:
Absolute needs positioned parent for expected offsets [OK]
Hint: Absolute offsets need positioned parent (relative/absolute/fixed) [OK]
Common Mistakes:
Assuming offsets always relate to parent
Thinking classes are invalid
Confusing absolute with relative positioning
5. You want to place a notification badge 0.75rem from the top and 0.5rem from the right inside a relative container. Which Tailwind classes achieve this?
hard
A. top-3 right-2
B. top-2 right-3
C. top-4 right-4
D. top-6 right-1
Solution
Step 1: Match spacing scale to rem values
In Tailwind, 3 equals 0.75rem and 2 equals 0.5rem spacing.
Step 2: Assign correct classes for top and right offsets
Use top-3 for 0.75rem top offset and right-2 for 0.5rem right offset.
Final Answer:
top-3 right-2 -> Option A
Quick Check:
top-3 = 0.75rem, right-2 = 0.5rem [OK]
Hint: Use Tailwind spacing scale: 3=0.75rem, 2=0.5rem for offsets [OK]