DIV 相对于另一个 DIV 的位置?

新手上路,请多包涵

是否可以相对于另一个 DIV 定位一个 DIV?我想这可以通过首先将它放在参考 DIV 中,然后使用 position: relative 来完成。但是,我不知道如何在不影响引用 DIV 的内容的情况下执行此操作。我怎样才能正确地做到这一点?

请参阅:http: //jsfiddle.net/CDmGQ

原文由 Nathanael 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 626
2 个回答

First set position of the parent DIV to relative (specifying the offset, ie left , top etc. is not necessary) and then将 position: absolute 应用于具有所需偏移量的子 DIV。

这很简单,应该可以很好地解决问题。

原文由 rhino 发布,翻译遵循 CC BY-SA 3.0 许可协议

您需要设置外部 DIV 的 postion:relative 和内部 div 的位置:绝对值。

尝试这个。这是 演示

#one
{
    background-color: #EEE;
    margin: 62px 258px;
    padding: 5px;
    width: 200px;
    position: relative;
}

#two
{
    background-color: #F00;
    display: inline-block;
    height: 30px;
    position: absolute;
    width: 100px;
    top:10px;
}​

原文由 Nishu Tayal 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏