Words separated by spaces wrap when they reach the edge of their parent box. If the next word does not fit, the browser moves it to the following line and repeats, which is the behaviour we rely on in ordinary paragraphs. CSS has five properties that can influence where and when a line breaks, but the more instructive case is one where the browser has no way to know a break is allowed at all.
Take a "tag list": the goal is to move the default vertical list into a horizontal row by making the list items display: inline.
<ul>
<li>PHP</li>
<li>JavaScript</li>
<li>Rust</li>
<!-- etc. -->
</ul>
ul {
margin: 0;
padding: 0;
list-style: none;
}
li {
display: inline;
padding-right: 5px;
}
The result matches the intended look. A small amount of space between items keeps the row from appearing crowded. Wrapping the list in an element with a class such as .tags and fixing that wrapper to a width of 200px sets up the boundary that should trigger wrapping — this is where line breaks are supposed to happen.
<div class="tags">
<ul>
<li>PHP</li>
<li>JavaScript</li>
<li>Rust</li>
</ul>
</div>
.tags {
width: 200px;
}
Minifiers are commonly used in build pipelines to shrink files, and some of them strip whitespace thought to be irrelevant to the output, including spaces, tabs, and line-break characters. Removing the newlines from the markup produces the following.
<div class="tags"><ul><li>PHP</li><li>JavaScript</li><li>Rust</li></ul></div>
The list stops breaking at the 200px boundary. This is not because the browser ignores whitespace; it cares only sometimes, and here it matters. During parsing, the list reads as one long word because nothing in the markup distinguishes one item from another. The browser in effect treats the whole list as a single word. Removing the padding from the list items changes nothing except the spacing between items — the wrapping behaviour stays the same.
Characters that create a natural break
Space is not the only character that permits a line break, and non-breaking spaces ( ) explicitly do not. Also allowed are:
- After a hyphen (
-) - After an en dash (
–) - Before and after an em dash (
—) - After a question mark (
?) - Zero-width white space (
U+200Bor​)
These breaks occur at render time, so the parser still regards the content as one long word. Adding a list item whose text contains one of these characters forces a break — an item named "Objective-C" works because of its hyphen.
<div class="tags">
<ul>
<li>PHP</li>
<li>JavaScript</li>
<li>Rust</li>
<li>Objective-C</li>
</ul>
</div>
Three fixes for a list that will not wrap
Along these lines, three approaches work.
Insert a breaking character
Forcing the break with one of the characters above is an option, but a minifier may strip the extra spaces contained in or following a closing tag, and not every minifier behaves the same way.
<div class="tags">
<ul>
<li>PHP </li>
<li>JavaScript </li>
<li>Rust </li>
<li>Objective-C </li>
</ul>
</div>
Add the character through pseudo-elements
The breaking character can also come from the ::before and ::after pseudo-elements. This is sturdier because the whitespace is inserted when CSS is applied, so an HTML minifier has no effect on it.
This raises collapsing whitespace. Inside inline elements, the browser collapses whitespace before and after a character that forces a line break. That enables a trick pairing ::after with the content property, whitespace, and display: inline-block: the inline-block element adds a breaking character at the end of the text, and the content space that follows is removed at render time — unless the white-space property is set to pre.
Switch to inline-block or flex
Applying display: inline-block forces the same kind of break, since the inline-block element already carries the whitespace needed. The effect resembles inserting a zero-width space, though the list items then have no visual separation. Alternatively, the unordered list can be made a flex container, using flex-flow to set direction and wrap items onto multiple lines as needed. Using display: inline-flex instead of inline-block gives the same idea with the entire flexible container displayed inline.
This kind of break failure is not common, and it is easy to miss, but minifiers and many libraries are sophisticated enough to try to prevent it. When it does surface, the workarounds are straightforward.



