2010-01-17 55 views
2

我有以下代码:直列块弄乱与相对定位

<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×位置,除非删除了显示:内联块属性,该属性我需要。有没有解决的办法?

编辑:它似乎工作,如果我删除显示:内联块,但然后滚动条将水平出现(因为该div需要一些不可见的空间)。

回答

1

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

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

+0

尝试运行代码,您会看到两个元素,其中第二个* not *位于x:32;即使 - 尽管我把它放在那里。 – Tower 2010-01-17 12:48:55

+0

哦,但我做到了:http://jsbin.com/aqoju。你能展示一个图片或类似的网站到你想要达到的目标吗? – Kobi 2010-01-17 12:58:20

+0

是的,但我不能像这样使用绝对定位...尝试我在我的帖子中修改后的版本。 – Tower 2010-01-17 13:10:20

0

当使用 “inline-block的” 属性,你应该总是在DTD格式开始你的HTML开始标记。放置一个应该解决这个问题。