html - 内联 block 搞乱了相对定位

标签 html css-position css

我有以下代码:

    <HTML>
<head>
<style>div{border:dashed 1px silver}</style>
</head>
<BODY style="background: #fff;">

<div style="position: absolute; background: #0f0; width: 256px; height: 96px; overflow: scroll;">

<DIV style=" display: inline-block;position: relative;top: 64px; left: 32px;">
<DIV style="width: 18px; height: 14px; float: left; background: #f00;"></DIV>
<DIV style="float: left">First</DIV>
<div style="clear: both;"></div></DIV>

<DIV style=" display: inline-block;position: relative;top: 96px; left: 32px;">
<DIV style="width: 18px; height: 14px; float: left; background: #0f0;"></DIV>
<DIV style="float: left">Second</DIV><div style="clear: both;"></div></DIV>

</div>

</BODY>
</HTML>

第二个 div 没有定位在 32 x 位置,除非我删除了我需要的 display: inline-block 属性。有解决办法吗?

编辑:如果我删除 display: inline-block,它似乎可以工作,但是滚动条将水平显示(因为 div 占用了一些不可见的空间)。

最佳答案

position:relative 中,您应该使用 position:absolute 元素的显示方式。
此外,要避免水平滚动条,请使用overflow-y

工作示例:http://jsbin.com/uveni3

关于html - 内联 block 搞乱了相对定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2080956/

相关文章:

javascript - Selenium findElement 通过 CSS 和发送表单

javascript - HTML onload 不调用 JavaScript 函数

javascript - Fabric.js 便签类型文本换行

android - 如何以百分比设置对象的宽度、高度和边距?

javascript - 使用 css 自定义宽度

javascript - 如何本地存储展开/折叠 CSS 设置

php - 你如何在 MySQL 数据库字段中编写 HTML 并用 PHP 回应它?

html - 水平滚动条显示右侧有溢出内容但左侧没有?

css - 向下移动图像

html - 如何清除可变高度的 "position: fixed"元素的内容?