4

I am working on this code and this code working fine in Chrome but not in Firefox

Here is code

<html lang="en-US">
<body><table style="width: 100%;">
<tbody><tr>
<td style="position: relative; width: 50%; height: 500px; vertical-align: top; overflow: hidden;">
<div style="position: absolute; background-color: blue; height: 100%; width: 100%;"></div></td>
<td style="position: relative; width: 50%; height: 500px;"></td>
</tr>
</tbody></table>
</body>
</html>

If you want to see here is JSFiddle for code in working

Please explain me why div in Firefox covering whole width of the screen, it should cover only 50% width as Chrome is showing.

user1390378
  • 403
  • 2
  • 7
  • 20

2 Answers2

3

Possibly because as of the 2.1 Specification relative positioning on table-cell elements is undefined by CSS:

The box's position is calculated according to the normal flow (this is called the position in normal flow). Then the box is offset relative to its normal position. When a box B is relatively positioned, the position of the following box is calculated as though B were not offset. The effect of 'position:relative' on table-row-group, table-header-group, table-footer-group, table-row, table-column-group, table-column, table-cell, and table-caption elements is undefined.

This is not true as of the Positioned Layout Module (They explicitly define the behaviour) but it hasn't been adopted by vendors yet.

Adrift
  • 58,167
  • 12
  • 92
  • 90
1

The problem is the position: absolute; of the inner DIV:

<html lang="en-US">
  <body>
    <table style="width: 100%;">
      <tbody>
        <tr>
          <td style="position: relative; width: 50%; height: 500px; vertical-align: top; overflow: hidden;">
            <div style="background-color: blue; height: 100%; width: 100%;"></div>
          </td>
          <td style="position: relative; width: 50%; height: 500px;">
          </td>
        </tr>
      </tbody>
    </table>
  </body>
</html>

You can test this with an update I made to your jsfiddle.

If you really need to use an absolute positioned div inside a cell, then you should put inside that cell a relative positioned div that contains the absolute one:

<td>
  <div style="position: relative; ... ">
    <div style="position: absolute;... ">
      ...
    </div>
  </div>
</td>

This another update to the original jsfiddle shows an absolute div inside the relative one, with an offset of 30 from the left and 10 from the top.

This old thread here in stackoverflow may be useful: Using Position Relative/Absolute within a TD?

Community
  • 1
  • 1
Nicolás Ozimica
  • 9,481
  • 5
  • 38
  • 51