codingNightmares codingNightmares - 4 months ago 5
HTML Question

How to adjust width of <td>

I have a table with a multiple "sections". First there is the title, then the table data.

<table>
<tr> <th>First Name </th> </tr>
<tr>
<td>1</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>2</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>3</td>
<td>Johnson</td>
<td>67</td>
</tr>
<tr>
<td>4</td>
<td>Nilson</td>
<td>35</td>
</tr>
</table>


The width of the
<td>
is always as wide as the title's. How can I adjust the width of the
<td>
?

I've tried giving the
<td>
a class and adjusting it that way but no luck.

Answer

That's not how tables work. You either give a th for every column:

<tr>
    <th>ID</th>
    <th>First Name</th>
    <th>Age</th>
</tr>
<tr>
   <td>1</td>
   <td>Smith</td>
   <td>50</td>
</tr>
<tr>
   <td>2</td>
   <td>Jackson</td>
   <td>94</td>
</tr>
<tr>
   <td>3</td>
   <td>Johnson</td>
   <td>67</td>
</tr>
<tr>
   <td>4</td>
   <td>Nilson</td>
   <td>35</td>
</tr>

or you use colspan:

<tr>
    <th colspan="3">First Name </th>
</tr>
<tr>
   <td>1</td>
   <td>Smith</td>
   <td>50</td>
</tr>
<tr>
   <td>2</td>
   <td>Jackson</td>
   <td>94</td>
</tr>
<tr>
   <td>3</td>
   <td>Johnson</td>
   <td>67</td>
</tr>
<tr>
   <td>4</td>
   <td>Nilson</td>
   <td>35</td>
</tr>