【发布时间】:2017-05-22 13:31:56
【问题描述】:
我觉得问这个有点傻,但是我已经用尽了我对 Flexbox 的了解,所以我希望也许其他人可以进来帮助我。
我的总体目标是让中间行中的两个项目拉伸以填充标题和项目之间的空间,我已经四处搜索,老实说无法弄清楚我应该做什么。我从最底部的CSS Tricks Guide 分叉了代码,并且我做了一些改动。我目前拥有的代码是(以全屏模式打开以使其更清晰):
body,
html {
height: 100%;
}
.wrapper {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
justify-content: flex-start;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
height: 100%;
font-weight: bold;
text-align: center;
}
.wrapper > * {
padding: 10px;
flex: 1 1 100%;
}
.header {
background: tomato;
height: 50px;
flex: 1 1 100%;
}
.footer {
background: lightgreen;
height: 50px;
}
.main {
text-align: left;
align-self: stretch;
background: deepskyblue;
}
.aside-1 {
background: gold;
}
.aside-2 {
background: hotpink;
}
@media all and (min-width: 600px) {
.aside {
flex: 1 auto;
}
}
@media all and (min-width: 800px) {
.main {
flex: 3 0px;
}
.aside-1 {
order: 1;
}
.main {
order: 2;
}
.aside-2 {
order: 3;
}
.footer {
order: 4;
}
}
body {
padding: 2em;
}
<div class="wrapper">
<header class="header">Header</header>
<article class="main">
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est.
Mauris placerat eleifend leo.</p>
</article>
<aside class="aside aside-1">Aside 1</aside>
<footer class="footer">Footer</footer>
</div>
是否有可能在 flexbox 中实现这一目标无需更改 HTML,还是我应该寻找另一种方式来实现这一目标?
【问题讨论】:
-
@Ced 这是一个非常接近的副本,尽管微不足道的区别可以轻松实现,只需稍加思考即可。
-
@Jhecht 谢谢你的问题,我觉得这很好,这就是为什么当有人说这是骗子时我在评论部分发疯的原因。请检查我的答案,我花了很长时间。
-
据我所知,否,如果不更改标记是不可能得到这个结果的
-
@vals 由于页眉和页脚的高度是固定的,所以它是可行的 :) .... 可行!= 推荐
-
@LGSon 我的意思是,让 flex 做布局......当然你总是可以自己计算高度(如 kukkuz 回答,或你自己)