简体   繁体   English

FlexBox - 如何显示一列 100% 和另外两列 50%

[英]FlexBox - How to display one column 100% and two others 50%

Looking at the bellow code, I'm trying to have the 3 child columns as follow:查看下面的代码,我试图让 3 个子列如下:

The first column should be 100% wide and above two other columns.第一列应为 100% 宽并高于其他两列。 Two other columns should be bellow the first column and each 50% wide.另外两列应位于第一列下方,每列宽 50%。

Like this:像这样: 在此处输入图像描述

 .flex-container { width: 80%; min-height: 300px; margin: 0 auto; display: flex; }.flex-container.column { padding: 10px; background: #dbdfe5; flex: 1; }.column.first { background: blueviolet; }.column.third { background: #b4bac0; }
 <div class="flex-container"> <div class="column first">Column 1</div> <div class="column second">Column 2</div> <div class="column third">Column 3</div> </div>

But whatever I try it doesn't work that way.但无论我尝试什么,它都不会那样工作。

Is it possible or I'm trying an impossible layout?有可能还是我正在尝试不可能的布局?

Flexbox version Flexbox版

You can use flex-wrap: wrap on the container to make children that overflow go below, and use flex-basis: 100% on the first child and flex-basis: 50% on the 2 others.您可以在容器上使用flex-wrap: wrap以使子元素溢出下面的 go,并在第一个子元素上使用flex-basis: 100%并在其他 2 个子元素上使用 flex- flex-basis: 50%

I have also added box-sizing: border-box on the children, to avoid border or padding count in the percentage.我还在孩子上添加了box-sizing: border-box ,以避免百分比中的边框或填充计数。

 .flex-container { width: 80%; min-height: 300px; margin: 0 auto; display: flex; flex-wrap: wrap; }.flex-container.column { padding: 10px; background: #dbdfe5; box-sizing: border-box; }.column.first { background: blueviolet; flex-basis: 100%; }.column.second { flex-basis: 50%; }.column.third { background: #b4bac0; flex-basis: 50%; }
 <div class="flex-container"> <div class="column first">Column 1</div> <div class="column second">Column 2</div> <div class="column third">Column 3</div> </div>

Grid version网格版

The grid version is even simpler, you only need display: grid on the container, and grid-column: 1 / 3 on the first child. grid 版本更简单,你只需要在容器上display: grid ,在第一个孩子上使用grid-column: 1 / 3

 .flex-container { width: 80%; min-height: 300px; margin: 0 auto; display: grid; }.flex-container.column { padding: 10px; background: #dbdfe5; }.column.first { background: blueviolet; grid-column: 1 / 3; }.column.third { background: #b4bac0; }
 <div class="flex-container"> <div class="column first">Column 1</div> <div class="column second">Column 2</div> <div class="column third">Column 3</div> </div>

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

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