2011-02-05 121 views
4

我试图让div填充屏幕的某个部分,左边和右边都剩下412px。很简单,呃?100%宽度,填充和绝对定位

现在,当我添加位置:绝对&顶部:0,填充消失。元素检查器显示它为空 - “填充”保留,但宽度:100%消失。我用这个代码(不定位):

<div style="height:73px; padding-left:413px; padding-right:413px;"> 
    <div style="width:100%; height:73px; background:url(top-fill-small.png);"> 
    </div> 
</div> 

所以,我怎么能进行绝对定位它(我需要后来动画),同时保留填充?我非常喜欢你的帮助。提前致谢!

+0

在你的HTML你不应该混合CSS:http://phrogz.net/css/HowToDevelopWithCSS.html#separatestyle – Phrogz 2011-02-05 20:02:59

+0

谢谢,我知道了。这只是为了写作的速度。发布时,我会将其分开。再次感谢您的关注! – 2011-02-05 20:17:12

回答

5

Perhaps this?

<div style="width:100%; position:absolute; top:0; left:0"> 
    <div style="height:73px; background:url(top-fill-small.png); margin-left:413px; margin-right:413px"></div> 
</div> 
+0

不是,因为,正如我所说的,我需要它在左边和右边留下412px(在这种情况下)。这样...只是尝试一下,你会看到;)谢谢虽然:) – 2011-02-05 19:15:56

5

从你需要使用position:absolute;为什么不使用属性left & right而不是填充的时刻?

HTML

<div style="height:73px;position:absolute;left:413px;right:413px;"><div style="width:100%; height:73px; background:url(top-fill-small.png);border:1px solid red">Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content </div></div> 

例子:http://jsbin.com/anega3

编辑

的选择,如果你急需的使用填充可以设置left:0; right:0;然后已经在使用垫衬你有。

HTML

<div style="height:73px;position:absolute;left:0;right:0px;padding-left:413px;padding-right:413px;"><div style="width:100%; height:73px; background:url(top-fill-small.png);border:1px solid red">Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content </div></div> 

例子:http://jsbin.com/anega3/2