HTML Guide
Both <table>
and <td>
elements no longer accept a width
attribute. Instead, you should use CSS as in this example:
<table style="width:100%;">
<tr>
<td style="width:50px;">Name</td>
</tr>
</table>
Related W3C validator issues
A <table> contains a <tr> row that has less <td> columns than the column count established by the first row. Check the table to ensure all rows have the same number of columns.
For example, in the following table, the first <tr> row defines that it’s 2 columns wide, but the second <tr> row tries to use only 1 column:
<table>
<!-- This first row sets the table as 2 columns wide -->
<tr>
<td>First</td>
<td>Second</td>
</tr>
<!-- This second row has only 1 column -->
<tr>
<td>Wrong</td>
</tr>
</table>
The <table> element does not accept a height attribute. Use CSS instead.
The value px for a width property is incorrect, it should include both the value and the units, like 10px, or just 0 if it’s zero width. Using only the units without the value is incorrect.
Example of Incorrect CSS
<style>
.example {
width: 300; /* This is missing the unit */
}
.example2 {
width: px; /* This is missing the value */
}
</style>
Corrected Example of CSS
Make sure to include the unit (like px, em, %, etc.) when specifying the width:
<style>
.example {
width: 300px; /* Correctly includes 'px' unit */
}
</style>
Conclusion
Always ensure to provide proper units when specifying dimensions in CSS. Common units are:
- px (pixels)
- em (relative to the font size of the element)
- % (percentage of the parent element’s width)
The value specified for a width attribute in CSS is not valid.
The width CSS property sets an element’s width. There are many allowed values for this attribute, for example:
/* <length> values */
width: 300px;
width: 25em;
/* <percentage> value */
width: 75%;
/* Keyword values */
width: max-content;
width: min-content;
width: fit-content(20em);
width: auto;
/* Global values */
width: inherit;
width: initial;
width: revert;
width: unset;
Ensure each column in your table has at least one <td> or <th> cell starting in it. This error often occurs when using the colspan or rowspan attributes incorrectly.
Example of correct usage:
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
Incorrect usage example:
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td colspan="2">Row 1, Cell 1</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<!-- Missing cell in column 2 -->
</tr>
</table>
The corrected version ensuring each column has a starting cell:
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td colspan="2">Row 1, spanning both columns</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
A <table> contains an incoherent number of columns on one of its rows. Check the structure of the table to find the invalid row.
For example, in the following table, the first <tr> row defines that it’s 2 columns wide, but the second <tr> row tries to use 5 columns by means of a colspan attribute:
<table>
<!-- This first row sets the table as 2 columns wide -->
<tr>
<td>First</td>
<td>Second</td>
</tr>
<!-- This second row tries to use 5 columns -->
<tr>
<td colspan="5">Wrong</td>
</tr>
</table>
<td> elements no longer accept an align attribute. This can be achieved using CSS like this:
<td style="text-align:center;">content</td>
<td> elements no longer accept a valign attribute. This can be achieved using CSS like this:
<td style="vertical-align:middle;">content</td>
Table rows on the same <table> element must have the same number of columns, which comes determined by the first tr row.
For example, this table is wrong as the first row defines 2 columns, while the second row tries to use 4 columns:
<table>
<tr>
<td>Liza</td>
</tr>
<tr>
<td>Jimmy</td>
<td>14</td>
</tr>
</table>
The width and height attributes on <img> elements expect a digit to specify the dimension in pixels. It should not contain units, letters or percent signs.
You can achieve this using CSS instead, for example:
<!-- Invalid syntax, the height attribute expects only digits -->
<img src="photo.jpg" alt="cat" height="auto" />
<!-- Valid syntax using CSS -->
<img src="photo.jpg" alt="cat" style="height: auto" />