Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is OK to place a div inside th tag?

Tags:

html

I didn't think this was a problem but my IDE seems to tell me otherwise. Are there any browsers that would fail to render this correctly?

Using Netbeans IDE 7.3.

Thanks.

like image 615
Iglo Avatar asked Dec 07 '13 00:12

Iglo


People also ask

Can you put div inside a tag?

Nothing is wrong with placing a div inside a tag. In fact, you can place just about anything inside a tag as long as they are categorized as transparent , except that no descendant may be interactive content (eg: buttons or inputs) or an a element, and no descendant may have a specified tabindex attribute.

Can we put div inside TR?

No, you cannot insert a div directly inside of a table.

Can I have div in TD?

If you use a div in a td you will however get in a situation where it might be hard to predict how the elements will be sized. The default for a div is to determine its width from its parent, and the default for a table cell is to determine its size depending on the size of its content.


Video Answer


2 Answers

According to the HTML5 specification, that's absolutely fine. The content model is a normative description of what is allowed to be a child/descendant of the element. For the <th> element, only the following tags are permitted:

Content model:

Flow content, but with no header, footer, sectioning content, or heading content descendants, and if the th element is a sorting interface th element, no interactive content descendants.

The <div> tag is an example of flow content, so this should validate.

In the future, I'd like to recommend the W3C's validator service, which gives you a quick check to see whether your HTML is valid. In this instance, it passes:

<!DOCTYPE html>
<html>
<head><title>&nbsp;</title></head>
<body>
<table>
  <tbody>
    <tr>
      <th>
        <div></div>
      </th>
    </tr>
  </tbody>
</table>
</body>
</html>
like image 97
Qantas 94 Heavy Avatar answered Sep 26 '22 04:09

Qantas 94 Heavy


<DIV> inside <TD> or <TH> is absolutely fine, and is reasonably common requirement for positioning & layout purposes.

For example, the TH cannot have position:relative set, but the DIV can.. enabling it to be a positioning container.

like image 45
Thomas W Avatar answered Sep 22 '22 04:09

Thomas W