CSS text-overflow in a table cell?

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

    I solved this using an absolutely positioned div inside the cell (relative).

    td {
        position: relative;
    }
    td > div {
        position: absolute;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 100%;        
    }
    

    That's it. Then you can either add a top: value to the div or vertically center it:

    td > div {      
        top: 0;
        bottom: 0;
        margin: auto 0;
        height: 1.5em; // = line height 
    }
    

    To get some space on the right side, you can reduce the max-width a little.

提交回复
热议问题