简体   繁体   English

使弹性项目占据溢出容器的整个高度

[英]Make flex items take full height of overflowing container

In the example (link to jsfiddle at the end) I want to to在示例中(最后链接到 jsfiddle)我想

  1. make all columns to have the same width, but if the container overflows and a scrollbar appears, then all the columns still remain the same except for the last, this one should shrink, eg container width is 600px, every column of the six columns has width 100px - when scrollbar is shown, columns 1 - 5 have width 100px, last one 100px - scrollbar-width.使所有列具有相同的宽度,但是如果容器溢出并出现滚动条,那么除了最后一列之外所有列仍然保持不变,这一列应该缩小,例如容器宽度为600px,六列的每一列都有宽度 100px - 显示滚动条时,第 1 - 5 列的宽度为 100 像素,最后一列的宽度为 100 像素 - 滚动条宽度。 I achieved this by setting the width in px, but i wonder, if there is a more elegant way.我通过以 px 为单位设置宽度来实现这一点,但我想知道是否有更优雅的方式。
  2. make the container having a fixed height showing overflow via scroll, but have the children grow also to the full height of the container.使容器具有固定高度,通过滚动显示溢出,但让孩子也长到容器的整个高度。 In the jsfiddle-example the second, gray column should be the same height as the yellow container.在 jsfiddle-example 中,第二个灰色列应该与黄色容器高度相同。 I tried to set justify-content/items and align-content/items to stretch but this doesnt work.我试图将justify-content/itemsalign-content/itemsstretch但这不起作用。

Here is a link to the example in jsfiddle.这里是一个链接到的jsfiddle的例子。

 .outer_container { width: 100%; background-color: yellow; display: flex; height: 200px; overflow: auto; justify-content: stretch; align-items: stretch; justify-items: stretch; align-content: stretch; } .column { width: 199px; border-left: 1px solid white; flex-shrink: 0; } .column.last { flex-grow: 1; flex-shrink: 1; width: unset; } .column_item { height: 44px; background-color: blue; border-top: 1px solid red; }
 <div class="outer_container"> <div class="column"> <div class="column_item"> </div> <div class="column_item"> </div> <div class="column_item"> </div> <div class="column_item"> </div> <div class="column_item"> </div> <div class="column_item"> </div> </div> <div class="column" style="background-color: gray;"> <div class="column_item"> </div> </div> <div class="column"> <div class="column_item"> </div> </div> <div class="column"> <div class="column_item"> </div> </div> <div class="column"> <div class="column_item"> </div> </div> <div class="column last"> <div class="column_item"> </div> </div> </div>

For your problem No.1 Add flex-basis of desired width to the last column.对于您的问题 No.1 将所需宽度的 flex-basis 添加到最后一列。

.column.last {
  flex-basis: 100px;
  flex-grow: 1;
  flex-shrink: 1;
}

If we use flex-basis, it will overwrite width but if we use max-width, max-width will overwrite flex-basis but in that case flex-grow won't work.如果我们使用 flex-basis,它将覆盖宽度,但如果我们使用 max-width,max-width 将覆盖 flex-basis,但在这种情况下 flex-grow 将不起作用。 If you don't want the last column to grow, you can use max-width instead of flex-basis.如果您不希望最后一列增长,您可以使用 max-width 而不是 flex-basis。

Problem No.2问题二

Use viewport height(vh) instead of px for defining the height of the class "column-item".使用视口 height(vh) 而不是 px 来定义“column-item”类的高度。 if you want a constant height for grey column, put an additional "column_item-2" inside "column" and give it a width of 100% and height of your desire.如果您想要灰色列的恒定高度,请在“列”内添加一个额外的“column_item-2”,并为其提供 100% 的宽度和您想要的高度。

  <div class="column">
    <div class="column_item"></div>
    <div class="column_item-2"></div>
  </div>

.column_item-2{
  width: 100%;
  height: 150px;
  background: gray;
}

Hope it is helpful :)希望它有帮助:)

黄色帽子和灰色帽子应该排成一排吗?

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

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