Bootstrap Position
Position utilities let you control an element's CSS position property and place it precisely using edge classes. This lesson covers positioning contexts and common placement patterns.
Bootstrap Position Overview
Classes like position-relative, position-absolute, position-fixed, and position-sticky map directly to their CSS position values. Absolute and fixed positioning are typically paired with edge utilities—top-0, bottom-0, start-0, end-0—to anchor an element to a specific side of its positioning context.
The translate-middle utility, combined with top-50 and start-50, centers an absolutely positioned element exactly over the midpoint of its container, a pattern frequently used for notification badges, close buttons, and overlay icons.
| Concept | Description | Common Usage |
| position-relative | Establishes a positioning context for children | Wrapping elements that need absolute children |
| position-absolute | Removes element from normal flow, positions within nearest ancestor | Badges, overlays, close buttons |
| position-fixed | Positions relative to the viewport | Fixed headers, floating action buttons |
| position-sticky | Sticks within its scroll container once reached | Sticky sidebars, sticky table headers |
| Edge classes (top-0/50/100, start-0, etc.) | Anchors an element to a percentage position | Precise placement of absolutely positioned elements |
Bootstrap Position Example
<div class="position-relative" style="width: 200px; height: 120px;" >
<img src="card.jpg" class="w-100 h-100 object-fit-cover rounded" alt="Card image">
<span class="position-absolute top-0 end-0 badge bg-danger m-2">Sale</span>
</div>
<nav class="navbar position-fixed top-0 start-0 w-100 bg-white shadow-sm">
Fixed navigation bar
</nav>
<div class="position-sticky" style="top: 1rem;">
Sticky sidebar content
</div>
The card container uses position-relative to establish context for the absolutely positioned sale badge, anchored to the top-right corner with top-0 and end-0. The navbar uses position-fixed to stay pinned to the top of the viewport regardless of scroll, and the sidebar uses position-sticky combined with an inline top offset to stick in place once it reaches that position while scrolling.
Top 10 Bootstrap Position Examples
These are the position utility patterns you'll use for overlays, fixed elements, and sticky content.
| # | Example | Syntax |
| 1 | Relative positioning context | <div class="position-relative">...</div> |
| 2 | Absolute positioning | <div class="position-absolute top-0 end-0">...</div> |
| 3 | Fixed positioning | <div class="position-fixed bottom-0 end-0">...</div> |
| 4 | Sticky positioning | <div class="position-sticky" style="top: 0;">...</div> |
| 5 | Top-left anchored badge | <span class="position-absolute top-0 start-0">...</span> |
| 6 | Centered overlay icon | <i class="position-absolute top-50 start-50 translate-middle"></i> |
| 7 | Fixed bottom action button | <button class="btn btn-primary position-fixed bottom-0 end-0 m-3">Chat</button> |
| 8 | Full coverage overlay | <div class="position-absolute top-0 start-0 w-100 h-100 bg-dark opacity-50"></div> |
| 9 | Sticky table header row | <thead class="position-sticky" style="top: 0;">...</thead> |
| 10 | Static reset (rare) | <div class="position-static">...</div> |
Popular Real-World Bootstrap Position Examples
These positioning patterns are common for overlays, floating buttons, and sticky navigation.
| Scenario | Pattern |
| Sale badge on a product image | <span class="position-absolute top-0 end-0 badge bg-danger m-2">-20%</span> |
| Sticky page navigation sidebar | <div class="position-sticky" style="top: 1rem;">Sidebar</div> |
| Fixed floating action button | <button class="btn btn-primary rounded-circle position-fixed bottom-0 end-0 m-4">+</button> |
| Image overlay play button | <i class="bi bi-play-circle-fill position-absolute top-50 start-50 translate-middle fs-1"></i> |
| Fixed announcement bar at top | <div class="position-fixed top-0 start-0 w-100 bg-dark text-white text-center py-2">Sale ends tonight</div> |
| Dark overlay over a background image | <div class="position-absolute top-0 start-0 w-100 h-100 bg-dark opacity-25"></div> |
| Notification badge on an icon | <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger">3</span> |
| Sticky sub-navigation bar | <nav class="position-sticky bg-white" style="top: 0; z-index: 1020;">...</nav> |
Best Practices
- Always set position-relative on the parent before placing absolutely positioned children inside it.
- Use translate-middle together with top-50/start-50 for perfectly centered overlay elements.
- Set an explicit z-index when stacking multiple positioned elements to control layering intentionally.
- Use position-sticky for in-flow sticky behavior instead of position-fixed when the element should scroll normally until a point.
- Combine position-fixed carefully with page padding so fixed elements don't overlap page content.
- Test fixed and sticky elements across mobile browsers, since behavior can vary with address bar collapsing.
- Keep positioned overlays accessible—ensure they don't trap focus or hide content from screen readers unexpectedly.
Common Mistakes
- Forgetting position-relative on the parent, causing absolutely positioned children to anchor to the wrong ancestor.
- Not setting a z-index, causing positioned elements to stack in an unintended order.
- Using position-fixed for elements that should scroll with the page, hiding content behind them.
- Overlapping fixed headers with page content because no top padding was added to compensate.
- Relying on position-sticky without testing its scroll container context, where it can silently fail to stick.
- Placing critical actions only in a fixed floating button without keyboard-accessible alternatives.
Key Takeaways
- Position utilities map to CSS position values: relative, absolute, fixed, sticky, and static.
- Edge classes (top-0, end-0, etc.) anchor absolutely or fixed positioned elements precisely.
- translate-middle combined with 50% offsets centers an element over a specific point.
- position-sticky keeps an element in flow until it reaches a scroll threshold, then sticks.
- A positioning parent (position-relative) is required for predictable absolute positioning.
Pro Tip
When building notification badges on icons, always wrap the icon in a position-relative container first, then use position-absolute with top-0, start-100, and translate-middle on the badge for consistent, correctly anchored placement.