Offsets help you move elements away from the edges of their container. They control space from the top, right, bottom, or left sides.
Top, right, bottom, left offsets in Tailwind
Start learning this pattern below
Jump into concepts and practice - no test required
top-{size}
right-{size}
bottom-{size}
left-{size}Replace {size} with Tailwind spacing values like 0, 1, 2, 4, 8, etc.
These classes work when the element has relative, absolute, or fixed positioning.
<div class="relative top-4">Content</div><div class="absolute right-8">Box</div><div class="fixed bottom-2 left-2">Fixed Box</div>This example shows a box with four smaller boxes positioned inside it using top, right, bottom, and left offsets. Each small box is moved away from the edges by 1rem (4 in Tailwind spacing).
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Top, Right, Bottom, Left Offsets Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="relative h-screen bg-gray-100"> <div class="relative bg-white p-4 w-64 h-40 border border-gray-300"> <div class="absolute top-4 left-4 bg-blue-500 text-white p-2 rounded"> Top Left Offset </div> <div class="absolute top-4 right-4 bg-green-500 text-white p-2 rounded"> Top Right Offset </div> <div class="absolute bottom-4 left-4 bg-red-500 text-white p-2 rounded"> Bottom Left Offset </div> <div class="absolute bottom-4 right-4 bg-yellow-500 text-black p-2 rounded"> Bottom Right Offset </div> </div> </body> </html>
Offsets only work if the element has a positioning context like relative, absolute, or fixed.
Use Tailwind spacing scale for consistent spacing (e.g., 1 = 0.25rem, 4 = 1rem).
Offsets move the element visually but do not affect the space it takes in the normal page flow.
Top, right, bottom, and left offsets move elements away from container edges.
They require the element to have a position other than static.
Use Tailwind classes like top-4, right-8 to set these offsets easily.
Practice
relative positioning?Solution
Step 1: Understand Tailwind spacing scale
In Tailwind,4equals 1rem spacing.Step 2: Identify offset direction
bottom-4moves the element 1rem down from the top edge when positioned.Final Answer:
bottom-4-> Option BQuick Check:
Bottom offset 1rem = bottom-4 [OK]
bottom- plus spacing number [OK]- Confusing top with bottom classes
- Using offset classes without positioning
- Mixing left/right with top offsets
Solution
Step 1: Know Tailwind spacing values
Spacing8equals 2rem in Tailwind.Step 2: Match direction and spacing
left-8moves element 2rem to the right from its container edge.Final Answer:
left-8-> Option AQuick Check:
Left offset 2rem = left-8 [OK]
left- plus spacing number [OK]- Using
right-2which is 0.5rem, not 2rem - Confusing left and right classes
- Using top or bottom classes for horizontal offset
<div class="relative"> <div class="absolute top-6 left-4">Box</div> </div>
Solution
Step 1: Understand Tailwind spacing scale
top-6means 1.5rem down,left-4means 1rem right offset.Step 2: Positioning context
The parent isrelative, soabsolutechild offsets from parent's edges.Final Answer:
The inner box moves 1.5rem down and 1rem right inside the relative container. -> Option DQuick Check:
top-6 = 1.5rem down, left-4 = 1rem right [OK]
- Thinking top-6 moves element up
- Ignoring relative positioning on parent
- Confusing left offset direction
<div class="absolute top-4 right-4">Hello</div>when the parent has no positioning class.
Solution
Step 1: Understand positioning context
Anabsoluteelement 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 CQuick Check:
Absolute needs positioned parent for expected offsets [OK]
- Assuming offsets always relate to parent
- Thinking classes are invalid
- Confusing absolute with relative positioning
relative container. Which Tailwind classes achieve this?Solution
Step 1: Match spacing scale to rem values
In Tailwind,3equals 0.75rem and2equals 0.5rem spacing.Step 2: Assign correct classes for top and right offsets
Usetop-3for 0.75rem top offset andright-2for 0.5rem right offset.Final Answer:
top-3 right-2-> Option AQuick Check:
top-3 = 0.75rem, right-2 = 0.5rem [OK]
- Mixing up spacing numbers and rem values
- Using larger spacing than needed
- Forgetting to set parent position to relative
