I came across a code where a developer attempted to close an empty table cell like this <TD />
instead of <td></td>
.
Is <td />
valid? When is it appropriate to close a tag like <tag />
? What language does it come from originally?
I came across a code where a developer attempted to close an empty table cell like this <TD />
instead of <td></td>
.
Is <td />
valid? When is it appropriate to close a tag like <tag />
? What language does it come from originally?
Non-HTML Compatible XHTML (so not if you want to use a text/html
content-type or support IE 8 or older).
If an element permits content (e.g., the div element) but an instance of that element has no content (e.g., an empty section), DO NOT use the "minimized" tag syntax (e.g.,
<div />
).
No, <td />
is not valid HTML. If you check this in the W3C's HTML validator, you get an error:
Self-closing syntax (/>) used on a non-void HTML element. Ignoring the slash and treating as a start tag.
However, in HTML, for table rows, you can declare opening table rows without closing them (see Is it safe to omit </TD> and </TR> tags? for more info); this is valid:
...
<tr>
<td>Lorem Ipsum
<td>Dolor Sit
</tr>
...
If I were to add an empty, self-closed td (e.g. <td />
to that table row, it would probably be parsed by browsers like this:
...
<tr>
<td>Lorem Ipsum
<td>Dolor Sit
<td>
</tr>
But this is not guaranteed, and it also may or may not be what you are wanting, so it is dangerous to rely on; you should only self-close valid void tags, a list of which can be found on MDN. For table elements, either use opening tags as shown above or use the much more common opening and closing tags, <td>
and </td>
, respectively.
Self-closing tags comes from XML/XHTML, as Quentin's answer already discusses.