CSS text-overflow in a table cell?

后端 未结 12 931
情话喂你
情话喂你 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:17

    Why does this happen?

    It seems this section on w3.org suggests that text-overflow applies only to block elements:

    11.1.  Overflow Ellipsis: the ‘text-overflow’ property
    
    text-overflow      clip | ellipsis |   
    Initial:           clip   
    APPLIES TO:        BLOCK CONTAINERS               <<<<
    Inherited:         no  
    Percentages:       N/A  
    Media:             visual  
    Computed value:    as specified  
    

    The MDN says the same.

    This jsfiddle has your code (with a few debug modifications), which works fine if it's applied to a div instead of a td. It also has the only workaround I could quickly think of, by wrapping the contents of the td in a containing div block. However, that looks like "ugly" markup to me, so I'm hoping someone else has a better solution. The code to test this looks like this:

    td, div {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      border: 1px solid red;
      width: 80px;
    }
    Works, but no tables anymore:
    
    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.
    Works, but non-semantic markup required:
    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.

提交回复
热议问题