3
One way to settle this is with the property table-layout
, used the value as fixed
vc determines that the table construction algorithm should maintain a proportional width of all columns, as you can see in the Mozilla documentation on the subject: https://developer.mozilla.org/en-US/docs/Web/CSS/table-layout
OBS: This is an old property, still from CSS2, so it is widely accepted by all Browsers.
Now follow three table templates, one with table-layout: fixed
and another without (table-layout: auto
) so you can compare, in addition to a fixed
width-defined. Also display under "Full Page" to see how it behaves when the screen increases:
table {
width: 100%;
border-collapse: collapse;
border: 1px solid black;
text-align: center;
table-layout: fixed;
}
td, th {
border: 1px solid black;
overflow: hidden;
text-overflow: ellipsis;
}
<p>table com table-layout: fixed</p>
<table >
<thead>
<tr>
<th>texto</th>
<th>texto texto texto</th>
<th>texto texto</th>
<th>te</th>
<th>umapalavramuitomuitogrande</th>
</tr>
</thead>
<tbody>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td>e</td>
</tr>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td>e</td>
</tr>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td>e</td>
</tr>
</tbody>
</table>
<p>table com table-layout: auto</p>
<table style="table-layout: auto;">
<thead>
<tr>
<th>texto</th>
<th>texto texto texto</th>
<th>texto texto</th>
<th>te</th>
<th>umapalavramuitomuitogrande</th>
</tr>
</thead>
<tbody>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td>e</td>
</tr>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td>e</td>
</tr>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td>e</td>
</tr>
</tbody>
</table>
<p>table com table-layout: fixed e largura fixa</p>
<table style="table-layout: fixed; width: 400px;">
<thead>
<tr>
<th>texto</th>
<th>texto texto texto</th>
<th>texto texto</th>
<th>te</th>
<th>umapalavramuitomuitogrande</th>
</tr>
</thead>
<tbody>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td>e</td>
</tr>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td>e</td>
</tr>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td>e</td>
</tr>
</tbody>
</table>
only with HTML put width="value" in all
td
of the firsttr
– user60252