'div' inside 'table'
Asked Answered
M

5

37

Is a div inside a table allowed or not according to W3C?

Miguel answered 4/6, 2010 at 13:30 Comment(1)
Have you looked in the specification, or tried it out in the validator?Cooe
C
43
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <title>test</title>
  </head>
  <body>
    <table>
      <tr>
        <td>
          <div>content</div>
        </td>
      </tr>
    </table>
  </body>
</html>

This document was successfully checked as XHTML 1.0 Transitional!

Chippy answered 4/6, 2010 at 13:37 Comment(0)
W
40

You can't put a div directly inside a table, like this:

<!-- INVALID -->
<table>
  <div>
    Hello World
  </div>
</table>

Putting a div inside a td or th element is fine, however:

<!-- VALID -->
<table>
  <tr>
    <td>
      <div>
        Hello World
      </div>
    </td>
  </tr>
</table>
Woodard answered 4/6, 2010 at 13:38 Comment(2)
u didnt mention <tr> =PVespucci
@ I don't think you can use a div directly inside a tr as wellEttie
B
19

You can put div tags inside a td tag, but not directly inside a table or tr tag.

Examples:

This works:

<table>
  <tr>
    <td>
      <div>This will work.</div>
    </td>
  </tr>
<table>

This does not work:

<table>
  <tr>
    <div> this does not work. </div>
  </tr>
</table>

Nor does this work:

<table>
  <div> this does not work. </div>
</table>
Biped answered 4/6, 2010 at 13:36 Comment(2)
This solution works, but it seems that when , "div", elements are used as children of, "td", elements, the specified width and height of the, "div", is ignored.Draw
Re "does not work": Can you be more specific? What happens?Irreparable
S
9

While you can, as others have noted here, put a DIV inside a TD (not as a direct child of TABLE), I strongly advise against using a DIV as a child of a TD. Unless, of course, you're a fan of headaches.

There is little to be gained and a whole lot to be lost, as there are many cross-browser discrepancies regarding how widths, margins, borders, etc., are handled when you combine the two. I can't tell you how many times I've had to clean up that kind of markup for clients because they were having trouble getting their HTML to display correctly in this or that browser.

Then again, if you're not fussy about how things look, disregard this advice.

Smacking answered 4/6, 2010 at 13:50 Comment(0)
B
2

It is allowed as TD can contain inline and block lements.

Here you can find it in the reference: http://xhtml.com/en/xhtml/reference/td/#td-contains

Boil answered 4/6, 2010 at 13:36 Comment(1)
The is (effectively) broken: "This webpage was generated by the domain owner using Sedo Domain Parking."Irreparable

© 2022 - 2024 — McMap. All rights reserved.