2017-06-29 100 views
2

我需要建立一些可重复使用的模板,理想情况下是使用优雅和简约的基于Flex的CSS。此模板适用于具有五个不同大小的“卡片”的仪表板。细胞2,3,5应该是相等的宽度,大概是四分之一宽度。剩下的就是细胞1,4。 编辑:高度方面,除了cell1之外的所有单元应该是彼此相同的高度,即低于单元1的高度。因此,整体效果是一张巨型卡,三张迷你卡以及一张宽而低卡。Flexbox:如何优化材料设计仪表板布局

https://jsfiddle.net/2jjuxdhw/8/

我想对齐内容:拉伸将意味着细胞就会垂直填补空间,但是这是行不通的。另外,我是否真的需要所有被称为行和列的部分?我想知道是否有一个更好的方法来做到这一点。谢谢你的帮助。

HTML

<div id="container"> 


     <div class="column fullheight" id="column2"> 

     <section class="row row1"> 

      <div class="cell" id="cell1"><div>cell1</div></div> 

      <section class="column"> 
      <div class="cell" id="cell2"><div>cell2</div></div> 

      <div class="cell" id="cell3"><div>cell3</div></div> 
      </section> 

     </section> 
     <section class="row row2"> 

      <div class="cell" id="cell4"><div>cell4</div></div> 

      <div class="cell" id="cell5"><div>cell5</div></div> 

     </section> 


     </div> 
</div> 

CSS

div#container { 
     position: relative; 
     height: 400px; 
     display: -webkit-flex; 
     display: flex; 
     flex-flow: row wrap; 
     border: 1px solid blue; 
     align-items: stretch; 
    } 


div#column2 { 
     flex-grow:3; 
     display: flex; 
     flex-direction: column; 
     height: 100%; 
     /*flex-wrap: wrap;*/ 
     align-content: stretch; 
     justify-content: flex-start; 
     align-items: stretch; 
    } 

    div#column2 section.row { 
     display: flex; 
     flex-direction: row; 
     /*align-content: stretch;*/ 
    } 

    div#column2 section.column { 
     display: flex; 
     flex-direction: column; 

    } 

    section.column, div.cell { flex: 1 0 auto; } 


    div.cell { 
     background-color: whitesmoke; 
     padding: 0.5em; 

    } 

    div#cell1,div#cell4 { padding-left: 1em; flex: 3 0 auto; } 
    div#cell1,div#cell2 { padding-top: 1em; } 
    div#cell2,div#cell3, div#cell5 { padding-right: 1em; } 

    div.cell > div { 
     flex: 1 1 auto; 
     background-color: white; 
     height: 100%; 
     width: 100%; 
     box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 6px, rgba(0, 0, 0, 0.12) 0px 1px 4px; 
    } 

https://jsfiddle.net/2jjuxdhw/8/

+0

没有阅读完整的内容,但是因为你在使用flexbox(和我一样)挣扎,我想我会在这里为你保留,希望它能帮助你,因为它帮助了我:[flexyboxes](http ://the-echoplex.net/flexyboxes/)。哦,你也应该把你的CSS/HTML代码变成一个可运行的Stack Snippet,这样人们可以更容易地重现你的问题。 – domsson

+0

https://css-tricks.com/snippets/css/a-guide-to-flexbox/是与flexbox相关的所有东西的绝佳资源。我正在查看您的代码并简化它。我注意到的一件事是有很多显示:flexbox;关于不需要它们的“孩子”元素。您必须了解,有一些Flexbox属性意在操纵父级,而另一些则意味着操作父级内具有显示的元素:flexbox –

回答

3

编辑

要回答你的问题更新,你可以采取这样的做法:

fiddle

.container { 
 
    height: 400px; 
 
    display: flex; 
 
    flex-direction: column; 
 
    border: 1px solid blue; 
 
} 
 

 
.row { 
 
    display: flex; 
 
} 
 

 
.row--top { 
 
    flex: 2; 
 
} 
 

 
.row--bottom { 
 
    flex: 1; 
 
} 
 

 
.cell-wrap { 
 
    width: 25%; 
 
    display: flex; 
 
    flex-direction: column; 
 
} 
 

 
.cell { 
 
    flex: 1; 
 
    background-color: white; 
 
    margin: .5em; 
 
    box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 6px, rgba(0, 0, 0, 0.12) 0px 1px 4px; 
 
}
<div class="container"> 
 
    <div class="row row--top"> 
 
    <div class="cell">cell one</div> 
 
    <div class="cell-wrap"> 
 
     <div class="cell">cell two</div> 
 
     <div class="cell">cell three</div> 
 
    </div> 
 
    </div> 
 
    <div class="row row--bottom"> 
 
    <div class="cell">cell four</div> 
 
    <div class="cell-wrap"> 
 
     <div class="cell">cell five</div> 
 
    </div> 
 
    </div> 
 
</div>

原来的答复

fiddle

.container { 
 
    height: 400px; 
 
    display: flex; 
 
    border: 1px solid blue; 
 
} 
 

 
.left { 
 
    flex: 1; 
 
    display: flex; 
 
    flex-direction: column; 
 
} 
 

 
.right { 
 
    min-width: 25%; 
 
    display: flex; 
 
    flex-direction: column; 
 
} 
 

 
.cell { 
 
    flex: 1; 
 
    background-color: white; 
 
    margin: .5em; 
 
    box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 6px, rgba(0, 0, 0, 0.12) 0px 1px 4px; 
 
}
<div class="container"> 
 
    <div class="left"> 
 
    <div class="cell">cell one</div> 
 
    <div class="cell">cell four</div> 
 
    </div> 
 
    <div class="right"> 
 
    <div class="cell">cell two</div> 
 
    <div class="cell">cell three</div> 
 
    <div class="cell">cell five</div> 
 
    </div> 
 
</div>

+0

感谢您的回答,并且您的代码非常精益!但我希望单元格4和5的高度相同。所以实际上细胞2,3,4,5都是相同的高度,而细胞4是宽的。我会更新我的问题以反映这一点。 – emma

+0

@emma道歉,我误解了你的问题。那么你原来的解决方案有什么问题?如果你能提供你想要的最终结果图片,可能会有所帮助,欢呼声 – sol

+0

我的感觉不够好 - 将它与你的第一个答案进行比较!我想知道是否有没有嵌套元素的方式。而且我也无法让它垂直拉伸,你已经完成了。谢谢 – emma