使用CSS网格或flexbox将未知数量的项目分布到2列中

mzaanser  于 2023-10-21  发布在  其他
关注(0)|答案(3)|浏览(106)

我在容器中有未知数量的子项:

<div class="container">
    <div class="child">Item 1</div>
    <div class="child">Item 2</div>
    <div class="child">Item 3</div>
    <div class="child">Item 4</div>
    <div class="child">Item 5…</div>
</div>

我想将这些项目堆叠成2列,使它们出现:

Item 1  Item 4
Item 2  Item 5
Item 3

我不知道如何在不知道网格行数的情况下使用CSS grid,或者在不明确设置容器高度的情况下使用flexbox来实现这一点。
有没有人能提出一个方法来做到这一点?

k4emjkb1

k4emjkb11#

如果有使用flexbox和grid的义务,那么据我所知,你需要容器的高度(使用JavaScript或你谈到的CSS自定义属性动态计算它)。
我可以想到一种方法来实现这一点纯粹在CSS没有任何动态计算或提前知道的高度,这是通过使用column-count属性。有了这个,你不必担心进来的物品数量。在这里分享我的解决方案,以供参考,如果你正在寻找一个解决方案以外的flexbox或网格:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    .container {
      column-count: 2; /* speciifes that the content inside the container should be divided into two columns */
      column-gap: 1rem;
      width: 100%;
    }
    .child {
      display: inline-block;
      width: 100%;
      box-sizing: border-box;
      break-inside: avoid; /* ensures that each child element stays within a single column */
      padding: 0.5rem;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="child">Item 1</div>
    <div class="child">Item 2</div>
    <div class="child">Item 3</div>
    <div class="child">Item 4</div>
    <div class="child">Item 5</div>
    <div class="child">Item 6</div>
    <div class="child">Item 7</div>
    <div class="child">Item 8</div>
    <div class="child">Item 9</div>
    <div class="child">Item 10</div>
    <div class="child">Item 11</div>
    <div class="child">Item 12</div>
    <div class="child">Item 13</div>
    <div class="child">So on...</div> 
  </div>
</body>
</html>
z4iuyo4d

z4iuyo4d2#

它可以在CSS中完成,如果你有一个最大数量的可能的元素
基于数量查询设置样式(很多样式)可以完成这项工作:

.container {
    display: grid;
    grid-template-columns: 100px 100px;
    margin: 10px;
    border: solid 1px black;
    grid-gap: 5px;
    width: 205px;
    grid-auto-flow: column;
}

.child {
    grid-column: 1;
  eight: 30px;
  background-color: lightgreen;
}

.child:nth-child(3):nth-last-child(-n+4) ~ .child {
    grid-column: 2;
    color: red;
}
.child:nth-child(4):nth-last-child(-n+5) ~ .child {
    grid-column: 2;
    color: blue;
}
.child:nth-child(5):nth-last-child(-n+6) ~ .child {
    grid-column: 2;
    color: green;
}
.child:nth-child(6):nth-last-child(-n+7) ~ .child {
    grid-column: 2;
    color: green;
}
<div class="container">
    <div class="child">Item 1</div>
    <div class="child">Item 2</div>
    <div class="child">Item 3</div>
    <div class="child">Item 4</div>
    <div class="child">Item 5…</div>
</div>
<div class="container">
    <div class="child">Item 1</div>
    <div class="child">Item 2</div>
    <div class="child">Item 3</div>
    <div class="child">Item 4</div>
    <div class="child">Item 5…</div>
    <div class="child">Item n…</div>
</div>
<div class="container">
    <div class="child">Item 1</div>
    <div class="child">Item 2</div>
    <div class="child">Item 3</div>
    <div class="child">Item 4</div>
    <div class="child">Item 5…</div>
    <div class="child">Item n…</div>
    <div class="child">Item n…</div>
</div>
<div class="container">
    <div class="child">Item 1</div>
    <div class="child">Item 2</div>
    <div class="child">Item 3</div>
    <div class="child">Item 4</div>
    <div class="child">Item 5…</div>
    <div class="child">Item n…</div>
    <div class="child">Item n…</div>
    <div class="child">Item n…</div>
    <div class="child">Item n…</div>
    <div class="child">Item n…</div>
    <div class="child">Item n…</div>
</div>

另一种方法是在服务器端添加类

.container {
    display: grid;
    grid-template-columns: 100px 100px;
    margin: 10px;
    border: solid 1px black;
    grid-gap: 5px;
    width: 205px;
    grid-auto-flow: column;
}

.child {
    grid-column: 1;
    background-color: lightgreen;
}

.newcol, .newcol ~ .child {
    grid-column: 2;
}
<div class="container">
    <div class="child">Item 1</div>
    <div class="child">Item 2</div>
    <div class="child">Item 3</div>
    <div class="child">Item 4</div>
    <div class="child">Item 5…</div>
    <div class="child">Item n…</div>
    <div class="child newcol">Item n…</div>
    <div class="child">Item n…</div>
    <div class="child">Item n…</div>
    <div class="child">Item n…</div>
    <div class="child">Item n…</div>
</div>
gg0vcinb

gg0vcinb3#

试试这个:

const container = document.getElementById('container');
let itemCount = 0;

addEventListener('click', function() {
    itemCount++;
    const newItem = document.createElement('div');
    newItem.className = 'child';
    newItem.textContent = 'Item ' + itemCount;
    container.appendChild(newItem);
});
.container {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px; /* Add spacing between items */
}

.child {
    padding: 10px;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="w.css">
</head>
<body>
    <div class="container">
        <div class="child">Item 1</div>
        <div class="child">Item 2</div>
        <div class="child">Item 3</div>
        <div class="child">Item 4</div>
        <div class="child">Item 5</div>
        <div class="child">Item 5</div>
        <div class="child">Item 5</div>
        <div class="child">Item 5</div>
        <div class="child">Item 5</div>
        <div class="child">Item 5</div>
        <div class="child">Item 5</div>
        <div class="child">Item 5</div>
        <div class="child">Item 5</div>
        
    </div>
    <script src="w.js"></script>
</body>
</html>

但我也会用JavaScript

相关问题