Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML table last td to take remaining width

I have a table with one TR and 2 TD's. I want the first TD to adjust width automatically depending on its content, and the second TD to use up the remainder of the width.

This is what I have:

<table style="width: 100%;">
  <tr>
    <td>short content</td>
    <td>long content</td>
  </tr>
</table>

The entire table width is 100% (as I want it), but I don't know how to adjust the TD widths to achieve my goal.

like image 304
user3173098 Avatar asked Jul 02 '16 16:07

user3173098


People also ask

How do I keep my table width fixed 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.

How do I fix TD length in HTML?

To fix the width of td tag the nth-child CSS is used to set the property of specific columns(determined by the value of n) in each row of the table.

How do you give equal width to all TD tables?

Using table-layout: fixed as a property for table and width: calc(100%/3); for td (assuming there are 3 td 's). With these two properties set, the table cells will be equal in size.


1 Answers

You can give the first td a small width, e.g. 1px, with white-space: nowrap;

table {
  width: 100%;
  border-collapse: collapse;
}
td {
  border: 1px solid red;
}
td:first-child {
  width: 1px;
  white-space: nowrap;
}
<table>
  <tr>
    <td>short content</td>
    <td>long content</td>
  </tr>
</table>

Or, give the last td a large width, e.g. 100%.

table {
  width: 100%;
  border-collapse: collapse;
}
td {
  border: 1px solid red;
}
td:first-child {
  white-space: nowrap;
}
td:last-child {
  width: 100%;
}
<table>
  <tr>
    <td>short content</td>
    <td>long content</td>
  </tr>
</table>
like image 194
Stickers Avatar answered Oct 29 '22 18:10

Stickers