• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

CSS类似蛇的对齐方式

用户头像
it1352
帮助1

问题说明

我一直在努力解决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.

任何帮助将不胜感激.

提前谢谢!

正确答案

#1

如果使用 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
系列文章
更多 icon
同类精品
更多 icon
继续加载