Word-wrap in an HTML table
To achieve text wrapping in table cells, you will want to apply the word-wrap: break-word; and word-break: break-all; properties in your CSS. This will assure that even the longest of words do not surpass the cell boundaries. Here's an example:
This and other nifty tips below will help ensure your cells are crowd-friendly-- holding their words in the ring.
Nailing the table layout
Code for unpredictability is like a soup sandwich! Here's how to stomp out unexpected behaviour with table-layout: fixed;:
This piece of magic 🎩🐰 secures column widths across the board, giving your layout the rigidity of grandma's fruitcake.
Cross-browser consistency with word-wrap
word-wrap is snowflake-sensitive. Here's how to brave the Snow Queen's browser world:
- To make Internet Explorer behave, complement
word-wrapwith the-ms-word-wrapproperty. - To override any stubborn
white-space:nowrap;, assert your dominance withwhite-space: normal;.
In extreme wrap-or-else situations, a global * { word-wrap: break-word; } could be used. But use this power sparingly, like hot sauce on a taco.
Navy SEAL Strategies for text wrapping
Here are some advanced strategies for when text in td decides to play hide and seek:
- Put the text inside a
divorspan, and setword-break: break-all;. Like an electric fence for your text. - Opt out of
overflow: hidden;- no one likes a party pooper that hides overflowing text. - Use dev tools to resolve style conflicts. It's like a Swiss Army knife ⚙️ for web developers.
From tables to divs: The layout evolution
Some tables act like divas, and when yours does, serve it with a div-based layout:
In the world of design, divs offer you the flexibility tables dream about.
Further reads to quench your curiosity
- CSS Text Module Level 3 - Word-wrap, straight from the horse mouth!
- overflow-wrap - CSS: Cascading Style Sheets | MDN - Overflow-wrap: Meaning, examples and usage.
- Handling Long Words and URLs (Forcing Breaks, Hyphenation, Ellipsis, etc) | CSS-Tricks - Practical guide for managing text wrapping-a pot of gold at the end of the rainbow.
- CSS word-wrap property - W3Schools on word-wrap: Tutorial and examples.
- HTML table basics - Learn web development | MDN - A gentle intro to the world of HTML tables.
- Stack Overflow Discussion on Word-Wrap in Tables - Community-driven Q&A on text wrapping in tables.
Was this article helpful?