你有正确的想法,但倒退。父元素需要position: relative和内部元素,因为内部元素相对于其父position: absolute元素是绝对定位的(从技术上讲,它的. 在父元素上指定使其成为其所有子元素的一部分)。offsetParentposition: relativeoffsetParent
下一步:要将父元素的左上角与绝对定位的子元素的右下角对齐,请在子元素right: 100%; bottom: 100%的 CSS 中指定。这使子 <100% of parent's width> 远离父级的右边缘,而 <100% of parent's height> 远离底部。
HTML
<div class=outer-box>
The Button
<div class=inner-box>
</div>
</div>
CSS
.outer-box {
position: relative;
}
.inner-box {
position: absolute;
/* align bottom-right with offsetParent's top-left */
bottom: 100%;
right: 100%;
width: 100px; /* fixed width, else contents will shrink */
}
同样在 jsFiddle:http: //jsfiddle.net/ryanartecona/g344W/2/
当你把它们对齐时,你可能想在里面放另一个盒子.inner-box,让它position: relative进行任何位置调整,比如在按钮上滑动固定距离等。