4

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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
santa
  • 12,234
  • 49
  • 155
  • 255
  • Related, possible duplicate (but doesn't address the direct question of 'is `` a valid(void) tag'): https://stackoverflow.com/questions/3558119/are-non-void-self-closing-tags-valid-in-html5 – TylerH Jul 18 '23 at 21:06

2 Answers2

14

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 />).

http://www.w3.org/TR/xhtml-media-types/#C_3

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

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.

Screenshot: Screenshot of an error message about self-closing syntax being used on a non-void element

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101