简体   繁体   English

使父容器采用弹性项目宽度

[英]Make parent container take flex items width

I have a list container with limited height.我有一个高度有限的列表容器。 It has list of some child items.它有一些子项的列表。 These items should be placed from top to bottom and from left to right.这些项目应从上到下、从左到右放置。 If there are more and more elements, they'll be placed in a new column on the right.如果元素越来越多,它们将被放置在右侧的新列中。

在此处输入图像描述

How can I make the parent container take the width from the content?如何让父容器从内容中获取宽度? So that the parent container doesn't expand to the full width of the window and hide the next columns?这样父容器就不会扩展到 window 的整个宽度并隐藏下一列?

What I tried: I tried to set different display of the parents, but the children from the second column get hidden, and the entire width of the .parent_item = the width of the first column only.我尝试了什么:我试图设置不同的父母显示,但第二列的孩子被隐藏,并且.parent_item的整个宽度 = 只有第一列的宽度。

Example:例子:

 .parent { box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); box-sizing: border-box; margin: auto; padding: 2px; height: 100%; max-height: 150px; max-width: 80vw; display: grid; grid-template-columns: 1fr 1fr; gap: 2px; }.parent_item { display: flex; flex-flow: column wrap; gap: 2px; max-width: max-content; overflow: auto; }.parent_item div { background-color: #f2f2f2; height: 30px; min-width: 30px; }
 <div class="parent"> <div class="parent_item"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> </div> <div class="parent_item"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> </div>

By using display contents you can make display flex ignore its children and make it flex the grand children, check the snippet for the result.通过使用显示内容,您可以使 display flex 忽略其子项并使其显示孙子项,检查代码段以获取结果。

 .parent { box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); box-sizing: border-box; margin: auto; padding: 2px; height: 100%; max-height: 150px; max-width: 80vw; gap: 3px; display: flex; flex-flow: column wrap; overflow: auto; }.grand-child { background-color: #f2f2f2; height: 30px; min-width: 30px; }.child { display: contents; }
 <div class="parent"> <div class="child"> <div class="grand-child">1</div> <div class="grand-child">2</div> <div class="grand-child">3</div> <div class="grand-child">4</div> <div class="grand-child">5</div> <div class="grand-child">6</div> <div class="grand-child">7</div> <div class="grand-child">8</div> <div class="grand-child">9</div> <div class="grand-child">10</div> <div class="grand-child">11</div> </div> <div class="child"> <div class="grand-child">1</div> <div class="grand-child">2</div> <div class="grand-child">3</div> <div class="grand-child">4</div> <div class="grand-child">5</div> <div class="grand-child">6</div> </div> </div>

To make the row cells start in a new row for another child use the following CSS styling;要使行单元格在另一个孩子的新行中开始,请使用以下 CSS 样式;

 .parent { box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); box-sizing: border-box; margin: auto; height: 100%; max-height: 150px; max-width: 80vw; display: flex; }.child { margin: 2px; flex: 1; gap: 3px; display: flex; flex-flow: column wrap; overflow: auto; }.grand-child { background-color: #f2f2f2; height: 30px; min-width: 30px; }
 <div class="parent"> <div class="child"> <div class="grand-child">1</div> <div class="grand-child">2</div> <div class="grand-child">3</div> <div class="grand-child">4</div> <div class="grand-child">5</div> <div class="grand-child">6</div> <div class="grand-child">7</div> <div class="grand-child">8</div> <div class="grand-child">9</div> <div class="grand-child">10</div> <div class="grand-child">11</div> </div> <div class="child"> <div class="grand-child">1</div> <div class="grand-child">2</div> <div class="grand-child">3</div> <div class="grand-child">4</div> <div class="grand-child">5</div> <div class="grand-child">6</div> </div> </div>

I hope this solved your issue, if it didn't let me know, and I will look into it some more.我希望这能解决您的问题,如果它没有让我知道,我会再调查一下。

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

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