Skip to content Skip to sidebar Skip to footer

Positioning Relative To Table-cell

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 i

Solution 1:

IMHO: In browsers which support CSS3position:relative on table elements should work. It now works only in Firefox.

Sources:

https://www.w3.org/TR/css-position-3/#valdef-position-relative

https://www.w3.org/TR/css-position-3/#property-index

Property name: position Applies to: all elements except table-column-group and table-column

https://developer.mozilla.org/en-US/docs/Web/CSS/position#relative About 'stacking context' but that is not the subject of this question

This value (position: relative) creates a new stacking context when the value of z-index is not auto. Its effect on table-*-group, table-row, table-column, table-cell, and table-caption elements is undefined.

Related questions:

  1. Overlay on top of tbody
  2. Bug in most browsers?: Ignoring position relative on 'tbody', 'tr' and 'td'?

Solution 2:

There is a kinda hacky way without additional elements using negative margin:

http://jsfiddle.net/CvyxM/1/

tdspan {
    display: block;
    width: 10px;
    height: 10px;
    margin: -5px;
    background: red;
    position: relative;
    top: -5px;
    left: -5px;
}

You simply position the element relative to the cell, while eliminating its dimensional impact on other elements by adding negative margins on all sides in correlation to the elements own dimensions.

Problem here is that the positioned element must have fixed dimensions (can't work with percentage negative margin because that depends on the outer element).

The other, probably more common solution is to add an additional wrapper element inside the cell width position relative and position other child elements absolute to this wrapper.

Post a Comment for "Positioning Relative To Table-cell"