Position utilities help you place elements exactly where you want on the page. They control how elements move and stay in place.
Position utilities (relative, absolute, fixed) in Tailwind
Start learning this pattern below
Jump into concepts and practice - no test required
relative absolute fixed
relative moves the element relative to its normal place.
absolute places the element relative to the nearest positioned parent.
fixed keeps the element fixed on the screen even when scrolling.
<div class="relative">...</div><div class="absolute top-0 right-0">...</div><div class="fixed bottom-0 left-0">...</div>This example shows a white box with relative positioning. Inside it, two buttons are placed absolutely at the top right and bottom left corners. Also, a red box is fixed at the bottom left of the screen and stays visible when you scroll.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Position Utilities Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="bg-gray-100 p-6"> <section class="relative bg-white border border-gray-300 rounded-lg p-6 w-64 h-40"> <p class="mb-4">This box is <strong>relative</strong> positioned.</p> <button class="absolute top-2 right-2 bg-blue-600 text-white px-3 py-1 rounded">Top Right</button> <button class="absolute bottom-2 left-2 bg-green-600 text-white px-3 py-1 rounded">Bottom Left</button> </section> <div class="fixed bottom-4 left-4 bg-red-600 text-white px-4 py-2 rounded shadow-lg"> Fixed at bottom left of screen </div> </body> </html>
Use relative on a parent to make absolute children position relative to it.
fixed elements stay visible on screen even when you scroll the page.
Combine position utilities with top, right, bottom, left classes to move elements.
Position utilities control where elements appear on the page.
relative moves elements from their normal spot.
absolute places elements relative to the nearest positioned parent.
fixed keeps elements visible on screen during scrolling.
Practice
Solution
Step 1: Understand the behavior of
Thefixedfixedclass keeps an element in the same place on the screen, even when scrolling.Step 2: Compare with other position classes
relativemoves the element relative to its normal spot,absolutepositions relative to nearest positioned parent, andstaticis default with no special positioning.Final Answer:
fixed-> Option AQuick Check:
Fixed position =fixed[OK]
- Confusing
absolutewithfixed - Thinking
relativekeeps element fixed - Assuming
staticaffects position
absolute positioning align relative to it?Solution
Step 1: Understand absolute positioning context
An element withabsolutepositioning is placed relative to the nearest ancestor that has a position other thanstatic.Step 2: Identify which position sets the context
Setting the parent torelativemakes it the reference for the child'sabsoluteposition.Final Answer:
relative-> Option DQuick Check:
Parent must be relative for child's absolute [OK]
relative for child's absolute to work [OK]- Using
fixedon parent instead ofrelative - Not setting any position on parent
- Using
absoluteon parent mistakenly
absolute top-0 left-0 to a child inside a parent with no position class in Tailwind?Solution
Step 1: Check parent's position
The parent has no position class, so it defaults tostatic.Step 2: Understand absolute positioning without positioned parent
When no positioned parent exists,absolutepositions relative to the page (viewport).Final Answer:
The child will be positioned at the top-left of the page (viewport). -> Option BQuick Check:
Absolute without positioned parent = viewport position [OK]
- Assuming child positions relative to parent without position
- Thinking child stays in normal flow
- Believing child gets hidden
absolute bottom-0 right-0 to it, but it doesn't stay fixed. What is the fix?Solution
Step 1: Understand difference between absolute and fixed
absolutepositions relative to nearest positioned parent and scrolls with page;fixedstays visible on screen during scroll.Step 2: Identify correct class for fixed position
To keep the button visible at bottom right while scrolling, usefixedinstead ofabsolute.Final Answer:
Changeabsolutetofixedon the button. -> Option CQuick Check:
Fixed = stays visible on scroll [OK]
fixed for always visible elements [OK]- Using
absoluteexpecting fixed behavior - Adding
relativeto parent instead of fixing button - Removing position classes without replacing
div with relative class and inside it a span with absolute top-0 right-0. You want the span to stay fixed at the top right of the viewport instead. How do you change the classes?Solution
Step 1: Understand current setup
Thedivisrelative, so thespanwithabsolutepositions relative to it.Step 2: Change
To position thespanto fixed for viewport positioningspanrelative to the viewport (screen), it needsfixedinstead ofabsolute. The parent can remainrelative.Final Answer:
Changeabsoluteonspantofixedand keeprelativeondiv. -> Option AQuick Check:
Fixed on child = position relative to viewport [OK]
fixed on element to fix on viewport [OK]- Changing parent to fixed instead of child
- Keeping absolute on child expecting fixed behavior
- Adding fixed to both elements unnecessarily
