简体   繁体   English

容器中子元素的可调宽度

[英]Adjustable width of child elements in container

There is parent container with child elements: 有带有子元素的父容器:

 .item { min-width: 156px; max-width: 188px; width: auto; float: left; } .container { display: inline; } 
 <div class="container"> <div class="item"> </div> <div class="item"></div> <div class="item"></div> </div> 

I need to put all items one by one. 我需要一一放所有物品。 And they have to have max available width (in specified range) when container is resized. 调整容器大小时,它们必须具有最大可用宽度(在指定范围内)。 What might be solution? 可能是什么解决方案? flexbox is not an option. 不能选择flexbox Also items are generated by Angular and can be any number. 项目也是由Angular生成的,可以是任意数量。 I have attahced screen. 我已附加屏幕。 There is empty place on the right side (marked with red) since I have changed windows size. 由于我更改了窗口大小,因此右侧有一个空白处(标有红色)。 But it hasn't be. 但事实并非如此。 There are two options. 有两种选择。 1) New item is added if thre is enough room 2) width of all items are correlated so that there is no empty space. 1)如果有足够的空间,则添加新项目。2)所有项目的宽度都相互关联,因此没有空白空间。

https://i.stack.imgur.com/Oqpxk.png https://i.stack.imgur.com/Oqpxk.png

<div class="container">
 <div class="item"></div>
 <div class="item"></div>
 <div class="item"></div>
</div>

just change the css to be like that 只需将css更改为那样

.item {
  min-width: 33.33%;
  float: left;
}

.container {
  min-width:468px;
  display: inline;
}

Please be more specific when adding questions on SO. 在添加有关SO的问题时,请更加具体。 What do you want to achieve? 您想实现什么? What are your limitations and dependencies. 您的局限性和依赖性是什么? Why is flexbox no option? 为什么flexbox没有选择? (Just remember it for the next time) (请记住下一次)

  1. Remove display:inline from the container! 从容器中移除display:inline
  2. Create the desired amount of columns (in this example) three. 创建所需数量的列(在此示例中)为三个。
  3. Create elements ( .box ) inside them with the desired max/min-width 在其中以所需的最大/最小宽度创建元素( .box
  4. Center the .box elements inside the columns with margin:0 auto ; .box元素.boxmargin:0 auto列中;
  5. You have now a "mostly" flexible grid (without flexbox) 您现在有了一个“大部分”的灵活网格(没有flexbox)

 .container { border: 2px solid green; overflow: hidden; /* hacky clearfix */ } .item { width: 33.33%; float: left; border: 2px solid red; } .box { min-width: 156px; max-width: 188px; margin: 0 auto; background: #ddd; } .container, .item, .box { padding: 10px; box-sizing: border-box; font-family: sans-serif; } 
 <div class="container"> <div class="item"> <div class="box"> min-width: 156px; <br> max-width: 188px; </div> </div> <div class="item"> <div class="box"> min-width: 156px; <br> max-width: 188px; </div> </div> <div class="item"> <div class="box"> min-width: 156px; <br> max-width: 188px; </div> </div> </div> 

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

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