CSS中类似蛇的对齐方式 [英] Snake-like alignment in css

查看:36
本文介绍了CSS中类似蛇的对齐方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决CSS中的以下问题.我有任意数量的项目(span或div),它们想以蛇形的方式包装在容器内.

I have been struggling with the following problem in CSS. I have an arbitrary number of items (spans or divs) that I want to wrap inside a container in a snake-like pattern.

我的意思是,如果我有10个项目,每个项目的宽度为20px,我希望它们在60px宽度的容器中显示如下:

What I mean by this is that if I have 10 items, which are each 20px wide, I would like them to display as follow in a 60px wide container:

0 1 2
5 4 3
6 7 8
    9

我尝试在CSS中使用flexbox,但是我只能使项目显示如下:

I have tried using flexbox in CSS, but I can only get the items to display like this:

0 1 2
3 4 5
6 7 8
9

如果可以的话,我知道单个项目的确切宽度,但是我不知道容器的宽度.

If that can help, I know the exact width of the individual items, but I do not know the width of the container.

任何帮助将不胜感激.

提前谢谢!

推荐答案

如果使用 parent - rows - items 创建HTML结构您可以在 .row:nth-​​child(2n)元素上使用 flex-direction:row-reverse ,这将产生所需的结果.

If you create your HTML structure with parent - rows - items you can use flex-direction: row-reverse on .row:nth-child(2n) elements and that will create desired result.

.content {
  display: flex;
  flex-direction: column;
}
.row {
  display: flex;
  justify-content: space-between;
}
.row:nth-child(2n) {
  flex-direction: row-reverse;
}
.row:nth-child(2n):last-child .item {
  margin-right: auto;
}

<div class="content">
  <div class="row">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
  </div>
  <div class="row">
    <div class="item">4</div>
    <div class="item">5</div>
    <div class="item">6</div>
  </div>
  <div class="row">
    <div class="item">7</div>
    <div class="item">8</div>
    <div class="item">9</div>
  </div>
  <div class="row">
    <div class="item">10</div>
  </div>
</div>

这篇关于CSS中类似蛇的对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆