行 flexbox 内的嵌套列 flexbox 带包装 [英] Nested column flexbox inside row flexbox with wrapping

查看:24
本文介绍了行 flexbox 内的嵌套列 flexbox 带包装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个带有嵌套 flex 框的页面:http://jsfiddle.net/fr0/6dqLh30d/

<ul class="flex-container red"><li class="flex-item">1</li><li class="flex-item">2</li><li class="flex-item">3</li><ul class="flex-container gold"><li class="flex-item">1</li><li class="flex-item">2</li><li class="flex-item">3</li><li class="flex-item">4</li><li class="flex-item">5</li><ul class="flex-container blue"><li class="flex-item">1</li><li class="flex-item">2</li><li class="flex-item">3</li><li class="flex-item">4</li><li class="flex-item">5</li><li class="flex-item">6</li><li class="flex-item">7</li><li class="flex-item">8</li><div>

和(相关的)CSS:

 .flex-group {显示:弹性;弹性方向:行;高度:500px;}.flex 容器 {填充:0;边距:0;列表样式:无;边框:1px纯银;显示:弹性;弹性方向:列;flex-wrap: 包裹;弹性:0 0 自动;}.flex-item {填充:5px;宽度:100px;高度:100px;边距:10px;行高:100px;白颜色;字体粗细:粗体;字体大小:2em;文本对齐:居中;}

外部 flexbox (.flex-group) 是从左到右布局的.内部 flexboxes (.flex-container) 旨在从上到下布局,如果没有足够的空间(在我的 jsfiddle 中没有),应该换行.我想要发生的是,当包装发生时 .flex-containers 将在 X 方向上增长.但这并没有发生.相反,容器正在溢出.

我希望它看起来像什么(在 Chrome 中):https://dl.dropboxusercontent.com/u/57880242/flex-good.png

有没有办法让 .flex-container 在 X 方向上适当地调整大小?我不想硬编码它们的宽度,因为出现在这些列表中的项目是动态的.

解决方案

我已经玩了几分钟了,我想我已经得到了你要找的东西.

这是CSS

.flex-group {保证金:自动;显示:弹性;弹性方向:行;justify-content:空间环绕;}.flex 容器 {弹性:0 1 自动;显示:弹性;flex-flow:行包装;align-items: space-around;对齐内容:flex-start;填充:0;边距:0;列表样式:无;边框:1px纯银;}.红李{背景:红色;}.gold li {背景:黄金;}.blue li {背景:深蓝;}.flex-item {弹性:0 1 自动;填充:5px;宽度:100px;高度:100px;边距:10px;行高:100px;白颜色;字体粗细:粗体;字体大小:2em;文本对齐:居中;}

这里有一个更新的小提琴,可以看到它的实际效果.

I have a page with nested flex boxes here: http://jsfiddle.net/fr0/6dqLh30d/

<div class="flex-group">
  <ul class="flex-container red">
    <li class="flex-item">1</li>
    <li class="flex-item">2</li>
    <li class="flex-item">3</li>
  </ul>

  <ul class="flex-container gold">
    <li class="flex-item">1</li>
    <li class="flex-item">2</li>
    <li class="flex-item">3</li>
    <li class="flex-item">4</li>
    <li class="flex-item">5</li>
  </ul>

  <ul class="flex-container blue">
    <li class="flex-item">1</li>
    <li class="flex-item">2</li>
    <li class="flex-item">3</li>
    <li class="flex-item">4</li>
    <li class="flex-item">5</li>
    <li class="flex-item">6</li>
    <li class="flex-item">7</li>
    <li class="flex-item">8</li>
  </ul>
<div>

And the (relevant) CSS:

 .flex-group {
   display: flex;
   flex-direction: row;
   height: 500px;
 }

 .flex-container {
   padding: 0;
   margin: 0;
   list-style: none;
   border: 1px solid silver;
   display: flex;
   flex-direction: column;
   flex-wrap: wrap;
   flex: 0 0 auto;
 }

 .flex-item {
   padding: 5px;
   width: 100px;
   height: 100px;
   margin: 10px;

   line-height: 100px;
   color: white;
   font-weight: bold;
   font-size: 2em;
   text-align: center;
 }

The outer flexbox (.flex-group) is meant to lay out from left-to-right. The inner flexboxes (.flex-container) are meant to layout from top-to-bottom, and should wrap if there isn't enough space (which there isn't in my jsfiddle). What I want to happen, is that the .flex-containers will grow in the X direction when the wrapping happens. But that's not occurring. Instead, the containers are overflowing.

What I want it to look like (in Chrome): https://dl.dropboxusercontent.com/u/57880242/flex-good.png

Is there a way to get the .flex-containers to size appropriately in the X direction? I don't want to hard-code their widths, since the items that appear in these lists will be dynamic.

解决方案

I've been playing with this for a few minutes now, and I think I've got what you're looking for.

Here's the CSS

.flex-group {
    margin: auto;
    display: flex;
    flex-direction: row;
    justify-content: space-around;
}

.flex-container {
    flex: 0 1 auto;

    display: flex;
    flex-flow: row wrap;
    align-items: space-around;
    align-content: flex-start;

    padding: 0;
    margin: 0;
    list-style: none;
    border: 1px solid silver;
}

.red li {
    background: red;
}

.gold li {
    background: gold;
}

.blue li {
    background: deepskyblue;
}

.flex-item {
    flex: 0 1 auto;

    padding: 5px;
    width: 100px;
    height: 100px;
    margin: 10px;

    line-height: 100px;
    color: white;
    font-weight: bold;
    font-size: 2em;
    text-align: center;
}

And here's an updated fiddle to see it in action.

这篇关于行 flexbox 内的嵌套列 flexbox 带包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆