Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Position Relative/Absolute within a TD?

I have the following code:

<td style="position: relative; min-height: 60px; vertical-align: top;">     Contents of table cell, variable height, could be more than 60px;      <div style="position: absolute; bottom: 0px;">         Notice     </div> </td> 

This does not work at all. For some reason, the position:relative command isn't being read on the TD and the notice DIV is being placed outside of the content container at the bottom of my page. I have tried to put all the contents of the TD into a DIV such as:

<td>     <div style="position: relative; min-height: 60px; vertical-align: top;">         Contents of table cell, variable height, could be more than 60px;          <div style="position: absolute; bottom: 0px;">             Notice         </div>     </div> </td> 

However, this creates a new problem. Since the height of the contents of the table cell is variable, the notice DIV isn't always at the bottom of the cell. If a table cell stretches beyond the 60px marker, but none of the other cells do, then in the other cells, the notice DIV is at 60px down, instead of at the bottom.

like image 929
Jason Axelrod Avatar asked Dec 30 '10 17:12

Jason Axelrod


People also ask

How do you use absolute and relative positioning?

position: relative places an element relative to its current position without changing the layout around it, whereas position: absolute places an element relative to its parent's position and changing the layout around it.

Should I use position absolute or relative?

If you specify position:relative, then you can use top or bottom, and left or right to move the element relative to where it would normally occur in the document. Position Absolute: When you specify position:absolute, the element is removed from the document and placed exactly where you tell it to go.

Can an element have position absolute and relative?

If you position it as absolute , then you're positioning it relative to its closest ancestor which is fixed or relative ... Clearly, nothing can be absolute and relative at the same time. Either you want the element to position itself based on another ancestor's position or based on the page flow.

Is it good practice to use position absolute?

As long as you structure your HTML so that the elements follow a logical order that makes sense when rendered without CSS, there is no reason why using absolute positioning should be considered bad practice.


2 Answers

This is because according to CSS 2.1, the effect of position: relative on table elements is undefined. Illustrative of this, position: relative has the desired effect on Chrome 13, but not on Firefox 4. Your solution here is to add a div around your content and put the position: relative on that div instead of the td. The following illustrates the results you get with the position: relative (1) on a div good), (2) on a td(no good), and finally (3) on a div inside a td (good again).

On Firefox 4

<table>    <tr>      <td>        <div style="position:relative;">          <span style="position:absolute; left:150px;">            Absolute span          </span>          Relative div        </div>      </td>    </tr>  </table>
like image 173
avernet Avatar answered Oct 11 '22 07:10

avernet


This trick also suitable, but in this case align properties (middle, bottom etc.) won't be working.

<td style="display: block; position: relative;"> </td> 
like image 45
Sergey Onishchenko Avatar answered Oct 11 '22 09:10

Sergey Onishchenko