【问题标题】:Need div arrangement as shown in the screenshot需要 div 排列如截图所示
【发布时间】:2017-06-27 02:22:57
【问题描述】:

我正在开发一个 chrome 扩展。有 n 个 div 块。这 n 个 div 块取决于我要访问的 URL。因此,在某些页面上,可见块可能是 1、2、3、4、5,但在另一个页面上,它们可能是 1、5 或 1、2、5。 所以相应地我的div's 应该占据空间并且不要留下任何空白空间。

我需要的输出:

这是我尝试过的(也在jsfiddle),但问题是红色下方的空白区域。

div {
  float: left;
  width: 200px;
}
.One {
  background-color: Red;
  height: 200px;
}
.Two {
  background-color: Green;
  height: 400px;
}
.Three {
  background-color: Blue;
  height: 500px;
}
.Four {
  background-color: Orange;
  height: 700px;
}
.Five {
  background-color: Yellow;
  height: 200px;
}
.Six {
  background-color: Pink;
  height: 400px;
}
.Seven {
  background-color: Grey;
  height: 600px;
}
<div class="One">
</div>
<div class="Two">
</div>
<div class="Three">
</div>
<div class="Four">
</div>
<div class="Five">
</div>
<div class="Six">
</div>
<div class="Seven">
</div>

【问题讨论】:

  • 我并没有真正得到太多,你的小提琴离想要的输出很远,但如果你想要的只是让红色框占据/延伸到下面的空白空间,那么为什么不只是改变它身高?
  • 因为它们应该是动态的。如果其中一个 div 被删除,其他 div 应该占用该空间
  • 我们需要知道您有什么要求(如果有的话),当某些块没有显示时,您希望如何排列不同的块。使块流入区域中相对容易,否则该区域将被特定未显示的块占据。但是,确切地它的外观将取决于它是如何完成的。我们需要知道您想要什么,因为它的外观有太多可能的解释。

标签: html css google-chrome-extension css-float


【解决方案1】:

HTML

<article>
  <section>
    <p class="sect1">1</p>
  </section>
  <section>
    <p class="sect2">2</p>
  </section>
  <section>
    <p class="sect3">3</p>
  </section>
  <section>
    <p class="sect4">4</p>
  </section>
  <section>
    <p class="sect5">5</p>
  </section>
  <section>
    <p class="sect6">6</p>
  </section>
  <section>
    <p class="sect7">7</p>
  </section>
  <section>
    <p class="sect8">8</p>
  </section>
  <section>
    <p class="sect9">9</p>
  </section>
  <section>
    <p class="sect10">10</p>
  </section>
</article>

CSS

*, *:before, *:after {box-sizing:  border-box !important;}

article {
 -moz-column-width: 10em;
 -webkit-column-width: 10em;
 -moz-column-gap: 1em;
 -webkit-column-gap: 1em; 

}

section {
 display: inline-block;
 margin:  0.25rem;
 padding:  1rem;
 width:  100%; 
 background:  #efefef;
}

p {
 min-width: 200px;
 margin:  1rem 0; 
}

.sect1{ min-height: 100px; }
.sect2{ min-height: 200px; }
.sect3{ min-height: 300px; }
.sect4{ min-height: 300px; }
.sect5{ min-height: 500px; }
.sect6{ min-height: 600px; }
.sect7{ min-height: 200px; }
.sect8{ min-height: 400px; }
.sect9{ min-height: 400px; }
.sect10{ min-height: 400px; }

这是链接: https://jsfiddle.net/JPratik2712/huLkhpux/

【讨论】:

  • 将代码放入snippet 会显着改善这个答案。使用 sn-p 也使其更有可能被投票,因为人们可以快速验证您的代码是否正确运行。虽然不应该总是使用 sn-ps,但对于基于 JavaScript/HTML/CSS 的问题和答案,它们可以显着改善问题和解决方案的交流。如果可以使用,sn-p 比在场外的可运行代码示例(例如 JSFiddle)更好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-11
  • 2021-07-19
  • 2013-06-30
  • 2012-04-06
  • 1970-01-01
  • 1970-01-01
  • 2012-03-11
相关资源
最近更新 更多