Making a Bootstrap table column fit to content

前端 未结 7 2089
情话喂你
情话喂你 2021-01-30 02:40

I\'m using Bootstrap, and drawing a table. The rightmost column has a button in it, and I want it to drop down to the minimum size it needs to fit said button.

7条回答
  •  孤独总比滥情好
    2021-01-30 03:26

    Tested on Bootstrap 4.5 and 5.0

    None of the solution works for me. The td last column still takes the full width. So here's the solution works.

    Add table-fit to your table

    table.table-fit {
        width: auto !important;
        table-layout: auto !important;
    }
    table.table-fit thead th, table.table-fit tfoot th {
        width: auto !important;
    }
    table.table-fit tbody td, table.table-fit tfoot td {
        width: auto !important;
    }
    

    Here's the one for sass uses.

    @mixin width {
        width: auto !important;
    }
    
    table {
        &.table-fit {
            @include width;
            table-layout: auto !important;
            thead th, tfoot th  {
                @include width;
            }
            tbody td, tfoot td {
                @include width;
            }
        }
    }
    

提交回复
热议问题