简体   繁体   English

当flex-direction为'column'时,CSS flex-basis无法正常工作

[英]CSS flex-basis not working when flex-direction is 'column'

My goal is to create a two-column layout with flexbox wherein the first column has two rows and the second column has one, like this: 我的目标是使用flexbox创建一个双列布局,其中第一列有两行,第二列有一行,如下所示:

在此输入图像描述

Setting flex-basis: 100% on the third item gives the desired effect, but only when the container's flex-direction is row : 设置flex-basis: 100%第三项上的flex-basis: 100%给出了所需的效果,但仅当容器的flex-directionrow

在此输入图像描述

Changing flex-direction to column results in the following, unless the height is set explicitly, which is infeasible in my project: flex-direction更改为column导致以下结果,除非明确设置height ,这在我的项目中是不可行的:

在此输入图像描述

How can I get the first image without explicitly setting the height of the container? 如何在不明确设置容器height的情况下获取第一张图像?

Here's a Plunker illustrating the problem . 这是一个说明问题的Plunker

 body { display: flex; height: 100px; width: 100px; } .container { display: flex; flex-direction: column; /* Setting this to `row` gives the expected effect,but rotated */ flex-grow: 1; flex-wrap: wrap; /* height: 100%; */ /* Setting this fixes the problem, but is infeasible for my project*/ } .item { flex-grow: 1; } .one { background-color: red; } .two { background-color: blue; } .three { background-color: green; flex-basis: 100%; } 
 <div class="container"> <div class="item one">&nbsp;</div> <div class="item two">&nbsp;</div> <div class="item three">&nbsp;</div> </div> 

Why it doesn't work : 为什么它不起作用

Flex wrap only happens when the initial main size of the flex items overflows the flex container. Flex包装仅在Flex项目的初始主要大小溢出Flex容器时发生。 In the case of flex-direction: row , this is when they flex items are wider than the container. flex-direction: row的情况下,这是当它们的flex项比容器宽时。 With flex-direction: column , they must overflow the height of the container. 对于flex-direction: column ,它们必须溢出容器的高度。

But in CSS, height behaves fundamentally differently than width. 但在CSS中,高度的表现与宽度基本不同。 The height of a container in CSS is determined by the height of its children. CSS中容器的高度由其子项的高度决定。 Thus, without something constraining the container's height, they flex items will never overflow; 因此,如果没有限制容器高度的东西 ,它们的柔性物品永远不会溢出; they just make their container taller. 他们只是让他们的容器更高。 If they don't overflow, they won't wrap. 如果它们没有溢出,它们将不会包裹。

Possible solutions : 可能的方法

You have to constrain the height of the container. 您必须约束容器的高度。 The obvious way (which you said is off the table), is setting an explicit height or max-height . 显而易见的方式(你说的不在桌面上),是设置一个明确的heightmax-height

Ask yourself why you need to constrain the height. 问问自己为什么需要限制高度。 Is it to stay within the viewport? 它是否留在视口内? You could use viewport-relative units and set something like max-height: 100vh . 您可以使用视口相对单位并设置类似max-height: 100vh Is it to equal the height of another column outside this flex container? 它是否等于此flex容器外的另一列的高度? You might be able to constrain it with another flexbox. 您可以使用另一个Flexbox约束它。 Make this flex container a flex item in an outer flexbox. 使此Flex容器成为外部Flexbox中的flex项。 Something else in that outer flexbox would have to establish its height, though. 但是,外部flexbox中的其他东西必须确定其高度。

Try to set flex: none for blocks inside the container in the condition when you change the orientation ( @media etc.) 在更改方向时,尝试为容器内的块设置flex: none@media等)

I tested it in Chrome and Firefox. 我在Chrome和Firefox中测试过它。

As far as i understand, the following code give the result you want: 据我所知,以下代码给出了你想要的结果:

body {
  display: flex;
  height: 100px;
  width: 100px;
}

.container {
  display: flex;
  flex-direction: column; 
  flex-wrap: wrap;
  flex-grow:1;
}

.item {
  flex-basis:50%;
}

.one {
  background-color: red;
}

.two {
  background-color: blue;

}

.three {
  background-color: green;
  flex-basis:100%;
}

Except from the flex-basis:50%; 除了flex-basis:50%; this code is equal to the code you use in the plunker. 此代码等于您在plunker中使用的代码。 For some reason the plunker does not support flex-basis , see also: 由于某些原因,plunker不支持flex-basis ,另见:

在此输入图像描述

I think you should build your layout using a Grid System in mind. 我认为您应该使用网格系统来构建您的布局。 On your first picture we can see 2 cols: first col is red and blue boxes, second col is the green box. 在你的第一张照片上,我们可以看到2个cols:第一个col是红色和蓝色框,第二个col是绿色框。

And then the structure should be like this: 然后结构应该是这样的:

<div class="flex-container">
    <div class="col column">
        <div class="content red"></div>
        <div class="content blue"></div>
    </div>
    <div class="col">
        <div class="content green"></div>
    </div>
</div>

Using these styles: 使用这些样式:

.flex-container {
   display:flex;
}
.col {
   flex:1;
   display:flex;
}
.column {
   flex-direction:column;
}
.content {
   flex:1;
}

Here is a quick code using your demo goal: https://jsfiddle.net/wyyLvymL/ Hope it helps! 以下是使用您的演示目标的快速代码: https//jsfiddle.net/wyyLvymL/希望它有所帮助! (if you need to understand how does it works, ping me) (如果你需要了解它是如何工作的,请ping我)

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

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