我想知道是否可以将柔性物品水平对齐到同一容器中较大的柔性物品.使用CSS浮动它很容易实现,但我无法使用flex项目完成它.
View this JSFiddle for a flexbox example.
View this JSFiddle a float example
网格布局
<div class="flex-container"> <div class="large-flex-item"> </div> <div class="small-flex-item"> </div> <div class="small-flex-item"> </div> </div>
CSS
.flex-container { display: flex; flex-direction: row; flex-wrap: wrap; align-items: flex-start; max-width: 500px; margin-right: auto; margin-left: auto; } .small-flex-item { flex-basis: 33.333%; background: #000; padding-top: 30%; } .large-flex-item { flex-basis: 66.667%; background: #333; padding-top: 60%; }
解决方法
您可以通过嵌套Flex容器来实现布局.
HTML
<div class="flex-container"> <div class="large-flex-item"></div><!-- flex item #1 --> <div class="flex-container-inner"><!-- flex item #2 & nested flex container --> <div class="small-flex-item"></div><!-- this flex item and sibling will align... --> <div class="small-flex-item"></div><!-- ... in column next to .large-flex-item --> </div> </div>
CSS
.flex-container { display: flex; width: 500px; margin-right: auto; margin-left: auto; } .large-flex-item { flex-basis: 66.667%; height: 200px; background: #333; } .flex-container-inner { display: flex; flex-direction: column; flex-grow: 1; } .small-flex-item { flex-basis: 100%; height: 100px; background: #000; }