How to handle long words and text overflow with CSS?
使用overflow-wrap: break-word可安全折行长单词,仅在必要时断词;word-break: break-all强制任意位置断字,适用于技术场景;text-overflow: ellipsis需配合white-space: nowrap和overflow: hidden实现省略。

How to break long words with word-break
When a single word (like a URL or hex string) is longer than its container, it overflows instead of wrapping — even with white-space: normal. The most direct fix is word-break: break-all, which forces breaks *anywhere* in the word, including mid-letter. Use it only when you’re sure breaking arbitrary characters is acceptable — e.g., log displays or technical IDs.
But beware: word-break: break-all doesn’t respect syllables or grapheme boundaries, and it’ll split words like "hello" into "hel" "lo" if needed. For languages like Chinese or Japanese, prefer word-break: break-word (deprecated alias) or better yet, overflow-wrap: break-word.
-
word-break: normal→ respects word boundaries, no forced breaks -
word-break: break-all→ breaks anywhere, even within ASCII letters -
word-break: keep-all→ prevents breaks in CJK text (useful for Chinese/Japanese)
When overflow-wrap is safer than word-break
overflow-wrap (formerly word-wrap) only breaks a line *if there’s no other option* — i.e., when an unbreakable word would overflow. It tries normal wrapping first, then inserts a break *between words* or, as fallback, *inside* a long word. That makes it more predictable for mixed-content paragraphs.
This property works well with hyphens: auto for English, but note: hyphens requires lang attribute and browser support varies (Safari needs -webkit-hyphens).
- Set
overflow-wrap: break-wordon the container (not inherited by default) - Avoid combining it with
word-break: break-all— they compete and behavior becomes inconsistent - Doesn’t affect text in
preortextareaunless explicitly styled
Prevent overflow without breaking words: text-overflow white-space
If your goal isn’t wrapping but *clipping* overflowing text (e.g., file names in a narrow table cell), use text-overflow: ellipsis. But it only works under three strict conditions: the element must have white-space: nowrap, overflow: hidden, and a fixed width (or max-width).
For multi-line ellipsis, CSS still lacks a clean native solution. You’ll need display: -webkit-box with -webkit-line-clamp, but that’s WebKit-only and unreliable for dynamic content.
-
text-overflow: cliphides overflow silently;ellipsisadds"…" -
white-space: nowrapdisables line breaks entirely — don’t use it if wrapping is desired - Flex or grid containers may need
min-width: 0on children to allowoverflow: hiddento take effect
Real-world example: A responsive code snippet display
Here’s how to safely render a long base64 string or CLI command in a narrow card without breaking layout or readability:
code {
display: block;
max-width: 100%;
overflow-x: auto;
white-space: pre-wrap;
word-break: break-all;
overflow-wrap: break-word;
}
Note: pre-wrap preserves intentional line breaks and spaces while allowing wrapping. overflow-x: auto ensures horizontal scrolling remains available if wrapping fails — a practical fallback. Don’t rely solely on break-all for user-facing prose; test with real data, especially non-Latin scripts or emoji sequences, where grapheme clusters can break unexpectedly.
The above is the detailed content of How to handle long words and text overflow with CSS?. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20521
7
13634
4
CSS mobile performance optimization_Use will-change to inform transition attributes in advance
Mar 12, 2026 am 11:15 AM
Will-change should only be declared for transform and opacity attributes that will change frequently and can trigger synthesis; avoid abusing invalid attributes such as all, left, top, etc., which must be added/removed dynamically, and be used with caution in scrolling containers. Mobile terminals need to take into account compatibility and memory limitations.
How to make a simple fixed bottom toolbar with CSS_Set bottom:0 through position:fixed
Mar 10, 2026 pm 02:12 PM
The main reason why bottom:0 does not take effect is that the ancestor element triggers transform/will-change/filter to create a new containing block, so that the fixed element is positioned relative to it rather than the viewport; dynamic changes in the iOS Safari address bar cause occlusion; fixed elements need to be given way with padding-bottom; z-index failure is often caused by the parent creating a cascading context.
How to avoid content overflow when using CSS with float_Set the box-sizing of the box model and ensure that the sum of the percentages does not exceed 100%
Mar 12, 2026 pm 12:00 PM
If the floating element cannot open the parent container, BFC needs to be triggered. Overflow:hidden is commonly used; box-sizing:border-box must be set on the floating element itself; the percentage exceeds 100% due to whitespace characters, border/padding and pixel rounding; in IE, pay attention to the box-sizing prefix and margin parsing bugs.
How to set a transparent frosted sidebar with CSS_using rgba color through background-filter
Apr 03, 2026 pm 03:57 PM
In Safari, the background-filter needs to be prefixed with -webkit- and the background is opaque to take effect; the blur value cannot be 0; the size of the blurred area affects performance more than the numerical value; text needs to be enhanced with contrast and strokes to ensure readability.
How to adjust the picture display ratio in CSS_define the object-fit attribute in style
Mar 13, 2026 pm 01:09 PM
object-fit:cover is the most common choice - it maintains the aspect ratio, crops excess parts, and ensures the container is completely filled.
How to implement navigation signs with floating dynamic effects in CSS_using relative positioning and transition
Mar 12, 2026 am 11:51 AM
The main reason why CSShover animation is not smooth is that the transition is not written in the default state or acts on non-animable properties; the transition should be placed in a non-hover style, and transform should be used instead of left/relative. Pay attention to mobile hover invalidation and browser compatibility issues.
How does CSS exclude specific types of elements_Use the not pseudo-class selector to filter css
Mar 10, 2026 pm 02:57 PM
:not() only supports a single simple selector, disabling composite writing methods such as descendants/descendants; to exclude multiple conditions, you need to write repeatedly, such as: not(.a):not([b]); when mixed with :nth-child, it will still match the original sequence number, and will be rearranged after non-filtering.
How to make a simple jitter tip with CSS_using keyframes and rotation
Apr 03, 2026 pm 03:42 PM
Shake animation should use translateX() to achieve left and right displacement instead of rotate(); it needs to be combined with @keyframes to define a 0%/25%/50%/75%/100% offset sequence, and the offset is controlled within ±2px~±6px; animation-fill-m must be added ode:forwards (or use both), select cubic-bezier(.36,.07,.19,.97) for easing; the old animation must be cleared and forced to rearrange before JS is triggered; use will-change with caution on the mobile terminal, and only add translateZ(0) to ensure hardware acceleration.





