简体   繁体   English

具有动态宽度的div,其大小为浮动顶部的div的动态数量

[英]Div with dynamic width sized to dynamic number of divs that float top

I have an issue trying to force the CSS to work the way I want it to. 我在尝试强制CSS按照我希望的方式工作时遇到问题。 Below you will find a 2 pictures and a description of what the behaviour should be like. 您将在下面找到两张图片,并说明其行为。 All the CSS and content is dynamically created so any form of "flexibility" is doable, though if possible, should be avoided. 所有CSS和内容都是动态创建的,因此任何形式的“灵活性”都是可行的,即使可能,也应避免。

The RED div has dynamical width (width:auto) and needs to be filled with a bunch of BLACK divs. RED div具有动态宽度(width:auto),需要填充一堆BLACK div。 The number of black divs is random, or rather depends on a case-to-case basis. 黑色div的数量是随机的,或者取决于具体情况。

Picture1 and Picture2 show how the menu should expand in case the amount of black divs is taking more space then one height (which is fixed to 720px). Picture1和Picture2显示了在黑色div占用的空间多于一个高度(固定为720px)的情况下菜单应如何展开。 In case a thrid column was to be needed, it once again should be expanded towards left. 如果需要第三列,则应再次向左扩展。

I've searched around and found this CSS: 我到处搜索并找到以下CSS:

#redDiv {
-moz-column-count: 2; 
-moz-column-gap: 50%;
-webkit-column-count: 2;
-webkit-column-gap: 50%;
column-count: 3;
column-gap: 50%;
}

however there are three issues there: 但是,这里存在三个问题:

  • First one, the amount of columns needs to be fixed this way, and I need it to dynamically suit the content that fills it. 首先,需要以这种方式固定列数,而我需要它来动态适应填充它的内容。
  • Secondly, I do not think that this is meant to be done with dynamical width divs, mainly because gap and width have no meaning there. 其次,我认为这并不是要用动态宽度div来完成的,主要是因为间隙宽度在那里没有意义。
  • Lastly, even if I do use the javascript to determin the number of columns, the table expands to the right and I need it to expand to the left, because it is docked to the right side of the screen with some controls being there. 最后,即使我确实使用javascript确定列数,该表也会向右扩展,并且我需要将其向左扩展,因为该表停靠在屏幕的右侧,并带有一些控件。

EDIT: ( js fiddle ) UPDATED 编辑:js小提琴更新

HTML: HTML:

<div id="rightdocked">
  <div id="RedDiv">
    <div class="blackdivs"></div>
    <div class="blackdivs"></div>
    <div class="blackdivs"></div>
    <div class="blackdivs"></div>
    <div class="blackdivs"></div>
  </div>
</div>

CSS: CSS:

.blackdivs {
  position: relative;
  margin-top: 0px;
  margin-bottom: 20px;
  width: 300px;
  height: 40px;
  border: solid 2px black;
  background-color: white;
}

.blackdivs:first-of-type {
  background-color: green;
}

#RedDiv {
  max-height: 180px;
  width: auto;
  z-index: 9;
  background-color: lightgreen;
  position: relative;
  float: left;
  background-color: white;
  border: 2px red solid;
}

#rightdocked {
  position: relative;
  width: auto;
  height: 300px;
  float: right
}

Any suggestions? 有什么建议么?

Here is some code to get you started. 这是一些入门代码。 I guess this is the behavior you wanted. 我想这是您想要的行为。 I have kept max-height to 120px for demo, you can change it to 720px. 我将最大高度保持为120px,可以将其更改为720px。 Just keep on pressing add box button to add inner divs. 只需继续按下添加框按钮即可添加内部div。

 $('#addBoxButton').click(function() { $('.outer').append('<div class="inner">innner</div>'); }); 
 .outer { border: 1px solid red; max-height: 120px; width: inherit; display: flex; flex-wrap: wrap-reverse; flex-direction: column; } .inner { border: 1px solid black; width: 50px; height: 20px; margin: 5px } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script> <div class='outer'> </div> <input id="addBoxButton" type="button" value="add box" /> 

 .red { border:solid 2px red; text-align:center; float:right; } .black { border:solid 2px #000; display:inline-block; width:200px; margin:4px; height:40px; } span .black:nth-child(1) { width:0; opacity:0; transition:0.3s; } span:hover .black:nth-child(1) { width:200px; opacity:1; } 
 <div class="red"> <span> <div class="black"></div> <div class="black"></div> </span> </div> 

Maybe this can get you started 也许这可以帮助您入门

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

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