Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change column width bootstrap tables

I have a static PHP site I'm using Bootstrap on. The tables are controlled by Bootstrap.

I've noticed that I have seemingly no control over the lengths of the TD sections so that I can prevent the phone numbers, etc..., from wrapping. As you can see there is plenty of space on the end, just don't know how to spread that space out.

I tried inline widths on the <th> but nothing happens:

  <table class="table table-condensed table-striped  table-hover">
  <thead>
    <tr>
        <th>#</th>
        <th width="12%">Last Name</th>
        <th width="12%">First Name</th>
        <th width="12%">Spouse's Name</th>
        <th width="20%">Address</th>
        <th width="5%">City</th>
        <th  width="12%"class="did_phone">Phone</th>
        <th width="15%">E-Mail</th>
         <th width="15%"></th>
    </tr>
  </thead>
  <tbody>

So... how does one change the <td> widths, make adjustments so there is no wrapping, in Bootstrap?

like image 647
Norman Bird Avatar asked Dec 29 '13 17:12

Norman Bird


People also ask

How do you change the column width in a table?

Change column width To change the width to a specific measurement, click a cell in the column that you want to resize. On the Layout tab, in the Cell Size group, click in the Table Column Width box, and then specify the options you want.

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

On the table you now need to enable flex box by adding the class d-flex , and drop the xs to allow bootstrap to automatically detect the viewport. 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.

How do I change the size of a table in bootstrap?

Small table: To make the table smaller in size use the class table-sm along with the class table within the <table> tag. This reduces the cell padding to half. To make the dark table smaller in size use the combination of classes table, table-sm, and table-dark within the <table> tag.

How do you change the width of a column in a table in HTML?

The width of the columns i.e. td in a table can be fixed very easily. This can be done by adding the width attribute in the <td> tag. If the width is not specified, the width of the column changes according to the change in the content.


2 Answers

Bootstrap makes tables 100%. So Tables th is set to 100%. The solution is to make it auto. So I simply overrode Bootstrap by adding the following to my css:

table th {
    width: auto !important;
}

And everything lined up perfectly.

like image 94
Norman Bird Avatar answered Sep 28 '22 06:09

Norman Bird


You could try using this style="width:12%" in each th.

Example:

<table class="table table-bordered">
 <thead>
 <tr>
    <th>#</th>
    <th style="width:12%">Last Name</th>
    <th style="width:12%">First Name</th>
    <th style="width:12%">Spouse's Name</th>
    <th style="width:20%">Address</th>
    <th style="width:5%">City</th>
    <th  style="width:12%"class="did_phone">Phone</th>
    <th style="width:15%">E-Mail</th>
 </tr>
 </thead>
 <tbody>
    <tr>
        <td>1</td>
        <td>Last Name</td>
        <td>First Name</td>
        <td>Spouse's Name</td>
        <td>Adress</td>
        <td>City</td>
        <td>Phone</td>
        <td>E-Mail</td>
    </tr>
 </tbody>
</table>
like image 22
David Navarro Astudillo Avatar answered Sep 28 '22 06:09

David Navarro Astudillo