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>
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcgjkab
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20