text-overflow: ellipsis in table-cell without width

后端 未结 7 688
后悔当初
后悔当初 2020-12-04 13:18

I\'m trying to achieve a responsive table width text-overflow: ellipsis; in the middle cell that looks like this:

| Button 1 | A one-lined text th

7条回答
  •  有刺的猬
    2020-12-04 13:46

    This is not really a clean solution, but it uses no JS and works in every browser I've tested.

    My solution consists in wrapping the cell's contents inside a table with table-layout: fixed and width: 100%.

    See the demo.

    Here's the full solution:

    Button 1
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Delectus doloremque magni illo reprehenderit consequuntur quia dicta labore veniam distinctio quod iure vitae porro nesciunt. Minus ipsam facilis! Velit sapiente numquam.
    Button 2

    .main-table {
        width: 100%;
    }
    
    .fixed-table {
        /* magic */
        width: 100%;
        table-layout: fixed;
    
        /*not really necessary, removes extra white space */
        border-collapse: collapse;
        border-spacing: 0;
        border: 0;
    }
    .fixed-table td {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    
    .nowrap {
        /* used to keep buttons' text in a single line,
         * remove this class to allow natural line-breaking.
         */
        white-space: nowrap;
    }
    

    It's not really clear what's the intent for the side buttons, hence I've used white-space: nowrap to keep them in the same line. Depending on the use case, you may prefer to apply a min-width and let them line-break naturally.

提交回复
热议问题