Text wrapping and overflow help keep your webpage neat by controlling how long text fits inside boxes. This stops text from spilling out or breaking your design.
Text wrapping and overflow in Bootsrap
Use Bootstrap utility classes: .text-wrap - allows text to wrap normally .text-nowrap - prevents text from wrapping .text-truncate - truncates text with an ellipsis if too long Example: <div class="text-wrap">Long text here</div>
These classes work by adding CSS rules to control wrapping and overflow.
Use .text-truncate inside a container with a fixed width to see the ellipsis effect.
<div class="text-wrap" style="width: 150px; border: 1px solid #ccc;"> This is a very long text that will wrap inside the box. </div>
<div class="text-nowrap" style="width: 150px; border: 1px solid #ccc;"> This is a very long text that will not wrap and may overflow. </div>
<div class="text-truncate" style="width: 150px; border: 1px solid #ccc; white-space: nowrap; overflow: hidden;"> This is a very long text that will be cut off with an ellipsis. </div>
This page shows three boxes demonstrating Bootstrap's text wrapping and overflow utilities. The first wraps text normally, the second prevents wrapping causing overflow, and the third truncates text with an ellipsis.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Bootstrap Text Wrapping and Overflow</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> <style> .box { width: 200px; border: 1px solid #666; margin-bottom: 1rem; padding: 0.5rem; } </style> </head> <body> <main class="container mt-4"> <h1>Text Wrapping and Overflow Examples</h1> <section> <h2>1. Text Wrap (default)</h2> <div class="box text-wrap"> This is a very long text that will wrap inside the box to keep everything neat and readable. </div> </section> <section> <h2>2. No Wrap</h2> <div class="box text-nowrap"> This is a very long text that will not wrap and might overflow outside the box. </div> </section> <section> <h2>3. Truncate with Ellipsis</h2> <div class="box text-truncate" style="white-space: nowrap; overflow: hidden;"> This is a very long text that will be cut off with an ellipsis if it does not fit. </div> </section> </main> </body> </html>
Always test your text on different screen sizes to ensure readability.
Use .text-truncate only inside containers with fixed width or max-width.
Combining white-space: nowrap; and overflow: hidden; is necessary for truncation to work.
Bootstrap provides simple classes to control text wrapping and overflow.
.text-wrap lets text wrap normally, .text-nowrap stops wrapping, and .text-truncate adds ellipsis for overflow.
These help keep your webpage clean and readable on all devices.
Practice
Solution
Step 1: Understand text wrapping classes in Bootstrap
Bootstrap provides.text-wrapto allow normal text wrapping inside containers.Step 2: Identify the class that enables wrapping
.text-nowrapprevents wrapping, and.text-truncatetruncates text with ellipsis. So,.text-wrapis the correct choice.Final Answer:
.text-wrap -> Option DQuick Check:
Text wrapping = .text-wrap [OK]
- Confusing .text-nowrap as wrapping text
- Thinking .text-truncate wraps text
- Assuming .text-break is for wrapping
Solution
Step 1: Recall Bootstrap classes for text wrapping control
.text-nowrapis designed to stop text from wrapping onto the next line.Step 2: Confirm the class that disables wrapping
.text-wrapallows wrapping,.text-truncatetruncates text, so.text-nowrapis the correct class to prevent wrapping.Final Answer:
.text-nowrap -> Option BQuick Check:
No wrap = .text-nowrap [OK]
- Using .text-wrap to stop wrapping
- Confusing .text-truncate with no wrap
- Assuming .text-break stops wrapping
<div class='text-truncate' style='width: 8rem;'>This is a very long text that should be cut off.</div>
Solution
Step 1: Understand .text-truncate behavior
The.text-truncateclass in Bootstrap truncates text that overflows its container and adds an ellipsis.Step 2: Analyze the style and effect
The container width is limited to 8rem, so the long text will not fit and will be cut off with an ellipsis on one line.Final Answer:
Text cut off with ellipsis within 8rem width -> Option AQuick Check:
.text-truncate = ellipsis cut off [OK]
- Expecting text to wrap with .text-truncate
- Thinking text overflows visibly without cut off
- Confusing .text-truncate with .text-nowrap
Solution
Step 1: Identify classes for no wrap and truncation
.text-nowrapstops wrapping, and.text-truncateadds ellipsis for overflow.Step 2: Combine classes correctly
Using.text-nowrap .text-truncatetogether prevents wrapping and truncates overflow with ellipsis.Final Answer:
.text-nowrap .text-truncate -> Option AQuick Check:
No wrap + ellipsis = .text-nowrap .text-truncate [OK]
- Using .text-wrap with .text-truncate (conflicting)
- Not combining classes, only using one
- Mixing order incorrectly
Solution
Step 1: Ensure no wrapping and truncation
Use.text-nowrapto prevent wrapping and.text-truncateto add ellipsis for overflow.Step 2: Set a max-width and add accessibility
Limit width withmax-width: 15rem;so truncation works. Addtitleattribute to show full text on hover for screen readers and keyboard users.Final Answer:
<h5 class='text-nowrap text-truncate' style='max-width: 15rem;' title='Long title'>Long title</h5> -> Option CQuick Check:
No wrap + ellipsis + accessibility =<h5 class='text-nowrap text-truncate' style='max-width: 15rem;' title='Long title'>Long title</h5>[OK]
- Not setting max-width so truncation fails
- Missing title attribute for accessibility
- Using .text-wrap which allows wrapping
Ready to prove it?
Pass the test and this pattern moves unknown
