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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11143273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:11:32  来源:igfitidea点击:

Position DIV relative to another DIV?

htmlcss

提问by Nathanael

Is it possible to position a DIV relative to another DIV? I imagine this can be done by first placing it inside of the reference DIV, and then using position: relative. However, I can't figure out how to do this without affecting the contents of the reference DIV. How can I do this properly?

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

See: http://jsfiddle.net/CDmGQ

见:http: //jsfiddle.net/CDmGQ

Thanks! =)

谢谢!=)

回答by rhino

First set positionof the parent DIV to relative(specifying the offset, i.e. left, topetc. is not necessary) and then apply position: absoluteto the child DIV with the offset you want.
It's simple and should do the trick well.

第一组position父DIV来relative(指定的偏移量,即lefttop等是没有必要的),然后应用position: absolute到子DIV你想要的偏移。
这很简单,应该能很好地解决问题。

回答by Nishu Tayal

You need to set postion:relative of outer DIV and position:absolute of inner div.
Try this. Here is the Demo

您需要设置 postion:relative of external DIV 和 position:absolute ofinner 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;
}?

回答by sachleen

You want to use position: absolutewhile inside the other div.

您想position: absolute在另一个 div 内使用。

DEMO

演示

回答by Dinesh Pathak DK

you can use position:relative;inside #one div and position:absoluteinside #two div. you can see it

您可以position:relative;在#one div 内和position:absolute#two div 内使用。 你可以看到