Positioning relative to table-cell

2019-09-05 07:18发布

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.

Source: http://www.w3.org/TR/CSS21/visuren.html#propdef-position

Is there any idea to position relative to which I have set display property to table-cell using :before or :after selector?

.someclass{display: table-cell; position: relative; z-index: auto;} 
/*this won't work*/

2条回答
戒情不戒烟
2楼-- · 2019-09-05 07:30

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

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

td span {
    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.

查看更多
迷人小祖宗
3楼-- · 2019-09-05 07:45

IMHO: In browsers which support CSS3 position: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'?
查看更多
登录 后发表回答