简体   繁体   English

Flexbox 未按预期包装项目

[英]Flexbox not wrapping items as expected

I need to create a drop down navigation menu which wraps onto two lines when it is really long.我需要创建一个下拉导航菜单,当它真的很长时,它会包装成两行。

By setting the following CSS properties on the menu I can achieve the desired result.通过在菜单上设置以下 CSS 属性,我可以获得所需的结果。

.dynamic {
  position: absolute;
  max-height: 80px;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
}

This, however, does not work on Internet Explorer 11. The items do not wrap onto the next line as it does on Chrome.但是,这在 Internet Explorer 11 上不起作用。这些项目不会像在 Chrome 上那样换行到下一行。

Here is a jsFiddle这是一个jsFiddle

It will work if I use height: 80px instead of max-height: 80px;如果我使用height: 80px而不是max-height: 80px;它会起作用max-height: 80px; but that does not work for me as I want the menu to grow with the items.但这对我不起作用,因为我希望菜单随项目一起增长。

Does anyone know how I can get IE to wrap the items properly?有谁知道如何让 IE 正确包装物品?

Since CSS Flexbox is not fully supported to provided a wrapping menu when the items pass a maximum height I have created a workaround using the height attribute.由于不完全支持 CSS Flexbox 在项目超过最大高度时提供环绕菜单,因此我使用height属性创建了一个解决方法。

As @Michael_B pointed out , the container doesn't wrap around the wrapped items.正如@Michael_B指出的那样,容器不会包裹包裹的物品。

A solution to this is apply the background style to the flex items instead of the container like this对此的解决方案是将背景样式应用于 flex 项目而不是像这样的容器

This allows the container 'to appear' to grow with the items.这允许容器“出现”与项目一起增长。 Then using the nth-child pseudos I can allow the last item to span the full height of the container.然后使用第nth-child伪项,我可以允许最后一个项目跨越容器的整个高度。

.dynamic > li:nth-child(4n),
.dynamic > li:last-child:nth-child(n+4) {
    flex: 1 0 auto;
}

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

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