如何使用flexbox进行此布局?

时间:2016-10-18 16:30:26

标签: css web flexbox

我已经找到了一个包含几个区域的表单,而且我一直试图弄清楚如何让flexbox像这样放置一些东西:

Desired Design

如果可能的话,如何在使用最少量的父容器时执行此操作? (或者,为什么我不想这样做?)

由于没有实现这一目标,我认为要求是正确的举动。我仍然围绕着这一切。

2 个答案:

答案 0 :(得分:1)



.wrapper {
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;  
  
  -webkit-flex-flow: row wrap;
  flex-flow: row wrap;
  
  font-weight: bold;
  text-align: center;
}

.wrapper > * {
  padding: 10px;
  flex: 1 100%;
}

.header {
  background: tomato;
}

.footer {
  background: lightgreen;
}

.main {
  text-align: left;
  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>
  <aside class="aside aside-1">Aside 1</aside>
  <aside class="aside aside-2">Aside 2</aside>
  <footer class="footer">Footer</footer>
</div>
&#13;
&#13;
&#13;

从找到的示例here修改。完全归功于css-tricks。

编辑:强烈推荐css-tricks文章。适用于所有Flexbox的非常有用的资源

答案 1 :(得分:0)

您可以使用一些CSS行从body构建此布局:

&#13;
&#13;
html,
body {
  height: 100%;/* or 100vw just for body */
  margin:0 /* reset */
}

body,
section {
  display: flex;
}
/* eventually : section {overflow:auto} if you want to keep footer down the screen no matter how much content */
body {
  flex-flow: column;
}
section,
article {
  flex: 1;/* use whole space avalaible if only child or share it evenly when multiple children */
}
/* add borders to see elements */
header,
footer,
article {
  border: solid;
  padding: 1em;
}

/* break point  without mediaqueries ? 
uncomment this below */
/* article {
  min-width:320px;/* 2 articles make break point at average 640px */
}*/
&#13;
<header>
  header any height
</header>
<section>
  <article>Side</article>
  <article>Side</article>
</section>
<footer>
  footer any height
</footer>
&#13;
&#13;
&#13;

http://codepen.io/gc-nomade/pen/WGazGX与(or download code samples

一起玩
相关问题