Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a Bootstrap table column fit to content

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.

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">  <table class="table table-responsive">          <tbody>              <tr>                  <th>Name</th>                  <th>Payment Method</th>                  <th></th>              </tr>              <tr>                  <td>Bart Foo</td>                  <td>Visa</td>                  <td><a role="button" class="btn btn-default btn-xs" href="/Payments/View/NnrN_8tMB0CkVXt06nkrYg">View</a></td>              </tr>          </tbody>      </table>

This renders like this:

Table Example

With some firebug highlighting, the column width has come out this wide:

Column Width

That column scales with the page, while the page is in the larger dynamic width modes. I have some idea how I'd go about fixing this in pure CSS, but most of those approaches will probably cause issues with the low width versions of the site.

How would I make that column drop down to the width of it's contents?

(As ever - Existing bootstrap classes > pure CSS > Javascript)

like image 648
Octopoid Avatar asked Jul 02 '15 11:07

Octopoid


People also ask

How do you set the size of a column in a bootstrap responsive table?

Table column width use the same layout as grids do; using col-[viewport]-[size] . Remember the column sizes should total 12; 1 + 3 + 3 + 5 = 12 in this example. Remember to set the <th> elements rather than the <td> elements so it sets the whole column.

How do you fit content in a table cell?

In "Table Tools" click the [Layout] tab > locate the "Cell Size" group and choose from of the following options: To fit the columns to the text (or page margins if cells are empty), click [AutoFit] > select "AutoFit Contents." To fit the table to the text, click [AutoFit] > select "AutoFit Window."

How do I resize a column in a table?

Select the rows or columns and then select Layout and choose your height and width. Select View > Ruler checkbox, select the cell you want, and then drag the markers on the ruler. Note: In Excel, select Home > Format, and then select Column Width.


1 Answers

Make a class that will fit table cell width to content

.table td.fit,  .table th.fit {     white-space: nowrap;     width: 1%; } 
like image 185
tmg Avatar answered Sep 19 '22 15:09

tmg