【发布时间】:2016-06-15 11:19:22
【问题描述】:
我尝试将 100% 的父级分配给固定 div,但占用了 100% 的屏幕。
我在这里准备了代码 http://codepen.io/rodboc/pen/ZOOEWp
HTML
<div id="wrapper">
<div class="box">
<div class="header">
<p>Header</p>
</div>
<div class="content">
<p>Content here Lorem Ipsum</p>
<p>Content here Lorem Ipsum</p>
<p>Content here Lorem Ipsum</p>
<p>Content here Lorem Ipsum</p>
<p>Content here Lorem Ipsum</p>
<p>Content here Lorem Ipsum</p>
<p>Content here Lorem Ipsum</p>
<p>Content here Lorem Ipsum</p>
<p>Content here Lorem Ipsum</p>
<p>Content here Lorem Ipsum</p>
<p>Content here Lorem Ipsum</p>
<p>Content here Lorem Ipsum</p>
<p>Content here Lorem Ipsum</p>
<p>Content here Lorem Ipsum</p>
<p>Content here Lorem Ipsum</p>
<p>Content here Lorem Ipsum</p>
<p>Content here Lorem Ipsum</p>
<p>Content here Lorem Ipsum</p>
</div>
</div>
</div>
CSS
#wrapper {
width: calc(100% / 3);
height: 900px;
background: #ecf0f1;
margin: 0 auto;
padding: 10px;
}
#wrapper .box {
width: 100%;
}
#wrapper .header {
width: inherit;
position: fixed;
background: #2ecc71;
}
#wrapper .content {
width: inherit;
background: #27ae60;
}
如果在 px 中定义父级的宽度有效,但我不能这样做,则应为 100%
【问题讨论】:
-
@MarcoValente 谢谢,但你提到的问题是固定宽度,在这种情况下是 %
-
@rodboc,兄弟,看看我的回答,你可能会喜欢它
-
它是继承它上面容器的宽度,也就是100%;这是您的包装器将 .box 推得更小。你把你的 div 顺序搞混了。使用基本网格可以解决所有这些问题。 Google Kindle 网格。