Explain Codes LogoExplain Codes Logo

How to show multiline text in a table cell

html
responsive-design
css-mastery
multiline-text
Anton ShumikhinbyAnton Shumikhin·Feb 21, 2025
TLDR

To display multiline text in HTML table cells, use the <br> tag or CSS with the white-space: pre-line property.

A quick example with <br>:

<td>Line 1<br>Line 2<br>Line 3</td>

A quick example with CSS:

<style>.multiline { white-space: pre-line; }</style> <td class="multiline">Line 1 Line 2 Line 3</td>

Use <br> for precise line breaks or CSS for automatic preservation of line breaks.

Controlling white space: CSS Mastery

Understanding how to handle white space is pivotal in tailoring text formatting in an HTML table. The white-space CSS property allows us to do just that.

Different values of white-space control how white spaces behave:

  • normal — Collapses white space and fits lines within the content box (default behavior).
  • nowrap — Collapses white space but prevents any line breaks.
  • pre — Maintains white space and line breaks authentically like in preformatted text.
  • pre-wrap — Maintains white space, allows line breaks, and keeps everything wrapped within the content box.
  • pre-line — Collapses white space but maintains line breaks.

These values let you select and style multiline text accurately within HTML table cells.

Getting fancy: Multiline options

When we talk about multiline displays in HTML table cells, we have a few options:

  1. <br> Element: Easy and efficient. Precisely insert line breaks where necessary.
  2. CSS white-space: Style all the things! white-space gives you more comprehensive control over text formatting.
  3. <pre> Element: Keep it authentic. Ideal for displaying code or poetry where the original formatting needs to be upheld.
  4. Replacing newline characters: For server-side masters. Replace '\n' with <br /> to manage newline characters.
  5. Nesting block elements: Go advanced with nested <div> or <p> within td for better design and control.

Details matter, but knowing your options, you can decide what fits best!

How to handle: Long lines and overflow

When we talk about multiline text, we often find long lines that can cause havoc in a perfectly styled table cell. We can't fit an elephant 🐘 into a suitcase 🧳, can we?

But we can help the elephant jump over long lines by using trampolines (well, CSS actually!)

  • Combine pre-wrap and word-wrap: break-word for the times you want words to break nicely to the next line.
  • Use overflow with scroll or auto if scrollbars are your thing 🕹️.
td.multiline { white-space: pre-wrap; word-wrap: break-word; } /* Here be dragons 🐉 ! No overflow issues m'lord */

Avoiding global CSS epidemics 🦠

When you look at CSS styling, be very careful. Like a pandemic, CSS can spread uncontrollably across all elements unless handled with care:

/* We're getting specific here, only affect multiclasses */ td.multiline { white-space: pre-wrap; word-wrap: break-word; } /* Don't catch the global epidemic! 🌍 */

Responsive design: Best practices

In the age of responsive design, multiline text in a table cell might look great on a desktop but downright weird on a mobile. Well, Rory said, "Winter is coming!" ⛄ and so are the CSS media queries!

@media screen and (max-width: 600px) { td.multiline { white-space: pre-wrap; /* Because, mobile screens! 📱 */ } }

Make sure your text adapts and looks great everywhere.

Crafting with semantics

HTML is more than just appearance. The right tags (<p>, <div>, <span>) within td can provide context and meaning. Oh, and while you're at it, ARIA roles and attributes can be handy for accessibility.

Diving into database content

When your content comes from a database, replacing newlines with HTML line breaks is no more problematic than watching a cat video 🐱. Here's a quick trick with PHP:

<?php echo '<td>' . nl2br(htmlentities($database_content)) . '</td>'; ?> /* Voila! Cats 🐱 have 9 lives, and developers have many tricks! */

The nl2br() function can be your best friend, turning newlines to <br>, while htmlentities() keeps you safe from HTML injection.