英文:
How to make two columns with CSS?
问题
我有一个包含N个元素的数组:
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
我需要按照以下顺序创建两列:
如果有10个元素:
1 6
2 7
3 8
4 9
5 10
如果有9个元素:
1 6
2 7
3 8
4 9
5
如果有7个元素:
1 5
2 6
3 7
4
等等。CSS应该将元素分成两列。
元素的数量不是固定的。
在React中的代码非常简单:
numList.map(item => (
<div>{item}</div>
))
我可以使用JavaScript将元素数组分成两个数组,然后分别渲染它们作为两个列表,并使用flex创建列。
但我想知道是否有一种只使用CSS而无需JavaScript的方法。
如何使用CSS创建两列?
英文:
I have an array of N elements:
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
I need to make two columns in following order:
If we have 10 elements:
1 6
2 7
3 8
4 9
5 10
If we have 9 elements:
1 6
2 7
3 8
4 9
5
If we have 7 elements:
1 5
2 6
3 7
4
And so on. CSS should devide elements into two columns.
The number of elements isn't a constant.
The code in react is pretty simple:
numList.map(item => (
<div>{item}</div>
))
I can devide elements array into two arrays using js and then render it as two list separatelly and make columns for it with flex.
But I wish to know if there a way to do it with CSS only without JS.
How do I make two columns with CSS?
答案1
得分: 1
根据你的示例,我认为只需要简单地使用 columns: 2
就可以实现这个功能。
body {
display: flex;
flex-direction: column;
gap: 20px;
}
.wrapper {
columns: 2;
border: 1px solid black;
}
<div class="wrapper">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
</div>
<div class="wrapper">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
</div>
<div class="wrapper">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
</div>
英文:
Based on your examples, I think just a simple columns: 2
would have this functionality.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
body {
display: flex;
flex-direction: column;
gap: 20px;
}
.wrapper {
columns: 2;
border: 1px solid black;
}
<!-- language: lang-html -->
<div class="wrapper">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
</div>
<div class="wrapper">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
</div>
<div class="wrapper">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
</div>
<!-- end snippet -->
答案2
得分: 0
你可以使用grid-template-columns属性来指定网格布局中的列数(以及宽度)。这些值是一个以空格分隔的列表,其中每个值指定了相应列的大小。
参考这里:CSS Grid Layout模块
<div class="grid-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
</div>
.grid-container {
display: grid;
grid-template-columns: 20px 20px;
}
对于这个示例,我指定了每列的宽度为20px,你可以使用自己的数字或"auto"来设置宽度。你还可以使用repeat函数来重复相同的列模式多次。例如:
.grid-container {
display: grid;
grid-template-columns: repeat(5, 50px);
}
英文:
You can use grid-template-columns property and specifies the number (and the widths) of columns in a grid layout. The values are a space separated list, where each value specifies the size of the respective column.
Refer this: CSS Grid Layout Module
<div class="grid-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
</div>
.grid-container {
display: grid;
grid-template-columns: 20px 20px;
}
For this example, I specified each column only 20px width, you can put your own numbers or auto for the width.
You can also use repeat function to repeat same pattern of column multiple of times. For example:
.grid-container {
display: grid;
grid-template-columns: repeat(5, 50px);
}
答案3
得分: 0
.container{
display:grid;
grid-template-columns: repeat(5,1fr)}
<div class="container">
<div></div>
<div></div>
<div></div>
</div>
英文:
Best way is to give class to parent div and give display as grid and in column give how many element you want in one column
.container{
display:grid;
grid-template-columns: repeat(5,1fr)}
<div class="container">
<div></div>
<div></div>
<div></div>
</div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论