How to Control Line Breaks in HTML Tables When You Need Them to Occur at Specific Points?

Susan Sarandon
Release: 2024-10-27 20:33:02
Original
499 people have browsed it

How to Control Line Breaks in HTML Tables When You Need Them to Occur at Specific Points?

Preferred Line Breaks in HTML

Creating web content often involves displaying text within tables, where line breaks are necessary to prevent content from overflowing. However, there might be a preference for line breaks to occur at specific points.

Designating Preferred Break Points

In the example provided, a table cell contains a list of cereals separated by commas. The desired behavior is for line breaks to occur after the commas, before spaces. However, HTML does not natively provide a way to specify preferred break points.

Avoiding Non-Breaking Spaces

Inserting non-breaking spaces can force the line break to occur at a specific point, but it also increases the text width unconditionally. To avoid this, an alternative approach is needed.

Text-Wrap Property

CSS3 introduced the text-wrap property, which allows control over line breaks in text. However, attempts to use text-wrap: avoid proved unsuccessful in the example.

Inline Block Display

Instead, the display: inline-block property can be used in conjunction with a wrapper element. This effectively creates a "block" of text within the cell that can wrap independently of the surrounding text.

Example

To implement the desired behavior, the following CSS and HTML can be used:

<code class="css">span.avoidwrap { display:inline-block; }</code>
Copy after login
<code class="html"><td>
  <span class="avoidwrap">Honey Nut Cheerios,</span>
  <span class="avoidwrap">Wheat Chex,</span>
  <span class="avoidwrap">Grape-Nuts,</span>
  <span class="avoidwrap">Rice Krispies,</span>
  <span class="avoidwrap">Some random cereal with a very long name,</span>
  <span class="avoidwrap">Honey Bunches of Oats,</span>
  <span class="avoidwrap">Wheaties,</span>
  <span class="avoidwrap">Special K,</span>
  <span class="avoidwrap">Froot Loops,</span>
  <span class="avoidwrap">Apple Jacks</span>
</td></code>
Copy after login

Effect

By using the avoidwrap class, the cereal names are kept together and line breaks occur at the commas, as desired. If the line needs to be further wrapped, the cereal names will then break at the spaces.

The above is the detailed content of How to Control Line Breaks in HTML Tables When You Need Them to Occur at Specific Points?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!