简体   繁体   English

对齐html网格项目以垂直包装

[英]Aligning html grid items to wrap vertically

I have a grid of generated divs that I need to flow 0 - 9 vertically, and then wrap 10 and on to the next column. 我有一个生成的div网格,我需要垂直流动0-9,然后包装10并进入下一列。 The current configuration is wrapping horizontally: 当前配置水平包装:

HTML 的HTML

<div id="item-container"></div>

JavaScript: JavaScript:

for (var i = 0; i <= 150; i++) {
  $("<li class='items grid'>item " + i + "</li>").appendTo("#item-    
  container"); 
}

CSS 的CSS

li.grid {
      display: inline-block;
      border: 1px solid #333;
      background-color: #000;
      color: #fff;
      padding: 30px 30px 30px 30px;
      background-image: linear-gradient(to bottom right, #eee, #ddd);
    }

Current implementation in jsfiddle: http://jsfiddle.net/Ldh9beLn/9/ jsfiddle中的当前实现: http : //jsfiddle.net/Ldh9beLn/9/

Normal flow of the HTML elements is horizontal for inline and inline-block elements. 对于内联和内联块元素,HTML元素的正常流动是水平的。 What you try to achieve is related to columns. 您尝试实现的目标与列有关。 You need to divide your item-container element into columns. 您需要将item-container元素分为几列。

The first option is to use CSS3 column-count or flex. 第一种选择是使用CSS3 column-count或flex。

#item-container{
  column-count: 4;
}

If you need support old browsers too, you may implement js fallback. 如果您还需要支持旧的浏览器,则可以实现js fallback。 There is a jquery plugin that might help you. 有一个jQuery 插件可能会帮助您。

The second option is to divide your elements into columns using a container element. 第二种选择是使用容器元素将元素分为几列。 Wrap every 10 elements inside a column and use appropriate css for the column wrapper as below; 在列内每隔10个元素换行,并对列包装程序使用适当的CSS,如下所示;

for (var i = 0; i <= 150; i++) {
    if(i%10 == 0) $("<div class='column'></div>").appendTo("#item-container");
  $("<li class='items grid'>item " + i + "</li>").appendTo("#item-container > .column:last-child");
}

.column{
  width: 35px;
  border: 1px solid;
  float: left;
  margin: 30px;
}

I figure out the solution, but creating two classes to toggle between column1 and column7 , depending on the grid layout required. 我想出了解决方案,但是根据所需的网格布局,创建了两个可在column1column7之间切换的类。

$('.item-container').removeClass('column7').addClass('column1');

and

$('.item-container').removeClass('column1').addClass('column7');

with the associated css classes: 与相关的CSS类:

.column1{
  column-count: 1;
}
.column7{
  column-count: 7;
}

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

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