简体   繁体   English

100%宽度,填充和绝对定位

[英]100% width, padding and absolute positioning

I was trying to make a div fill a certain part of the screen, leaving 412px empty both to the left and to the right. 我试图在屏幕的某个部分进行div填充,将412px留空到左侧和右侧。 Easy, eh? 容易,嗯?

Now, when I add position:absolute & top:0, the fill disappears. 现在,当我添加position:absolute&top:0时,填充消失。 The element inspector shows it's empty - the "paddings" stay, but the width:100% disappears. 元素检查器显示它是空的 - “填充”保持不变,但宽度:100%消失。 I've used this code (without the positioning): 我用过这段代码(没有定位):

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

So, how can I position it absolutely (I need it animated later), while preserving the padding? 那么,我怎么能绝对定位它(我需要稍后动画),同时保留填充? I'd love your help very much. 我非常爱你的帮助。 Thanks in advance! 提前致谢!

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>

from the moment you need to use position:absolute; 从你需要使用position:absolute;的那一刻起position:absolute; why not use the properties left & right instead of padding? 为什么不使用属性leftright的,而不是填充?

html 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>

Example: http://jsbin.com/anega3 示例: http//jsbin.com/anega3

edit 编辑

an alternative if you need badly to use padding you can set left:0; right:0; 如果你需要严格使用填充,你可以设置left:0; right:0; left:0; right:0; and then use the paddings you already have. 然后使用你已经拥有的填充。

html 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>

Example: http://jsbin.com/anega3/2 示例: http//jsbin.com/anega3/2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM