简体   繁体   English

flex-direction 列属性不起作用

[英]flex-direction column property is not working

I have a ul tag with display: flex .我有一个带有display: flexul标签。

I need it ordered by column with flex-direction: column;我需要它按列排序,带有flex-direction: column; , but it does not work. ,但它不起作用。

CSS for the container: CSS 用于容器:

#nav li.four_columns ul.sub-menu {
  width: 600px;
  display: flex;
  -webkit-flex-direction: column;
  flex-direction: column;
  justify-content: flex-start;
  align-items: flex-start;
  flex-flow: wrap;
}

CSS for the child:供儿童使用的 CSS:

#nav li.four_columns ul.sub-menu li {
  flex-basis: 25%;
  /* white-space: nowrap; */
  /* overflow: hidden; */
  /* text-overflow: ellipsis; */
  /* border-bottom: none; */
}

Here is the source of your problem: flex-flow: wrap 以下是您的问题的根源: flex-flow: wrap

This is a shorthand property for flex-direction and/or flex-wrap . 这是flex-direction和/或flex-wrap的简写属性。

The initial values of this property are row nowrap . 此属性的初始值为row nowrap

You have only declared the flex-wrap component: wrap . 您只声明了flex-wrap组件: wrap

This means that the flex-direction component remains the default value: row . 这意味着flex-direction组件保持默认值: row

Hence, your flex items are aligning horizontally in a row that wraps. 因此,您的弹性项目在水平对齐的行中进行换行。

As a solution, either specify both components: 作为解决方案,要么指定两个组件:

flex-flow: column wrap

OR, since you already have flex-direction: column in the rule, remove flex-flow and use: 或者,因为您已经在规则中有flex-direction: column ,所以删除flex-flow并使用:

flex-wrap: wrap

Also important : If you want flex items to wrap in column-direction, you need to define a height on the container. 同样重要的是 :如果您希望弹性项目在列方向上换行,则需要在容器上定义高度。 Without a fixed height, the flex items don't know where to wrap and they'll stay in a single column, expanding the container as necessary. 如果没有固定的高度,flex项目不知道在哪里换行,它们将保留在单个列中,根据需要扩展容器。

Reference: 参考:

If flex direction column is not working, make sure you didn't forget to specify the height:如果 flex 方向列不起作用,请确保您没有忘记指定高度:

Example:例子:

#container {
    display: flex;
    width: 100%;
    height: 100%;
    flex-direction: column;
}

.child {
    width: 200px;
    /* height: 200px;*/
    flex: 1;
}

#child1 {
    background-color: red;
    flex: 2;
}

#child2 {
    background-color: green;
}

#child3 {
    background-color: blue;
}

<section id="container">
    <div id="child1" class="child"></div>
    <div id="child2" class="child"></div>
    <div id="child3" class="child"></div>
</section>

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

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