html - 使用绝对定位

标签 html css

我正在尝试使用绝对定位来定位包含蓝色方 block 的 div。由于某种原因,我无法将其到达我想要的位置。

JSFIDDLE:http://jsfiddle.net/qkF3Z/

我的代码:

#share-area-arrow {
position: absolute;
height: 10px;
width: 10px;
background-color: blue;
}

它应该是什么样子: Correct Positioning

我可能做错了什么?

最佳答案

有 2 件。绝对位置将使用最近的相对定位父级的坐标系。所以你需要添加相对于父级的位置:

#share-something {
    margin-right: auto;
    margin-left: auto;
    margin-bottom: 40px;
    height: auto;
    width: 540px;
    overflow: auto;
    position:relative;
}

然后定位箭头:

#share-area-arrow {
    position: absolute;
    top:10px;
    left:70px;
    height: 10px;
    width: 10px;
    background-color: blue;
}

http://jsfiddle.net/qkF3Z/6/

可以在这里找到不同位置类型之间的非常好的解释:http://alistapart.com/article/css-positioning-101 。要点是当您希望元素在 dom 内保持其空间,但出现在另一个位置时,请使用相对位置。如果你想完全移动元素,请使用绝对位置。

关于html - 使用绝对定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19234616/

相关文章:

html5 : why is my progressbar represented in green when I ask it to be in red?

html - 使用导航栏、旋转木马和简介进行容器定位

Javascript 进度条没有更新 'on the fly' ,但是一旦进程完成就立即更新?

html - 父级中的对齐内联 block 元素

javascript - jQuery/JavaScript 不能在脚本标签内工作

jquery - Div 插入其他 div

html - 绝对侧边栏或固定页脚

html - 网页内的 Firefox 打印选项

javascript - 如何防止响应模式下图像换行

Javascript 3 种方式切换