58

Hi am trying to add a div above every <tr> but when i look at the html console the div are showing outside the table. below is the html code.

<table>
  <div>
    <tr><td></td></tr>
  </div>
  <div>
    <tr><td></td></tr>
  </div>
</table>

Is this not allowed? any help would be great.

jo_va
  • 13,504
  • 3
  • 23
  • 47
Amit
  • 3,251
  • 3
  • 20
  • 31

9 Answers9

68

<div> tag can not be used above <tr> tag. Instead you can use <tbody> tag to do your work. If you are planning to give id attribute to <div> tag and doing some processing, same purpose you can achieve through <tbody> tag. For further information visit this page

For example:

<table>
    <tbody class="green">
        <tr>
            <td>Data</td>
        </tr>
    </tbody>
    <tbody class="blue">
        <tr>
            <td>Data</td>
        </tr>
    </tbody>
</table>

secondly, you can put "div" tag inside "td" tag.

<table>
    <tr>
        <td>
            <div></div>
        </td>
    </tr>
</table>

Further questions are always welcome.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Aditya Ekbote
  • 1,493
  • 3
  • 15
  • 29
9

You can't put a div directly inside a table but you can put div inside td or th element.

For that you need to do is make sure the div is inside an actual table cell, a td or th element, so do that:

HTML:-

<tr>
  <td>
    <div>
      <p>I'm text in a div.</p>
    </div>
  </td>
</tr>

For more information :-

http://css-tricks.com/using-divs-inside-tables/

ghaschel
  • 1,313
  • 3
  • 20
  • 41
Neel
  • 11,625
  • 3
  • 43
  • 61
8

No, you cannot insert a div directly inside of a table. It is not correct html, and will result in unexpected output.

I would be happy to be more insightful, but you haven't said what you are attempting, so I can't really offer an alternative.

Patrick
  • 13,872
  • 5
  • 35
  • 53
8

You can not use tag to make group of more than one tag. If you want to make group of tag for any purpose like in ajax to change particular group or in CSS to change style of particular tag etc. then use

Ex.

<table>
  <tbody id="foods">
    <tr>
      <td>Group 1</td>
    </tr>
    <tr>
      <td>Group 1</td>
    </tr>
  </tbody>

  <tbody id="drinks">
    <tr>
      <td>Group 2</td>
    </tr>
    <tr>
      <td>Group 2</td>
    </tr>
  </tbody>
</table>
Abdullah
  • 2,015
  • 2
  • 20
  • 29
Deep Gandhi
  • 79
  • 1
  • 5
3

In the html tables, <table> tag expect <tr> tag right after itself and <tr> tag expect <td> tag right after itself. So if you want to put a div in table, you can put it in between <td> and </td> tags as data.

<table>
  <tr>
    <td>
      <div>
        <p>It works well</p>
      </div>
     </td>
  </tr>
<table>
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
PAVAN BANSAL
  • 127
  • 1
  • 3
1

A div cannot be added inside tr but there's an alternate solution here.

I tried adding a div inside tr but it seems a td should be the immediate child of a tr for it to work properly.

Adding a div inside td works fine.

I suppose you are trying to add some background or border-radius for the whole tr. Here's how I achieved the similar result in my project.

I'm using colspan and flex property to achieve that.

.flex-container{
  display: flex;
  margin: 5px;
}
table{
  border: 1px solid red;
}
tr{
  border: 1px solid green;
  padding: 5px;
}
.flex-container .col{
  box-sizing: border-box;
  border: 1px solid;
  padding: 5px;
  border-radius: 5px;
  margin: 5px;
  background: skyblue;
}
<table>
  <tr>
    <!-- Assuming you have 4 columns -->
    <td colspan="4">
      <div class="flex-container">
        <div class="col"> Item 1 </div>
        <div class="col"> Item 2 </div>
        <div class="col"> Item 3 </div>
      </div>
    </td>
  </tr>
</table>
Supercool
  • 453
  • 1
  • 4
  • 9
0

If we follow the w3 org table reference ,and follow the Permitted Contents section, we can see that the table tags takes tbody(optional) and tr as the only permitted contents.

So i reckon it is safe to say we cannot add a div tag which is a flow content as a direct child of the table which i understand is what you meant when you had said above a tr.

Having said that , as we follow the above link , you will find that it is safe to use divs inside the td element as seen here

semuzaboi
  • 4,972
  • 5
  • 20
  • 35
0

The problem will happen whenever it will render on small device. Element <div> inside <td> will occurs in mobile responsive screen.

mrhrifat
  • 161
  • 1
  • 2
  • 9
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 14 '22 at 06:33
-2

You could use display: table-row-group for your div.

<table>
  <div style="display: table-row-group">
    <tr><td></td></tr>
  </div>
  <div style="display: table-row-group">
    <tr><td></td></tr>
  </div>
</table>
Jaro
  • 982
  • 1
  • 10
  • 25