Bird
Raised Fist0
Tailwindmarkup~10 mins

Position utilities (relative, absolute, fixed) in Tailwind - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to make the container positioned relative.

Tailwind
<div class="[1] bg-blue-200 p-4">
  <p>This box is relative.</p>
</div>
Drag options to blanks, or click blank then click option'
Astatic
Babsolute
Cfixed
Drelative
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'absolute' or 'fixed' instead of 'relative'.
Using 'static' which is the default and does not allow positioning.
2fill in blank
medium

Complete the code to position the box absolutely inside its relative parent.

Tailwind
<div class="relative bg-gray-100 p-6">
  <div class="[1] bg-red-400 p-2">
    Absolute box
  </div>
</div>
Drag options to blanks, or click blank then click option'
Aabsolute
Brelative
Cfixed
Dstatic
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'relative' on the child instead of 'absolute'.
Using 'fixed' which positions relative to the viewport.
3fill in blank
hard

Fix the error in the code to make the box fixed at the top right corner.

Tailwind
<div class="[1] top-0 right-0 bg-green-300 p-3">
  Fixed box
</div>
Drag options to blanks, or click blank then click option'
Aabsolute
Bstatic
Cfixed
Drelative
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'absolute' which positions relative to the nearest positioned ancestor.
Using 'relative' or 'static' which do not fix the element on screen.
4fill in blank
hard

Fill both blanks to position the child absolutely at the bottom left inside the relative parent.

Tailwind
<div class="[1] bg-yellow-100 p-5">
  <div class="[2] bottom-0 left-0 bg-yellow-400 p-2">
    Bottom left box
  </div>
</div>
Drag options to blanks, or click blank then click option'
Arelative
Babsolute
Cfixed
Dstatic
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fixed' on child which positions relative to viewport.
Not setting parent to relative, so child absolute positions relative to body.
5fill in blank
hard

Fill all three blanks to create a fixed header with relative container and absolute logo inside.

Tailwind
<header class="[1] bg-gray-800 text-white p-4">
  <div class="[2] max-w-7xl mx-auto">
    <div class="[3] top-0 left-0 p-2">
      Logo
    </div>
    <nav>
      Navigation
    </nav>
  </div>
</header>
Drag options to blanks, or click blank then click option'
Afixed
Brelative
Cabsolute
Dstatic
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'static' which does not position elements.
Mixing up relative and absolute classes.

Practice

(1/5)
1. Which Tailwind class makes an element stay fixed on the screen even when you scroll?
easy
A. fixed
B. relative
C. absolute
D. static

Solution

  1. Step 1: Understand the behavior of fixed

    The fixed class keeps an element in the same place on the screen, even when scrolling.
  2. Step 2: Compare with other position classes

    relative moves the element relative to its normal spot, absolute positions relative to nearest positioned parent, and static is default with no special positioning.
  3. Final Answer:

    fixed -> Option A
  4. Quick Check:

    Fixed position = fixed [OK]
Hint: Fixed means stays put on screen during scroll [OK]
Common Mistakes:
  • Confusing absolute with fixed
  • Thinking relative keeps element fixed
  • Assuming static affects position
2. Which Tailwind class should you add to a parent element to make its child with absolute positioning align relative to it?
easy
A. static
B. fixed
C. absolute
D. relative

Solution

  1. Step 1: Understand absolute positioning context

    An element with absolute positioning is placed relative to the nearest ancestor that has a position other than static.
  2. Step 2: Identify which position sets the context

    Setting the parent to relative makes it the reference for the child's absolute position.
  3. Final Answer:

    relative -> Option D
  4. Quick Check:

    Parent must be relative for child's absolute [OK]
Hint: Parent needs relative for child's absolute to work [OK]
Common Mistakes:
  • Using fixed on parent instead of relative
  • Not setting any position on parent
  • Using absolute on parent mistakenly
3. What will happen if you apply absolute top-0 left-0 to a child inside a parent with no position class in Tailwind?
medium
A. The child will be positioned at the top-left of the parent.
B. The child will be positioned at the top-left of the page (viewport).
C. The child will stay in its normal place without moving.
D. The child will be hidden because of missing parent position.

Solution

  1. Step 1: Check parent's position

    The parent has no position class, so it defaults to static.
  2. Step 2: Understand absolute positioning without positioned parent

    When no positioned parent exists, absolute positions relative to the page (viewport).
  3. Final Answer:

    The child will be positioned at the top-left of the page (viewport). -> Option B
  4. Quick Check:

    Absolute without positioned parent = viewport position [OK]
Hint: Absolute without relative parent = position relative to page [OK]
Common Mistakes:
  • Assuming child positions relative to parent without position
  • Thinking child stays in normal flow
  • Believing child gets hidden
4. You want a button to stay visible at the bottom right corner of the screen while scrolling. You add absolute bottom-0 right-0 to it, but it doesn't stay fixed. What is the fix?
medium
A. Remove all position classes from the button.
B. Add relative to the button's parent.
C. Change absolute to fixed on the button.
D. Add static to the button.

Solution

  1. Step 1: Understand difference between absolute and fixed

    absolute positions relative to nearest positioned parent and scrolls with page; fixed stays visible on screen during scroll.
  2. Step 2: Identify correct class for fixed position

    To keep the button visible at bottom right while scrolling, use fixed instead of absolute.
  3. Final Answer:

    Change absolute to fixed on the button. -> Option C
  4. Quick Check:

    Fixed = stays visible on scroll [OK]
Hint: Use fixed for always visible elements [OK]
Common Mistakes:
  • Using absolute expecting fixed behavior
  • Adding relative to parent instead of fixing button
  • Removing position classes without replacing
5. You have a 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?
hard
A. Change absolute on span to fixed and keep relative on div.
B. Change relative on div to fixed and keep absolute on span.
C. Remove all position classes from both elements.
D. Add fixed to both div and span.

Solution

  1. Step 1: Understand current setup

    The div is relative, so the span with absolute positions relative to it.
  2. Step 2: Change span to fixed for viewport positioning

    To position the span relative to the viewport (screen), it needs fixed instead of absolute. The parent can remain relative.
  3. Final Answer:

    Change absolute on span to fixed and keep relative on div. -> Option A
  4. Quick Check:

    Fixed on child = position relative to viewport [OK]
Hint: Use fixed on element to fix on viewport [OK]
Common Mistakes:
  • Changing parent to fixed instead of child
  • Keeping absolute on child expecting fixed behavior
  • Adding fixed to both elements unnecessarily