Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to omit </TD> and </TR> tags?

People also ask

Can I use TR without TD?

Yes, you need <td> . Browsers will still try to render the table if you write invalid HTML, but the rendering will be inconsistent between browsers. <th> can take the place of <td> if the cell is a header cell. It does not take the place of <tr> which is always required.

What is the purpose of TD and TR tags?

The HTML <td> element is found in an HTML table within the <body> tag. The <td> tag defines the standard cells in the table which are displayed as normal-weight, left-aligned text. The <tr> tag defines the table rows. There must be at least one row in the table.

Is TR empty tag?

It's legal for a <tr> element to contain no <td> elements or only empty ones. This indicates an empty row in the table.


This is valid HTML but invalid XHTML.
There's nothing intrinsically wrong with it.

If you look at the source for Google's privacy policy (or any of their other pages), you'll find some much terser HTML.

It does mean that your page will not be usable by an XML parser.


It is safe, since optionality in the standard means that all the browsers (at least the ones which even remotely matter) would have implemented this - and the browser standards compliance usually runs to the opposite side, into trying to work correctly with even invalid HTML as opposed to failing on missing optional tags.

Having said that, I find that omitting such tags makes things harder to read, which may or may not matter to you if the goal is size optimization.

P.S. Also, if you have very large tables, I wonder whether there's any overhead incurred by the browser's HTML parser when dealing with such constructs? I am not sure without benchmarking or really deep thinking about how HTML parser works in detail, but it is something that could possibly be a factor if it happens.


I strongly recommend against doing that, though it is valid in HTML4 (and 5). The bandwidth savings are miniscule when compared to the technical debt you are incurring. Also keep in mind it is not valid in XHTML, so be sure your doctype is set appropriately.


There have been optional tags in HTML since the very beginning — they are a feature inherited from SGML, and so the early browsers must have been able to deal with them. While XHTML moved away from this and required a much more uniform syntax, this doesn’t affect you unless you explicitly tell the browser to parse in XML mode. I’ve never seen a problem when using the standard parsers of HTML 4/5.

Since HTML5 so carefully describes when certain tags are optional, I would read that to imply that someone did lots of testing to make sure that in these cases, most browsers produce the same document tree.

And while the space savings are negligible, I find that leaving off the closing tags of <p>, <li>, <td>, and <tr> simply makes my life easier when I’m working in the markup, and makes me less likely to make an error.


Personally I don't consider it good practice. Looking at the spec it didn't give a whole lot of information. I know it's required for XHTML so I looked up the HTML 5 spec. HTML 5 seems to take the same take on it as HTML 4 which is what you've linked to, but gives a little more information:

A td element's end tag may be omitted if the td element is immediately followed by a td or th element, or if there is no more content in the parent element.


I advise always closing your tags. There's not really too good of a reason not to. Browsers can handle some improperly closed tags, but just to be on the safe side (and it's good programming practice!), close your tags!