Bootstrap 5 Tutorial

v5.3.0

Bootstrap 5 Tutorial

Overflow Utilities in Bootstrap 5

Overflow Utilities: Control how content overflows its container with visible, hidden, scroll, and auto values.

Basic Overflow

Overflow Property Values
Visible (Default)
Content item 1
Content item 2
Content item 3
Content item 4
.overflow-visible
Hidden
Content item 1
Content item 2
Content item 3
Content item 4 (hidden)
.overflow-hidden
Scroll
Content item 1
Content item 2
Content item 3
Content item 4
Content item 5
.overflow-scroll
Auto
Content item 1
Content item 2
Content item 3
Content item 4
Content item 5 (scroll if needed)
.overflow-auto
<!-- Basic overflow utilities -->
<div class="overflow-visible">
  Content visible outside container (default)
</div>

<div class="overflow-hidden">
  Content clipped, no scrollbars
</div>

<div class="overflow-scroll">
  Always show scrollbars
</div>

<div class="overflow-auto">
  Show scrollbars only when needed
</div>

<!-- With fixed height -->
<div class="overflow-auto" style="height: 200px;">
  <p>Long content that may need scrolling...</p>
</div>

<!-- Inline elements -->
<span class="overflow-hidden d-inline-block" style="max-width: 100px;">
  Text that might overflow
</span>

Axis-Specific Overflow

Horizontal and Vertical Overflow
Horizontal Overflow
Wide item 1
Wide item 2
Wide item 3
Wide item 4
Wide item 5
.overflow-x-auto
Vertical Overflow
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
.overflow-y-auto
X Hidden, Y Auto
Very wide content that gets clipped horizontally
Vertical scroll available
More content
.overflow-x-hidden .overflow-y-auto
X Scroll, Y Hidden
Tab 1
Tab 2
Tab 3
Tab 4
Horizontal scroll only
.overflow-x-scroll .overflow-y-hidden
X and Y Auto
Wide content that may need horizontal scroll
Regular content
More content
Even more content
Final content item
.overflow-x-auto .overflow-y-auto
<!-- Axis-specific overflow -->
<div class="overflow-x-auto">
  Horizontal scrolling when needed
</div>

<div class="overflow-y-auto">
  Vertical scrolling when needed
</div>

<div class="overflow-x-hidden">
  Hide horizontal overflow
</div>

<div class="overflow-y-hidden">
  Hide vertical overflow
</div>

<div class="overflow-x-scroll">
  Always show horizontal scrollbar
</div>

<div class="overflow-y-scroll">
  Always show vertical scrollbar
</div>

<!-- Combined axis overflow -->
<div class="overflow-x-auto overflow-y-hidden">
  <!-- Horizontal auto, vertical hidden -->
</div>

<div class="overflow-x-hidden overflow-y-auto">
  <!-- Horizontal hidden, vertical auto -->
</div>

<div class="overflow-x-auto overflow-y-auto">
  <!-- Both axes auto -->
</div>

<!-- Practical examples -->
<div class="overflow-x-auto" style="max-width: 100%;">
  <table class="table">
    <!-- Wide table that scrolls horizontally -->
  </table>
</div>

<div class="overflow-y-auto" style="max-height: 300px;">
  <!-- Long content that scrolls vertically -->
  <ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <!-- More items -->
  </ul>
</div>

Text Overflow

Text Truncation and Wrapping
Text Truncation
This is a very long text that will be truncated with an ellipsis when it overflows the container width.
.text-truncate
Text Nowrap
This text will not wrap to the next line and will overflow horizontally.
.text-nowrap
Text Wrap
This text will wrap normally to fit within the container width.
.text-wrap
Text Break
ThisIsAVeryLongUnbrokenWordThatWillBreakAtAnyCharacterToPreventHorizontalOverflow
.text-break
Combined Example
Multiple classes combined for text overflow control
.overflow-hidden .text-truncate .text-nowrap
Table Cell Overflow
ProductDescriptionStatus
Premium Wireless Headphones with Noise Cancellation
High-quality wireless headphones featuring active noise cancellation, 30-hour battery life, and premium sound quality for the best audio experience.
In Stock
<!-- Text overflow utilities -->
<div class="text-truncate" style="max-width: 200px;">
  Long text truncated with ellipsis
</div>

<div class="text-nowrap">
  Text that won't wrap to next line
</div>

<div class="text-wrap">
  Text that wraps normally (default)
</div>

<div class="text-break">
  Long words will break to prevent overflow
</div>

<!-- Combined with overflow -->
<div class="overflow-hidden text-truncate">
  <!-- Hidden overflow with truncation -->
</div>

<!-- In table cells -->
<table class="table">
  <tr>
    <td class="text-truncate" style="max-width: 150px;">
      Long product name truncated
    </td>
    <td>
      <div class="overflow-hidden" style="max-height: 50px;">
        Long description with limited height
      </div>
    </td>
  </tr>
</table>

<!-- Practical examples -->
<!-- Email addresses -->
<div class="text-truncate" style="max-width: 200px;">
  very.long.email.address@companydomain.com
</div>

<!-- File paths -->
<div class="text-break">
  /users/username/projects/verylongprojectname/src/components/longcomponentname.js
</div>

<!-- URLs -->
<a href="#" class="text-truncate d-inline-block" style="max-width: 200px;">
  https://www.example.com/very/long/url/path/that/needs/truncation
</a>

Responsive Overflow

Responsive Overflow Auto
Item 1
Item 2
Item 3
Item 4
.overflow-auto .overflow-md-visible
Responsive Text Truncation
This text is truncated on mobile but wraps normally on tablet and larger screens for better readability on different devices.
.text-truncate .text-md-wrap
Mobile Scroll, Desktop Visible
Content
More content
Extra content
.overflow-auto .overflow-lg-visible
Responsive Horizontal Scroll
Tab 1
Tab 2
Tab 3
Tab 4
.overflow-x-auto .overflow-x-lg-hidden
Breakpoint Table
BreakpointClass PrefixExample
Alloverflow-.overflow-auto
≥576pxoverflow-sm-.overflow-sm-hidden
≥768pxoverflow-md-.overflow-md-scroll
≥992pxoverflow-lg-.overflow-lg-visible
≥1200pxoverflow-xl-.overflow-xl-auto
<!-- Responsive overflow -->
<div class="overflow-auto overflow-md-visible">
  <!-- Auto scroll on mobile, visible on tablet+ -->
</div>

<div class="overflow-hidden overflow-lg-scroll">
  <!-- Hidden on mobile, scroll on desktop -->
</div>

<!-- Axis-specific responsive overflow -->
<div class="overflow-x-auto overflow-x-lg-hidden">
  <!-- Horizontal auto on mobile, hidden on desktop -->
</div>

<div class="overflow-y-auto overflow-y-md-scroll">
  <!-- Vertical auto on mobile, scroll on tablet+ -->
</div>

<!-- Responsive text overflow -->
<div class="text-truncate text-md-wrap text-lg-nowrap">
  <!-- 
    Truncated on mobile
    Normal wrap on tablet
    No wrap on desktop
  -->
</div>

<div class="text-nowrap text-lg-wrap">
  <!-- No wrap on mobile, wrap on desktop -->
</div>

<!-- Complete responsive example -->
<div class="overflow-auto overflow-md-hidden overflow-lg-scroll" 
     style="height: 200px;">
  <!-- 
    Auto on mobile
    Hidden on tablet
    Scroll on desktop
  -->
</div>

<!-- Table with responsive overflow -->
<div class="table-responsive overflow-auto overflow-md-visible">
  <table class="table">
    <!-- Scrollable on mobile, visible on tablet+ -->
  </table>
</div>

Scrollbar Styling

Custom Scrollbar Behavior
Custom Scrollbar CSS
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Hide Scrollbar (CSS)
Content without visible scrollbar
Scroll with mouse wheel
Or keyboard navigation
Scrollbar is hidden
But still functional
More content here
Scrollbar Utilities Table
Utility ClassCSS EquivalentBrowser SupportUse Case
.overflow-autooverflow: autoAll browsersScroll when needed
.overflow-scrolloverflow: scrollAll browsersAlways show scrollbars
.overflow-hiddenoverflow: hiddenAll browsersClip overflow content
.text-truncatetext-overflow: ellipsisAll browsersText truncation
.text-nowrapwhite-space: nowrapAll browsersPrevent text wrapping
<!-- Custom scrollbar CSS (not Bootstrap utility) -->
<style>
  .custom-scrollbar::-webkit-scrollbar {
    width: 12px;
  }
  
  .custom-scrollbar::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 10px;
  }
  
  .custom-scrollbar::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 10px;
  }
  
  .custom-scrollbar::-webkit-scrollbar-thumb:hover {
    background: #555;
  }
  
  /* Hide scrollbar but keep functionality */
  .hide-scrollbar::-webkit-scrollbar {
    display: none;
  }
  
  .hide-scrollbar {
    -ms-overflow-style: none;  /* IE and Edge */
    scrollbar-width: none;     /* Firefox */
  }
</style>

<!-- Usage -->
<div class="overflow-auto custom-scrollbar" style="height: 200px;">
  <!-- Custom styled scrollbar -->
</div>

<div class="overflow-auto hide-scrollbar" style="height: 200px;">
  <!-- Hidden scrollbar but still scrollable -->
</div>

<!-- Bootstrap-only approach -->
<div class="overflow-auto" style="height: 200px; scrollbar-width: thin;">
  <!-- Native scrollbar styling -->
</div>

<!-- Accessibility consideration -->
<div class="overflow-auto" 
     style="height: 200px;"
     tabindex="0"
     aria-label="Scrollable content area">
  <!-- Scrollable content with keyboard accessibility -->
</div>

Practical Examples

Scrollable Content Area
Chat Messages
User
John: Hello! How are you doing today?
10:30 AM
You
You: I'm doing great! Working on some new projects.
10:32 AM
User
User 1: Message 1 in the conversation...
10:33 AM
User
User 2: Message 2 in the conversation...
10:34 AM
User
User 3: Message 3 in the conversation...
10:35 AM
User
User 4: Message 4 in the conversation...
10:36 AM
User
User 5: Message 5 in the conversation...
10:37 AM
User
User 6: Message 6 in the conversation...
10:38 AM
User
User 7: Message 7 in the conversation...
10:39 AM
User
User 8: Message 8 in the conversation...
10:40 AM
<!-- Chat interface -->
<div class="border rounded overflow-hidden">
  <!-- Header -->
  <div class="bg-light p-3 border-bottom">
    <h5 class="mb-0">Chat Title</h5>
  </div>
  
  <!-- Scrollable messages -->
  <div class="overflow-auto" style="height: 250px;">
    <div class="p-3">
      <!-- Message 1 -->
      <div class="d-flex mb-3">
        <img src="avatar.jpg" class="rounded-circle" alt="User">
        <div class="ms-3">
          <div class="bg-light p-3 rounded">
            <strong>Name:</strong> Message text
          </div>
          <small class="text-muted">Time</small>
        </div>
      </div>
      
      <!-- More messages... -->
    </div>
  </div>
  
  <!-- Input area -->
  <div class="border-top p-3">
    <div class="input-group">
      <input type="text" class="form-control" placeholder="Type...">
      <button class="btn btn-primary">Send</button>
    </div>
  </div>
</div>
Data Table with Overflow
IDProduct NameDescriptionCategoryPriceStatus
#1000
Premium Product with Very Long Name 1
Detailed product description that explains all features and benefits in depth for item number 1.
Electronics › Computers › Accessories
$99.00In Stock
#1001
Premium Product with Very Long Name 2
Detailed product description that explains all features and benefits in depth for item number 2.
Electronics › Computers › Accessories
$109.00Low Stock
#1002
Premium Product with Very Long Name 3
Detailed product description that explains all features and benefits in depth for item number 3.
Electronics › Computers › Accessories
$119.00Out of Stock
#1003
Premium Product with Very Long Name 4
Detailed product description that explains all features and benefits in depth for item number 4.
Electronics › Computers › Accessories
$129.00In Stock
#1004
Premium Product with Very Long Name 5
Detailed product description that explains all features and benefits in depth for item number 5.
Electronics › Computers › Accessories
$139.00Low Stock
#1005
Premium Product with Very Long Name 6
Detailed product description that explains all features and benefits in depth for item number 6.
Electronics › Computers › Accessories
$149.00Out of Stock
#1006
Premium Product with Very Long Name 7
Detailed product description that explains all features and benefits in depth for item number 7.
Electronics › Computers › Accessories
$159.00In Stock
#1007
Premium Product with Very Long Name 8
Detailed product description that explains all features and benefits in depth for item number 8.
Electronics › Computers › Accessories
$169.00Low Stock
#1008
Premium Product with Very Long Name 9
Detailed product description that explains all features and benefits in depth for item number 9.
Electronics › Computers › Accessories
$179.00Out of Stock
#1009
Premium Product with Very Long Name 10
Detailed product description that explains all features and benefits in depth for item number 10.
Electronics › Computers › Accessories
$189.00In Stock
#1010
Premium Product with Very Long Name 11
Detailed product description that explains all features and benefits in depth for item number 11.
Electronics › Computers › Accessories
$199.00Low Stock
#1011
Premium Product with Very Long Name 12
Detailed product description that explains all features and benefits in depth for item number 12.
Electronics › Computers › Accessories
$209.00Out of Stock
#1012
Premium Product with Very Long Name 13
Detailed product description that explains all features and benefits in depth for item number 13.
Electronics › Computers › Accessories
$219.00In Stock
#1013
Premium Product with Very Long Name 14
Detailed product description that explains all features and benefits in depth for item number 14.
Electronics › Computers › Accessories
$229.00Low Stock
#1014
Premium Product with Very Long Name 15
Detailed product description that explains all features and benefits in depth for item number 15.
Electronics › Computers › Accessories
$239.00Out of Stock
Showing 15 of 100 products
<!-- Scrollable table -->
<div class="table-responsive overflow-auto" style="max-height: 300px;">
  <table class="table">
    <!-- Sticky header -->
    <thead class="sticky-top bg-light">
      <tr>
        <th>Column 1</th>
        <th>Column 2</th>
      </tr>
    </thead>
    
    <tbody>
      <!-- Row with truncated cells -->
      <tr>
        <td>
          <div class="text-truncate" style="max-width: 150px;">
            Long text truncated
          </div>
        </td>
        <td>
          <div class="overflow-hidden" style="max-height: 50px;">
            Content with height limit
          </div>
        </td>
        <td class="text-nowrap">
          Non-wrapping content
        </td>
        <td>
          <span class="badge bg-success">Status</span>
        </td>
      </tr>
      
      <!-- More rows... -->
    </tbody>
  </table>
</div>

<!-- Pagination -->
<div class="d-flex justify-content-between align-items-center">
  <div class="text-muted">
    Showing X of Y items
  </div>
  <div class="d-flex gap-2">
    <button class="btn btn-sm btn-outline-primary">Previous</button>
    <button class="btn btn-sm btn-primary">Next</button>
  </div>
</div>

Best Practices

Overflow Utility Guidelines
  • ✅ Use .overflow-auto for scrollable areas (better UX than always visible scrollbars)
  • ✅ Combine with fixed heights for controlled scrollable areas
  • ✅ Use .text-truncate for long text in tables and cards
  • ✅ Employ .text-nowrap for codes, IDs, and other non-wrapping content
  • ✅ Use .text-break for long unbreakable strings (URLs, file paths)
  • ✅ Implement responsive overflow for different screen sizes
  • ✅ Always test overflow behavior on mobile devices
  • ✅ Consider accessibility - ensure keyboard navigation works in scrollable areas

Accessibility Considerations

Accessibility Best Practices
  • Keyboard navigation: Ensure scrollable areas are focusable with tabindex=&qout;0&qout;
  • Screen readers: Use aria-label or aria-labelledby for scrollable regions
  • Focus indicators: Maintain visible focus styles in scrollable areas
  • Truncated text: Provide tooltips or expandable sections for full content
  • Scrollbar visibility: Ensure sufficient contrast for scrollbars
  • Mobile touch: Test touch scrolling on mobile devices
  • Performance: Avoid excessive nested scrollable areas