Lines typically break when text reaches the parent container's edge. This is standard paragraph behavior. However, this relies on spaces or whitespace between words. CSS offers several properties influencing line breaks, but let's focus on a scenario where expected breaks fail.
What happens when the browser lacks clear break indicators?
Let's create a "tag list" example and then troubleshoot the line-breaking problem. Here's the initial HTML:
- PHP
- JavaScript
- Rust
We'll horizontally arrange list items using CSS:
ul { margin: 0; padding: 0; list-style: none; } li { display: inline; padding-right: 5px; }
The list appears as desired. Now, let's wrap the list in a div
with a fixed width:
<div class="tags"> <ul> <li>PHP</li> <li>JavaScript</li> <li>Rust</li> </ul> </div>
.tags { width: 200px; }
Here's the issue: HTML minification removes whitespace, resulting in:
<div><ul> <li>PHP</li> <li>JavaScript</li> <li>Rust</li> </ul></div>
The line no longer breaks at 200px. Why? The browser treats the entire list as a single, unbroken word due to the absence of separating characters. Padding doesn't solve this; the entire list remains a single entity.
Forcing Line Breaks with Special Characters
Besides spaces (excluding non-breaking spaces), several characters trigger line breaks:
- Hyphens (-)
- En dashes (–)
- Em dashes (—)
- Question marks (?)
- Zero-width spaces (U 200B or )
These breaks occur during rendering; the browser still initially sees the text as a single unit. Adding a list item with one of these characters forces a break. Let's add "Objective-C":
<div> <ul> <li>PHP</li> <li>JavaScript</li> <li>Rust</li> <li>Objective-C</li> </ul> </div>
Here are three solutions:
Solution 1: Incorporate Line-Breaking Characters
Manually add spaces or other line-breaking characters. However, minifiers might remove these.
<div> <ul> <li>PHP </li> <li>JavaScript </li> <li>Rust </li> <li>Objective-C </li> </ul> </div>
Solution 2: Utilize Pseudo-elements
Add line-breaking characters using CSS ::before
and ::after
pseudo-elements. Minifiers won't remove this CSS-added whitespace. Note that whitespace collapsing can affect this approach. Using white-space: pre;
might be necessary.
Solution 3: Employ inline-block
Using display: inline-block;
on list items forces breaks, similar to zero-width spaces, but without visual separation.
Solution 4: Leverage Flexbox
Using display: flex;
or display: inline-flex;
on the <ul></ul>
allows for flexible line wrapping.
While this issue is less common, being aware of it and its solutions is valuable, especially when using minifiers or build processes that modify whitespace. Many modern tools mitigate this problem, but understanding the underlying cause remains important.
The above is the detailed content of When a Line Doesn't Break. For more information, please follow other related articles on the PHP Chinese website!

If you've ever had to display an interactive animation during a live talk or a class, then you may know that it's not always easy to interact with your slides

With Astro, we can generate most of our site during our build, but have a small bit of server-side code that can handle search functionality using something like Fuse.js. In this demo, we’ll use Fuse to search through a set of personal “bookmarks” th

I wanted to implement a notification message in one of my projects, similar to what you’d see in Google Docs while a document is saving. In other words, a

Some months ago I was on Hacker News (as one does) and I ran across a (now deleted) article about not using if statements. If you’re new to this idea (like I

Since the early days of science fiction, we have fantasized about machines that talk to us. Today it is commonplace. Even so, the technology for making

I remember when Gutenberg was released into core, because I was at WordCamp US that day. A number of months have gone by now, so I imagine more and more of us

The idea behind most of web applications is to fetch data from the database and present it to the user in the best possible way. When we deal with data there

Let's do a little step-by-step of a situation where you can't quite do what seems to make sense, but you can still get it done with CSS trickery. In this


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor