Positioning relative to table-cell

痞子三分冷 提交于 2019-12-12 04:57:49

问题


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*/

回答1:


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'?



回答2:


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.



来源:https://stackoverflow.com/questions/16355672/positioning-relative-to-table-cell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!