In web design, there are times when you need to keep text on a single line — such as in buttons, navigation links, or labels. By default, text will wrap to the next line if there’s not enough space. But with a few lines of CSS, you can easily prevent unwanted line breaks.
In this guide, you’ll learn how to prevent line breaks in CSS using simple properties like white-space
and nowrap
— with practical examples and best use cases.
✅ Why Prevent Line Breaks?
Preventing line breaks is useful when you want to:
- Keep navigation items or tabs in one line
- Ensure a label or button stays intact
- Prevent text from splitting awkwardly on mobile
- Maintain the layout of cards, tables, or headers
🛠️ Solution: Use white-space: nowrap;
The most common and reliable way to prevent a line break is:
.no-break {
white-space: nowrap;
}
📌 Explanation:
white-space
controls how white space and line breaks are handled.nowrap
tells the browser not to break lines, even if the content overflows.
🧪 Example: Keep Button Text in One Line
<style>
.no-break-button {
white-space: nowrap;
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
font-size: 16px;
}
</style>
<button class="no-break-button">Do Not Break Text</button>
✅ Even if the button shrinks, the text stays on one line (though it may overflow).
🔒 Prevent Breaks in Headings or Labels
h1, label {
white-space: nowrap;
}
This is especially helpful when you want a title or label to remain in a single line across devices.
🔄 Combine with Overflow Handling
If the container is smaller than the content, it might overflow. Combine white-space
with:
overflow: hidden;
text-overflow: ellipsis;
Example:
.single-line {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
Result:
Long text will stay on one line and get truncated with ...
if it overflows.
📏 Use Case: Table Cell Without Line Break
td.no-break {
white-space: nowrap;
}
This ensures table content like price, date, or code doesn’t wrap unexpectedly.
💡 Tip: Use Responsibly
While white-space: nowrap
is powerful, overuse can cause layout issues, especially on smaller screens. Always test for:
- Overflowing text
- Hidden content
- Broken responsiveness
🔚 Conclusion
Controlling line breaks with CSS gives you precision over your layout. Whether you’re styling buttons, headings, or table cells, white-space: nowrap
is the key to keeping content on a single line.
📌 Quick Recap
Goal | CSS Code |
---|---|
Prevent all line breaks | white-space: nowrap; |
Prevent and handle overflow | white-space: nowrap; overflow: hidden; text-overflow: ellipsis; |
For specific elements | Apply .no-break class |