砌体效果 - 如何更改块的顺序?

时间:2017-11-19 08:17:58

标签: html css html5 css3 css-multicolumn-layout

我使用columns(效果砌体),位置顺序为 - 自上而下

1 3 5
2 4 6

* {
  box-sizing: border-box;
}

.columns {
  columns: 3;
}

.columns-item {
  background: #ccc;
  width: 100%;
  margin: 0 0 10px;
  break-inside: avoid;
  page-break-inside: avoid;
}

.columns-item:nth-of-type(2n + 3) {
  height: 75px;
}
<div class="columns">
  <div class="columns-item">
    1
  </div>
  <div class="columns-item">
    2
  </div>
  <div class="columns-item">
    3
  </div>
  <div class="columns-item">
    4
  </div>
  <div class="columns-item">
    5
  </div>
  <div class="columns-item">
    6
  </div>
  <div class="columns-item">
    7
  </div>
  <div class="columns-item">
    8
  </div>
</div>

但我希望顺序为 - 左右

1 2 3
4 5 6

2 个答案:

答案 0 :(得分:1)

您可以使用CSS网格

执行此效果

.columns {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: minmax(10px, 32.5px);
  grid-auto-flow: dense;
  grid-gap: 10px;
}

.columns-item {
  background: #ccc;
}

.columns-item:nth-of-type(2n+3) {
  background: red;
  grid-row-end: span 2
}

.columns-item:nth-of-type(6n+6) {
  grid-column: 3
}

.columns-item:nth-of-type(6n+5) {
  grid-column: 2
}
<div class="columns">
  <div class="columns-item">
    1
  </div>
  <div class="columns-item">
    2
  </div>
  <div class="columns-item">
    3
  </div>
  <div class="columns-item">
    4
  </div>
  <div class="columns-item">
    5
  </div>
  <div class="columns-item">
    6
  </div>
  <div class="columns-item">
    7
  </div>
  <div class="columns-item">
    8
  </div>
  <div class="columns-item">
    9
  </div>
  <div class="columns-item">
    10
  </div>
  <div class="columns-item">
    11
  </div>
  <div class="columns-item">
    12
  </div>
  <div class="columns-item">
    13
  </div>
  <div class="columns-item">
    14
  </div>
  <div class="columns-item">
    15
  </div>
</div>

答案 1 :(得分:0)

请尝试以下风格。

* {
  box-sizing: border-box;
}

.columns {
   -moz-column-width: 6em;
 -webkit-column-width: 6em;
 -moz-column-gap: 1em;
 -webkit-column-gap: 1em;
}

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

检查输出:https://jsfiddle.net/stalinrajindian/mqL83eq9/