Word-wrap in an HTML table

后端 未结 25 2896
温柔的废话
温柔的废话 2020-11-22 02:45

I\'ve been using word-wrap: break-word to wrap text in divs and spans. However, it doesn\'t seem to work in table cells. I have a tabl

相关标签:
25条回答
  • 2020-11-22 03:40

    Problem with

    <td style="word-break:break-all;">longtextwithoutspace</td>
    

    is that it will work not so good when text has some spaces, e.g.

    <td style="word-break:break-all;">long text with andthenlongerwithoutspaces</td>
    

    If word andthenlongerwithoutspaces fits into table cell in one line but long text with andthenlongerwithoutspaces does not, the long word will be broken in two, instead of being wrapped.

    Alternative solution: insert U+200B (ZWSP), U+00AD (soft hyphen) or U+200C (ZWNJ) in every long word after every, say, 20th character (however, see warning below):

    td {
      border: 1px solid;
    }
    <table style="width: 100%;">
      <tr>
        <td>
          <div style="word-wrap: break-word;">
            Looooooooooooooooooo&#xAD;oooooooooooooooooooo&#xAD;oooooooooooooooooooo&#xAD;oooooooooooooooooooo&#xAD;oooooooooooooooooooo&#xAD;oooooooooooooooooooo&#xAD;oooooooooooooooooooo&#xAD;oooooooooooooooooooo&#xAD;oooooooooooooooooooo&#xAD;oooooooooooooong word
          </div>
        </td>
        <td><span style="display: inline;">Short word</span></td>
      </tr>
    </table>

    Warning: inserting additional, zero-length characters does not affect reading. However, it does affect text copied into clipboard (these characters are of course copied as well). If the clipboard text is later used in some search function in the web app... search is going to be broken. Although this solution can be seen in some well known web applications, avoid if possible.

    Warning: when inserting additional characters, you should not separate multiple code points within grapheme. See https://unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries for more info.

    0 讨论(0)
  • 2020-11-22 03:43

    Workaround that uses overflow-wrap and works fine with normal table layout + table width 100%

    https://jsfiddle.net/krf0v6pw/

    HTML

    <table>
      <tr>
        <td class="overflow-wrap-hack">
          <div class="content">
            wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
          </div>
        </td>
      </tr>
    </table>
    

    CSS

    .content{
      word-wrap:break-word; /*old browsers*/
      overflow-wrap:break-word;
    }
    
    table{
      width:100%; /*must be set (to any value)*/
    }
    
    .overflow-wrap-hack{
      max-width:1px;
    }
    

    Benefits:

    • Uses overflow-wrap:break-word instead of word-break:break-all. Which is better because it tries to break on spaces first, and cuts the word off only if the word is bigger than it's container.
    • No table-layout:fixed needed. Use your regular auto-sizing.
    • Not needed to define fixed width or fixed max-width in pixels. Define % of the parent if needed.

    Tested in FF57, Chrome62, IE11, Safari11

    0 讨论(0)
  • 2020-11-22 03:43

    Tables wrap by default, so make sure the display of the table cells are table-cell:

    td {
       display: table-cell;
    }
    
    0 讨论(0)
  • 2020-11-22 03:44
    <td style="word-break:break-all;">longtextwithoutspace</td>
    

    or

    <span style="word-break:break-all;">longtextwithoutspace</span>
    
    0 讨论(0)
  • 2020-11-22 03:44

    Check out this demo

       <table style="width: 100%;">
        <tr>
            <td><div style="word-break:break-all;">LongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWord</div>
            </td>
            <td>
                <span style="display: inline;">Foo</span>
            </td>
        </tr>
    </table>

    Here is the link to read

    0 讨论(0)
  • 2020-11-22 03:45

    A long shot, but double-check with Firebug (or similar) that you aren't accidentally inheriting the following rule:

    white-space:nowrap;
    

    This may override your specified line break behaviour.

    0 讨论(0)
提交回复
热议问题