Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a table without tr valid?

Is it valid HTML to omit the tr elements in a table if you have only a single row? If it isn't valid, do all current browsers interpret it as intended?

In other words, does this ...

<table>
     <td>column 1</td>
     <td>column 2</td>
</table>

... behave the same as this ...

<table>
   <tr>
     <td>column 1</td>
     <td>column 2</td>
   </tr>
</table>

?

like image 698
Max Avatar asked Feb 07 '23 14:02

Max


1 Answers

Although it's not a valid HTML as per the Spec, modern web browsers don't behave strictly and give a more readable output to the users instead of junk which is sometimes the mistake of the developer (not pointing to you).

What exactly works and what not, cannot be determined. Also if some wrong code is parsed by the browsers currently does not ensure if will do the same in future versions too.

Check the below excerpt about Browser's Error Tolerance from HTMLRocks article How Browsers Work: Behind the scenes of modern web browsers. The link has certain examples also about error corrections.

Error handling is quite consistent in browsers, but amazingly enough it hasn't been part of HTML specifications. Like bookmarking and back/forward buttons it's just something that developed in browsers over the years. There are known invalid HTML constructs repeated on many sites, and the browsers try to fix them in a way conformant with other browsers.

The HTML5 specification does define some of these requirements. (WebKit summarizes this nicely in the comment at the beginning of the HTML parser class.)

The parser parses tokenized input into the document, building up the document tree. If the document is well-formed, parsing it is straightforward.

Unfortunately, we have to handle many HTML documents that are not well-formed, so the parser has to be tolerant about errors.

We have to take care of at least the following error conditions:

  1. The element being added is explicitly forbidden inside some outer tag. In this case we should close all tags up to the one which forbids the element, and add it afterwards.

  2. We are not allowed to add the element directly. It could be that the person writing the document forgot some tag in between (or that the tag in between is optional). This could be the case with the following tags: HTML HEAD BODY TBODY TR TD LI (did I forget any?).

  3. We want to add a block element inside an inline element. Close all inline elements up to the next higher block element.

  4. If this doesn't help, close elements until we are allowed to add the element–or ignore the tag.

You can also check the w3c article Validating your HTML > Different browsers interpret invalid HTML differently

Valid HTML is the only contract you have with the browser manufacturers. The HTML specification says how you should write it, and how they should interpret your document

....

None of the different browsers’ behaviours is incorrect; they’re all trying to fill in the gaps of your incorrect code. The bottom line is, avoid invalid markup if at all possible in your page!

Note that HTML5 fixes this, as for the first time in the history of HTML it defines how browsers should handle badly-formed markup. At the time of writing however, support for this HTML5 error handling was not widespread across browsers, so you can't yet rely on it.

like image 176
Techie Avatar answered Feb 16 '23 02:02

Techie