How to make a table cell shrink width to fit only its contents?

后端 未结 1 517
被撕碎了的回忆
被撕碎了的回忆 2021-02-05 07:55

I want the table to have 100% width, but only one column from it should have a free width, like:

| A | B | C                                                              


        
1条回答
  •  误落风尘
    2021-02-05 08:24

    If I understood you, you have tabular data but you want to present it in the browser using div elements (AFAIK tables and divs with display:table behaves mostly the same).

    (here is a working jsfiddle: http://jsfiddle.net/roimergarcia/CWKRA/)

    The markup:

    first
    test
    this is long
    2nd
    more test
    it is still long

    And the CSS:

    .theTable{
        display:table; 
        width:95%; /* or whatever width for your table*/
    }
    .theRow{display:table-row}
    .theCell{
        display:table-cell;
        padding: 0px 2px; /* just some padding, if needed*/
        white-space: pre; /* this will avoid line breaks*/
    }
    .bigCell{
        width:100%; /* this will shrink other cells */
    }​
    

    The sad part is I couldn't do this one year ago, when I really needed it -_-!

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