CSS text-overflow in a table cell?

后端 未结 12 937
情话喂你
情话喂你 2020-11-22 04:38

I want to use CSS text-overflow in a table cell, such that if the text is too long to fit on one line, it will clip with an ellipsis instead of wrapping to mult

相关标签:
12条回答
  • 2020-11-22 05:18

    It seems that if you specify table-layout: fixed; on the table element, then your styles for td should take effect. This will also affect how the cells are sized, though.

    Sitepoint discusses the table-layout methods a little here: http://reference.sitepoint.com/css/tableformatting

    0 讨论(0)
  • 2020-11-22 05:21

    This worked in my case, in both Firefox and Chrome:

    td {
      max-width: 0;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      width: 100%;
    }
    
    0 讨论(0)
  • 2020-11-22 05:22

    This is the version that works in IE 9.

    http://jsfiddle.net/s27gf2n8/

    <div style="display:table; table-layout: fixed; width:100%; " >
            <div style="display:table-row;">
                <div style="display:table-cell;">
                    <table style="width: 100%; table-layout: fixed;">
                        <div style="text-overflow:ellipsis;overflow:hidden;white-space:nowrap;">First row. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
                    </table>
                </div>
                <div style="display:table-cell;">
                    Top right Cell.
                </div>
            </div>
            <div style="display:table-row;">
                <div style="display:table-cell;">
                    <table style="width: 100%; table-layout: fixed;">
                        <div style="text-overflow:ellipsis;overflow:hidden;white-space:nowrap;">Second row - Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
                    </table>
                </div>
                <div style="display:table-cell;">
                    Bottom right cell.
                </div>
            </div>
        </div>
    
    0 讨论(0)
  • 2020-11-22 05:23

    When it's in percentage table width, or you can't set fixed width on table cell. You can apply table-layout: fixed; to make it work.

    table {
      table-layout: fixed;
      width: 100%;
    }
    td {
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
      border: 1px solid red;
    }
    <table>
      <tr>
        <td>Lorem ipsum and dim sum yeah yeah yeah. Lorem ipsum and dim sum yeah yeah yeah. Lorem ipsum and dim sum yeah yeah yeah. Lorem ipsum and dim sum yeah yeah yeah. Lorem ipsum and dim sum yeah yeah yeah.</td>
        <td>Lorem ipsum and dim sum yeah yeah yeah. Lorem ipsum and dim sum yeah yeah yeah. Lorem ipsum and dim sum yeah yeah yeah. Lorem ipsum and dim sum yeah yeah yeah. Lorem ipsum and dim sum yeah yeah yeah.</td>
      </tr>
    </table>

    0 讨论(0)
  • 2020-11-22 05:24

    In case you don't want to set fixed width to anything

    The solution below allows you to have table cell content that is long, but must not affect the width of the parent table, nor the height of the parent row. For example where you want to have a table with width:100% that still applies auto-size feature to all other cells. Useful in data grids with "Notes" or "Comment" column or something.

    Add these 3 rules to your CSS:

    .text-overflow-dynamic-container {
        position: relative;
        max-width: 100%;
        padding: 0 !important;
        display: -webkit-flex;
        display: -moz-flex;
        display: flex;
        vertical-align: text-bottom !important;
    }
    .text-overflow-dynamic-ellipsis {
        position: absolute;
        white-space: nowrap;
        overflow-y: visible;
        overflow-x: hidden;
        text-overflow: ellipsis;
        -ms-text-overflow: ellipsis;
        -o-text-overflow: ellipsis;
        max-width: 100%;
        min-width: 0;
        width:100%;
        top: 0;
        left: 0;
    }
    .text-overflow-dynamic-container:after,
    .text-overflow-dynamic-ellipsis:after {
        content: '-';
        display: inline;
        visibility: hidden;
        width: 0;
    }
    

    Format HTML like this in any table cell you want dynamic text overflow:

    <td>
      <span class="text-overflow-dynamic-container">
        <span class="text-overflow-dynamic-ellipsis" title="...your text again for usability...">
          //...your long text here...
        </span>
      </span>
    </td>
    

    Additionally apply desired min-width (or none at all) to the table cell.

    Of course the fiddle: https://jsfiddle.net/9wycg99v/23/

    0 讨论(0)
  • 2020-11-22 05:26

    Wrap cell content in a flex block. As a bonus, cell auto fits visible width.

    table {
      width: 100%;
    }
    
    div.parent {
      display: flex;
    }
    
    div.child {
      flex: 1;
      width: 1px;
      overflow-x: hidden;
      text-overflow: ellipsis;
    }
    <table>
      <tr>
        <td>
          <div class="parent">
            <div class="child">
              xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            </div>
          <div>
        </td>
      </tr>
    </table>

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