tags:

views:

563

answers:

5

I feel like this should be a no brainer, but clearly I'm missing something...

I'm stuck with an HTML table on a page, and need to absolutely position an element that is rendered inside of the table so that it can display properly when we apply DHTML to show it.

I tried absolutely positioning it relative to the bottom of a table row, but the browser (FF and IE) will not render it relative to the row. Instead it takes the positioning relative to the next parent above the row that has relative positioning.

Basically it is:

<table>
<tr class="aRelativelyPositionedClass">
    <td>
         <div class="anAbsolutelyPositionedClass">stuff I want to absolutely position</div>
    </td>
</tr>
</table>

Is it possible to position the inner div relative to the row? Or is there an HTML issue I'm missing with tables?

+1  A: 

If there's nothing else in the table cell apart from the div you want to position, it's possible that it's collapsing to zero dimensions when you move the div out of the flow with the absolute positioning, and this is throwing your calculations out. Is there an explicit height set on the row or the cell?

Edit: I think Guffa is correct. With just one div in the cell I couldn't get it to position relative to either the row or the cell. I think you could fake the effect you're looking for by adding some markup:

<table border="1">
    <tr style="position:relative;">
        <td><img src="http://sstatic.net/so/img/so/logo.png" height="61px" width="250px" alt=""/></td>
        <td>
            <div style="position: relative; height: 100px; width: 100px;">
                <div style="border: 1px solid red; position: absolute; bottom: -10px; left -10px;">Position me</div>
            </div>
        </td>
    </tr>
</table>
robertc
Sadly, there is plenty in the row which will keep it from collapsing. The row does have a valid height (approx 60px), so it must be something with the way the browsers interpret positioning within tables.
jkelley
Some clue as to why I'm getting down voted would be nice.
robertc
Not by me, I think you're response was correct in the case where no other data is in the cell
jkelley
Probably some people some people are just offended at how nasty the code gets once you start mixing table based and css layout :)
robertc
+1  A: 

try in CSS

.aRelativelyPositionedClass td {
   // your style 
}

I believe you would have to explicitly state relative too.

Daniel Elliott
i tried this - FF didn't honor the class, but IE did. Unfortunately, IE also makes the remainder of the table sit on a layer above the div I'm absolutely positioning, seemingly regardless of what I do w/ the z-index property.
jkelley
A: 

Paste this in a file to see how it's done.

Remember to set the container's size. I did it in HTML here to keep the example short, but you should do that in CSS.

<table border="1" width="500">
<tr height="200">
    <td>
         <div style="position:relative;top:20;left:20">stuff I want to position</div>
         <div style="position:relative;top:30;left:30">stuff I want to position</div>
         <div style="position:relative;top:40;left:40">stuff I want to position</div>
         <div style="position:relative;top:50;left:50">stuff I want to position</div>
    </td>
</tr>
<tr height="200">
    <td>
         <div style="position:relative;top:20;left:20">stuff I want to position</div>
         <div style="position:relative;top:30;left:30">stuff I want to position</div>
         <div style="position:relative;top:40;left:40">stuff I want to position</div>
         <div style="position:relative;top:50;left:50">stuff I want to position</div>
    </td>
</tr>
</table>
Wouter van Nifterick
I hate it when people downvote to negative without making an effort to explain what they didn't find helpful :(
Wouter van Nifterick
+1  A: 

I don't think that you can position it relative to the row, as the row is not really a visible element.

You should be able to position it relative to the cell by setting the style position:relative on the cell to make it a layer. Still the cell is not an independent element, so you may have to put another div in the cell and make that a layer instead to make it work properly.

(Tables are problematic for layout when you combine it with other techniques... Perhaps you should consider removing the table altogehter...)

Guffa
yes, I'd like to remove the table, but for now we are stuck with it in the page. I may have to end up using an independent div. When I do this, though, the other table elements still sit over the div (like the select input issue with DHTML overlays). Any way to place the layer clearly over the table layers, or is this beyond css control?
jkelley
OK - false alarm on the issue of other table elements overlaying the absolutely positioned items. It was due to the other relatively positioned divs from each row, not the table itself.
jkelley
+1  A: 

CSS 2.1 Specification:

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.

So the browsers fall back to the next parent whose behavior is considered defined: table.

One solution is to force those rows to display as blocks:

tr.aRelativelyPositionedClass {
    display: block;
}
scronide
Good idea - this works great in FF, but doesn't seem to agree with IE. I think I'll have to hack it in using extra markup in the cell per Guffa
jkelley