简体   繁体   English

有没有办法在div容器中放置n个div

[英]Is there a way to position n number of divs inside a div container

Is there a way to position n number of divs inside a div container, so that each div cell won't collide each other using css and js. 有没有一种方法可以在div容器中放置n个div,这样每个div单元都不会使用css和js相互冲突。 We need to set the left value without changing the height and top values. 我们需要在不更改高度和顶部值的情况下设置左侧值。 It is fine to change the cell width. 更改单元格宽度是可以的。

Please check this code sample. 请检查此代码示例。

 .container { width : 400px; height : 1000px; background : #000; position : relative; } .cell { position :absolute; background : yellow; border: 2px solid red; } 
 <div class="container"> <div class="cell" style="top:50px;width:100%;height:100px"></div> <div class="cell" style="top:150px;width:50%;height:50px"></div> <div class="cell" style="top:150px;width:50%;height:50px;"></div> <div class="cell" style="top:230px;width:33.33%;height:50px;"></div> <div class="cell" style="top:230px;width:33.33%;height:50px;"></div> <div class="cell" style="top:230px;width:33.33%;height:50px;"></div> </div> 

Expected output: 预期产量:

在此处输入图片说明

If all the cells should be stacked one above the other in the order of their mention, then simply make their display property 'block' 如果所有单元都应按照其提及的顺序堆叠在一起,则只需将其显示属性设置为“块”

 .container { width: 400px; height: 1000px; background: #000; position: relative; } .cell { display: block; background: yellow; border: 2px solid red; } 
 <div class="container"> <div class="cell" style="width:100%;height:100px;"></div> <div class="cell" style="width:50%;height:50px;"></div> <div class="cell" style="width:50%;height:50px;"></div> <div class="cell" style="width:33.33%;height:50px;"></div> <div class="cell" style="width:33.33%;height:50px;"></div> <div class="cell" style="width:33.33%;height:50px;"></div> </div> 

https://codepen.io/anon/pen/ryEaQr https://codepen.io/anon/pen/ryEaQr

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

.container
    {
      width : 400px;
      height : 1000px;
      background : #000;
      display:flex;
    flex-direction:column;
    }

    .cell
    {
      flex:1;
      background : yellow;
      border: 2px solid red;
    }

Using flexbox each row will not colide no matter the size of the container, and with flex:1; 使用flexbox不管容器的大小如何,每一行都不会冲突,而使用flex:1; each row will take as much space as it can. 每行将占用尽可能多的空间。 If you were to have flex-direction:row; 如果要使用flex-direction:row; instead each row would take a 6th of the space available. 而是每行将占用可用空间的六分之一。

Use ` 使用`

Z-index Z指数

` property of CSS. CSS的`属性。

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

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