简体   繁体   English

将最后一个元素移动到Flex容器中的下一行

[英]Move last element to next line in a flex container

I have this structure in my HTML code: 我的HTML代码中有这个结构:

<div style="display:flex">
    <div class="div_first">1</div>
    <div class="div_second">2</div>
    <div class="div_third">3</div>
    <div class="div_next_line">
      <div class="div_first">4</div>
      <div class="div_second">5</div>
      <div class="div_third">6</div>
    </div>
</div>

Is it possible to have 4 5 6 in second line with the same columns as 1 2 3, like so: 第二行中是否有4 5 6与1 2 3相同的列,如下所示:

1    2    3
4    5    6

The structure of the HTML code cannot be changed. HTML代码的结构无法更改。

Apply flex-wrap to the container. flex-wrap应用于容器。

Apply flex: 1 0 33% to the first three child divs. flex: 1 0 33%应用于前三个子div。

To the fourth div apply flex-basis: 100%; display: flex 到第四个div应用flex-basis: 100%; display: flex flex-basis: 100%; display: flex . flex-basis: 100%; display: flex

To the children of the fourth div apply flex: 1 0 33% . 对第四个div的孩子申请flex: 1 0 33%

The idea is to force the fourth item to wrap, then have its children replicate the behavior of the children in the primary container. 想法是强制第四个项目换行,然后让它的子项复制主容器中子项的行为。

Use flex-wrap for the parent and flex-basis for the children: 对子项使用flex-wrapflex-basis

CSS: CSS:

.parent {
    flex-wrap: wrap;
    width: 400px;
    border: 2px solid blue;
}
.parent > .div_next_line {
    display: flex;
}
.parent > div {
    display: inline-block;
    flex-grow: 1;
    flex-basis: calc(100% / 3);
}
.parent > div > div {
    display: inline-block;
    flex-grow: 1;
    flex-basis: calc(100% / 3);
}

JSFiddle: https://jsfiddle.net/ghjbhjLu/1/ JSFiddle: https ://jsfiddle.net/ghjbhjLu/1/

References: 参考文献:

CSS Tricks | CSS技巧| Flex 柔性

Force n items, SO 强制n项,SO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM