html - 具有列数属性的元素顺序

标签 html css

我正在尝试仅使用 CSS 为我的图像创建砖石式布局。我关注了this guide它运作良好。

HTML:

<div class="grid">  
    <img src="1.jpg">
    <img src="2.jpg">
    <img src="3.jpg">
    <img src="4.jpg">
    <img src="5.jpg">
    <img src="6.jpg">
    <img src="7.jpg">
    <img src="8.jpg">
</div>

CSS:

.grid {
  line-height: 0 ;

  -webkit-column-count: 4 ;
  -webkit-column-gap:   10px ;
  -moz-column-count:    4 ;
  -moz-column-gap:      10px ;
  column-count:         4 ;
  column-gap:           10px ;

  margin: 20px;
}

.grid img {
  width: 100% ;
  height: auto ;
  margin-bottom: 10px;
}

看起来像这样: Seamless grid layout

但是,图像按以下顺序出现:

1    3    5    7
2    4    6    8

我希望它是这样的:

1    2    3    4
5    6    7    8

我该如何解决这个问题?我更喜欢纯 CSS 解决方案,但我也对 Javascript 和 jQuery 方法持开放态度。理想情况下,我想要一个 row-count 属性,但它不存在。

最佳答案

.grid 上使用display:flexflex-wrap: wrap。在图像上使用 width:25%;flex: 1 0 25% 如果你想连续 4 个。

.grid {
  display: flex;
  flex-wrap: wrap;
  width: 100vw;
  height: auto;
}
img {
  width: 25%;
  height: auto;
  flex: 0 1 25%;
}
<div class='grid'>
  <img src='https://placem.at/people?w=160&h=90&random=1&txt=1'>
  <img src='https://placem.at/things?w=160&h=90&random=1&txt=2'>
  <img src='https://placem.at/places?w=160&h=90&random=1&txt=3'>
  <img src='https://placem.at/things?w=160&h=90&random=1&txt=4'>
  <img src='https://placem.at/people?w=160&h=90&random=1&txt=5'>
  <img src='https://placem.at/places?w=160&h=90&random=1&txt=6'>
  <img src='https://placem.at/things?w=160&h=90&random=1&txt=7'>
  <img src='https://placem.at/people?w=160&h=90&random=1&txt=8'>
</div>

关于html - 具有列数属性的元素顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39941941/

相关文章:

html - 字体不适用于网络

javascript - 将 "this"与多个按钮一起使用

javascript - 使用 Javascript 设置正文左边距

html - <code> 的 CSS padding-left 值仅应用于第一行

html - 显示行内 block 。如何防止将 block 推到新行?

javascript - 在 Ionic 中显示侧边菜单

html - 选择了类似导航栏的语言

html - DIV 中的下拉菜单

javascript - 在 Angular 中的 CSS 转换后渲染 JS

css - SASS SCSS函数生成具有动态路径的类