Bootstrap 5 Tutorial

v5.3.0

Bootstrap 5 Tutorial

Position Utilities in Bootstrap 5

Position Utilities: Control the positioning of elements with static, relative, absolute, fixed, and sticky positioning.

Position Values

Basic Positioning
Static (Default)
Static positioned
Normal flow
.position-static
Relative
Relative with offset
Continues flow
.position-relative
Absolute
Top Left
Bottom Right
.position-absolute
Fixed
Fixed position

Fixed relative to viewport

.position-fixed
Sticky
Sticky Header
Content line 1
Content line 2
Content line 3
Content line 4
Content line 5
Content line 6
Sticky Footer
.position-sticky
<!-- Position values -->
<div class="position-static">Default static position</div>
<div class="position-relative">Relative to normal position</div>
<div class="position-absolute">Absolute within nearest positioned ancestor</div>
<div class="position-fixed">Fixed relative to viewport</div>
<div class="position-sticky">Sticky based on scroll position</div>

<!-- Sticky with top offset -->
<div class="position-sticky top-0">
  Sticks to top when scrolling
</div>

<div class="position-sticky bottom-0">
  Sticks to bottom when scrolling
</div>

<!-- Fixed with positioning -->
<div class="position-fixed top-0 end-0">
  Fixed to top right corner
</div>

<!-- Absolute within relative container -->
<div class="position-relative" style="height: 200px;">
  <div class="position-absolute top-50 start-50">
    Centered absolutely
  </div>
</div>

Placement Utilities

Top, Right, Bottom, Left Placement
.top-0 .start-0
.top-0 .start-50
.top-0 .end-0
.top-50 .start-0
.top-50 .start-50
.top-50 .end-0
.bottom-0 .start-0
.bottom-0 .start-50
.bottom-0 .end-0
Individual Placement
.top-0
.end-0
.bottom-0
.start-0
Percentage Values
.top-25 .start-25
.top-75 .end-75
<!-- Placement utilities -->
<!-- Top, Right, Bottom, Left (0%) -->
<div class="top-0">top: 0</div>
<div class="end-0">right: 0 (RTL: left: 0)</div>
<div class="bottom-0">bottom: 0</div>
<div class="start-0">left: 0 (RTL: right: 0)</div>

<!-- 50% placement -->
<div class="top-50">top: 50%</div>
<div class="start-50">left: 50% (RTL: right: 50%)</div>

<!-- 100% placement -->
<div class="top-100">top: 100%</div>
<div class="start-100">left: 100% (RTL: right: 100%)</div>

<!-- Percentage values (25%, 50%, 75%, 100%) -->
<div class="top-25">top: 25%</div>
<div class="end-50">right: 50%</div>
<div class="bottom-75">bottom: 75%</div>
<div class="start-100">left: 100%</div>

<!-- Corner combinations -->
<div class="top-0 start-0">Top left corner</div>
<div class="top-0 end-0">Top right corner</div>
<div class="bottom-0 start-0">Bottom left corner</div>
<div class="bottom-0 end-0">Bottom right corner</div>

<!-- Center combinations -->
<div class="top-50 start-50 translate-middle">Absolute center</div>
<div class="top-0 start-50 translate-middle-x">Top center</div>
<div class="top-50 start-0 translate-middle-y">Left center</div>

<!-- Responsive placement -->
<div class="top-0 top-md-50">Top 0 on mobile, top 50% on tablet+</div>
<div class="start-0 start-lg-50">Left 0 on mobile, left 50% on desktop</div>

Translate Middle Utilities

Centering Elements
.translate-middle
Perfect center
Horizontal Center
.translate-middle-x
Vertical Center
.translate-middle-y
Responsive Centering
.translate-middle .translate-md-none
Centered on mobile only
Flex centering alternative
<!-- Translate middle utilities -->
<div class="translate-middle">
  transform: translate(-50%, -50%);
</div>

<div class="translate-middle-x">
  transform: translateX(-50%);
</div>

<div class="translate-middle-y">
  transform: translateY(-50%);
</div>

<!-- Perfect centering pattern -->
<div class="position-relative" style="height: 300px;">
  <div class="position-absolute top-50 start-50 translate-middle">
    Perfectly centered element
  </div>
</div>

<!-- Horizontal centering -->
<div class="position-relative">
  <div class="position-absolute start-50 translate-middle-x">
    Horizontally centered
  </div>
</div>

<!-- Vertical centering -->
<div class="position-relative">
  <div class="position-absolute top-50 translate-middle-y">
    Vertically centered
  </div>
</div>

<!-- Multiple elements centered -->
<div class="position-relative" style="height: 200px;">
  <div class="position-absolute top-0 start-50 translate-middle-x">
    Top centered
  </div>
  <div class="position-absolute top-50 start-50 translate-middle">
    Middle centered
  </div>
  <div class="position-absolute bottom-0 start-50 translate-middle-x">
    Bottom centered
  </div>
</div>

<!-- Responsive translate -->
<div class="translate-middle translate-md-none">
  <!-- Centered on mobile, normal on desktop -->
</div>

Fixed Top & Bottom

Fixed Position Helpers
.fixed-top - Fixed to top of viewport
Scrollable Content

This content is scrollable to demonstrate fixed positioning.

Content item 1
Content item 2
Content item 3
Content item 4
Content item 5
Content item 6
Content item 7
Content item 8
Content item 9
Content item 10
.fixed-bottom - Fixed to bottom of viewport
Sticky Top
.sticky-top - Sticks when scrolling
Content line 1
Content line 2
Content line 3
Content line 4
Content line 5
Content line 6
Content line 7
Content line 8
Sticky Bottom
Content line 1
Content line 2
Content line 3
Content line 4
Content line 5
Content line 6
Content line 7
Content line 8
.sticky-bottom - Sticks at bottom
<!-- Fixed top -->
<div class="fixed-top">
  Fixed to top of viewport
</div>

<!-- Fixed bottom -->
<div class="fixed-bottom">
  Fixed to bottom of viewport
</div>

<!-- Sticky top -->
<div class="sticky-top">
  Becomes fixed when scrolling past
</div>

<!-- Sticky bottom -->
<div class="sticky-bottom">
  Sticks to bottom when scrolling
</div>

<!-- Practical examples -->

<!-- Fixed header -->
<nav class="navbar fixed-top">
  <!-- Navigation stays at top -->
</nav>

<main class="container mt-5 pt-5">
  <!-- Content with top padding for fixed nav -->
</main>

<!-- Fixed footer -->
<footer class="fixed-bottom bg-dark text-white p-3">
  <!-- Always visible footer -->
</footer>

<!-- Sticky table headers -->
<div class="table-responsive" style="max-height: 300px;">
  <table class="table">
    <thead class="sticky-top bg-white">
      <tr>
        <th>Sticky Header</th>
        <th>Remains visible</th>
      </tr>
    </thead>
    <tbody>
      <!-- Table content -->
    </tbody>
  </table>
</div>

<!-- Multiple sticky elements -->
<div style="height: 400px; overflow-y: auto;">
  <div class="sticky-top bg-light p-2">Header 1</div>
  <div class="p-3">Content section 1</div>
  <div class="sticky-top bg-light p-2" style="top: 50px;">Header 2</div>
  <div class="p-3">Content section 2</div>
</div>

Responsive Positioning

Responsive Position
Responsive Translate
Breakpoint-based Positioning
BreakpointClass PrefixExample
Allposition-.position-absolute
≥576pxposition-sm-.position-sm-fixed
≥768pxposition-md-.position-md-sticky
≥992pxposition-lg-.position-lg-relative
≥1200pxposition-xl-.position-xl-absolute
Responsive Placement
.top-0 .start-0 .top-md-50 .start-md-50
<!-- Responsive position values -->
<div class="position-relative position-md-absolute position-lg-fixed">
  <!-- Relative on mobile, absolute on tablet, fixed on desktop -->
</div>

<div class="position-sticky position-md-static">
  <!-- Sticky on mobile, static on tablet+ -->
</div>

<!-- Responsive placement -->
<div class="top-0 top-md-50 top-lg-100">
  <!-- Top: 0 on mobile, 50% on tablet, 100% on desktop -->
</div>

<div class="start-0 start-md-50 start-lg-100">
  <!-- Left: 0 on mobile, 50% on tablet, 100% on desktop -->
</div>

<!-- Responsive translate -->
<div class="translate-middle translate-md-none">
  <!-- Centered on mobile, normal on tablet+ -->
</div>

<div class="translate-middle-x translate-lg-middle-y">
  <!-- Horizontal center on mobile, vertical center on desktop -->
</div>

<!-- Complete responsive example -->
<div class="position-relative position-md-absolute position-lg-fixed
            top-0 top-md-50 top-lg-100
            start-0 start-md-50 start-lg-0
            translate-middle translate-md-none translate-lg-middle">
  Complex responsive positioning
</div>

<!-- Responsive fixed/sticky -->
<nav class="navbar fixed-top fixed-md-static">
  <!-- Fixed on mobile, static on tablet+ -->
</nav>

<div class="sticky-top sticky-md-bottom">
  <!-- Sticky top on mobile, sticky bottom on tablet+ -->
</div>

Overlap & Layering

Creating Overlapping Elements
Content on top
Image Overlay
Placeholder
Image Caption

Overlay text positioned absolutely

Badge Overlap
Avatar3
<!-- Image with overlay -->
<div class="position-relative">
  <Image width={600} height={400} src="image.jpg" class="img-fluid" alt="">
  
  <!-- Dark gradient overlay -->
  <div class="position-absolute bottom-0 start-0 w-100 
              bg-gradient-dark text-white p-3">
    <h5>Image Title</h5>
    <p>Description text</p>
  </div>
</div>

<!-- Avatar with notification badge -->
<div class="position-relative d-inline-block">
  <Image width={600} height={400} src="avatar.jpg" class="rounded-circle" alt="User">
  
  <!-- Badge positioned outside -->
  <span class="position-absolute top-0 start-100 translate-middle
               badge rounded-pill bg-danger">
    3
  </span>
</div>

<!-- Button with badge -->
<button class="btn btn-primary position-relative">
  Notifications
  <span class="position-absolute top-0 start-100 translate-middle
               badge rounded-pill bg-danger">
    5
    <span class="visually-hidden">unread messages</span>
  </span>
</button>

<!-- Overlapping cards -->
<div class="position-relative">
  <div class="card position-absolute top-0 start-0 w-75">
    <!-- Background card -->
  </div>
  <div class="card position-absolute top-25 start-25 w-75 z-1">
    <!-- Foreground card -->
  </div>
</div>

<!-- Gradient overlay -->
<div class="position-relative">
  <div class="bg-image"></div>
  <div class="position-absolute top-0 start-0 w-100 h-100
              bg-gradient-primary bg-opacity-75">
    <!-- Gradient overlay -->
  </div>
  <div class="position-relative z-1 text-white">
    <!-- Content on top -->
  </div>
</div>

Practical Examples

Hero Section with Overlay

Hero Section

With positioned overlay elements

1
Step One

Get started with our platform

2
Step Two

Configure your settings

3
Step Three

Launch your project

<!-- Hero section -->
<div class="position-relative">
  <!-- Hero content -->
  <div class="bg-primary text-white p-5">
    <div class="position-relative z-1">
      <h1>Title</h1>
      <p>Description</p>
    </div>
  </div>
  
  <!-- Centered indicator -->
  <div class="position-absolute top-100 start-50 translate-middle">
    <div class="bg-white shadow rounded-circle">
      Indicator
    </div>
  </div>
  
  <!-- Cards with numbered badges -->
  <div class="row position-relative" style="margin-top: 40px;">
    <div class="col-md-4">
      <div class="card position-relative">
        <div class="position-absolute top-0 start-50 translate-middle">
          <span class="bg-primary text-white rounded-circle">1</span>
        </div>
        <div class="card-body pt-5">
          <h5>Step 1</h5>
          <p>Description</p>
        </div>
      </div>
    </div>
  </div>
</div>
Navigation & UI Elements
UserOnline
User Name

Online now

75% Complete
<!-- Brand with badge -->
<a class="navbar-brand position-relative">
  Brand
  <span class="position-absolute top-0 start-100 translate-middle
               badge rounded-pill bg-danger">
    New
  </span>
</a>

<!-- Button with notification badge -->
<button class="btn btn-outline-primary position-relative">
  <i class="bi bi-bell"></i>
  <span class="position-absolute top-0 start-100 translate-middle
               badge rounded-pill bg-danger">
    3
  </span>
</button>

<!-- Avatar with status indicator -->
<div class="position-relative">
  <Image width={600} height={400} src="avatar.jpg" class="rounded-circle" alt="User">
  <span class="position-absolute bottom-0 end-0 translate-middle
               bg-success border border-white rounded-circle">
    <!-- Online indicator -->
  </span>
</div>

<!-- Progress bar with centered text -->
<div class="progress position-relative">
  <div class="progress-bar" style="width: 75%">
    <span class="position-absolute top-50 start-50 translate-middle
                 text-white fw-bold">
      75% Complete
    </span>
  </div>
</div>

Best Practices

Position Utility Guidelines
  • ✅ Use .position-relative as container for absolute positioned children
  • ✅ Always set z-index for overlapping positioned elements
  • ✅ Use .fixed-top and .fixed-bottom for persistent navigation
  • ✅ Employ .sticky-top for table headers and section titles
  • ✅ Combine with translate utilities for perfect centering
  • ✅ Test positioned elements on all screen sizes
  • ✅ Consider mobile behavior for fixed/sticky elements
  • ✅ Use semantic HTML with positioning for accessibility

Common Patterns

PatternClassesResultUse Case
Perfect Center.position-absolute .top-50 .start-50 .translate-middleElement centered in containerLoading spinners, modals
Fixed Header.fixed-top .z-3Header stays at topNavigation bars
Sticky Table Header.sticky-top .bg-whiteHeader sticks when scrollingData tables
Badge Overlap.position-absolute .top-0 .start-100 .translate-middleBadge positioned outside elementNotification counters
Image Overlay.position-absolute .bottom-0 .start-0 .w-100Text overlay on imageHero sections, cards
Progress Label.position-absolute .top-50 .start-50 .translate-middleText centered in progress barProgress indicators