【问题标题】:How to make Div expand width with 100%如何使Div扩展宽度为100%
【发布时间】:2014-09-11 01:02:22
【问题描述】:

我已经制作了一个容器并将其设置为中间宽度边距:0 auto,但我遇到了一个问题,我想让某些内容宽度适合 100% 宽度的窗口宽度?

有什么办法吗?

如果我使用 padding:0 400px 和 margn-left:-200 ?

这是我的工作代码。

CSS

html, body {width:100%; height:100%; background:lightblue}
* { margin:0; padding:0}
#container { 
  width:1024px;
  min-height:100%;
  background:red;
  margin:0 auto;
  position:relative;
 }

header {
   width:100%;
  height:80px;
  background:skyblue;
}

header ul {list-style-type:none; float:right}
header ul li {display:inline-block; padding: 10px; 20px;}

#content {
  width:100%;
  height:200px;
  background:yellow;
  padding:0 300px;
  margin-left:-200px;
}

http://codepen.io/jaminpie/pen/CHrIf

谢谢你的帮助..

【问题讨论】:

    标签: html css


    【解决方案1】:

    将内容 div 移出容器,然后将其绝对定位到距顶部 80px 的位置,使其位于标题的正下方。

    #content {
      width:100%;
      height:200px;
      background:yellow;
      padding:0 300px;
      position:absolute;
      top:80px;
    }
    

    Updated Pen

    【讨论】:

    • 嗨,感谢您的帮助和代码,但是如果我有一个粘滞的页脚怎么办。如果我使用绝对页脚不会帮助我推到底部?
    • 只需在页脚处添加position:absolute;bottom:0;
    【解决方案2】:

    对于宽度为 100% 的窗口宽度

    DEMO

    使用1/100th of the width of the viewport.

      width:100vw;
    

    box-sizing

    完整代码:

    * { box-sizing:border-box;margin:0; padding:0}
    
    html, body {width:100vw; height:100vh; background:lightblue}
    
    #container { 
      width:100%;
      min-height:100%;
    
      background:red;
      margin:0 auto;
      position:relative;
     }
    
    header {
       width:100%;
      height:80px;
      background:skyblue;
    }
    
    header ul {list-style-type:none; float:right}
    header ul li {display:inline-block; padding: 10px; 20px;}
    
    #content {
      width:100vw;
      height:200px;
      background:yellow;
      padding:0 300px;
      margin-left:-200px;
    }
    

    【讨论】:

    • 但这仍然需要margin-left: -200px right ??
    • 不,如果您的计划是显示之前存在的完整 div,则不需要它
    猜你喜欢
    • 1970-01-01
    • 2012-12-22
    • 2012-07-13
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    相关资源
    最近更新 更多