【问题标题】:100% width, padding and absolute positioning100% 宽度、填充和绝对定位
【发布时间】:2011-06-21 23:45:20
【问题描述】:

我试图让一个 div 填充屏幕的某个部分,在左侧和右侧都留出 412px 空白。简单吧?

现在,当我添加 position:absolute & top:0 时,填充消失了。元素检查器显示它是空的 - “填充”保持不变,但 width: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>

那么,如何在保留填充的同时绝对定位它(稍后我需要动画)?我非常喜欢你的帮助。提前致谢!

【问题讨论】:

标签: html css padding css-position


【解决方案1】:

从您需要使用position:absolute; 的那一刻起,为什么不使用属性leftright 而不是填充?

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

【讨论】:

  • 等等,每次我都用了,只有一个有效?哇谢谢。我不认为我是那个nooby ;)
  • @Dwarf Vader:当您同时使用leftright 时,两者都可以工作。
【解决方案2】:

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>

【讨论】:

  • 不,因为,正如我所说,我需要它在左边和右边都留下 412px(在这种情况下)。这样......试试看,你会看到;)谢谢 :)
  • 那我还不明白你想要什么。 It does leave 413px (412?) 左右。是否有您希望 div 具有的最小宽度?
  • 哦..对不起,可能在回复时,我回答了错误的评论。这行得通。非常感谢,谢谢!
  • 注意,当你把窗口设置得太窄时,我和Sotiris的解决方案都会导致div没有宽度。
  • 谢谢,我想这是一个公平的缺点,看看我打算做什么。另外,可能会发生类似于 dlobal div 容器的 min-width ;)
猜你喜欢
  • 2011-12-20
  • 1970-01-01
  • 1970-01-01
  • 2015-05-21
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多