我使用CSS网格来布局这样的一些项目...
#container {
display: grid;
grid-template-columns: 16.666% 16.666% 16.666% 16.666% 16.666% 16.666%;
}
.item {
background: teal;
color: white;
padding: 20px;
margin: 10px;
}
<div id="container">
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
</div>
CSS Grid不适合整行对齐,因为交叉的轨道会挡住去路。下面是详细的解释:
对齐整行/整列的网格项目(就像flex项目一样) 或者,使用flexbox和justify-content: center。
这会将所有项目打包到行的水平中心。然后你的利润会把它们分开。
在完全填充的行上,justify-content将不起作用,因为没有空闲空间让它工作。
在有空闲空间的行上(在您的例子中,只有最后一行),项目居中。
#container {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.item {
flex: 0 0 calc(16.66% - 20px);
background: teal;
color: white;
padding: 20px;
margin: 10px;
}
* {
box-sizing: border-box;
}
<div id="container">
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
</div>
找到一篇关于如何用伪选择器控制剩余网格项目的文章
.grid {
display: grid;
grid-template-columns: repeat(6, 1fr);
grid-gap: 20px;
margin: 20px;
padding: 20px;
}
.item {
grid-column: span 2;
background: #AB47BC;
padding: 20px;
}
/* Dealing with 2 orphan items */
.item:last-child:nth-child(3n - 1) {
grid-column-end: -2;
}
.item:nth-last-child(2):nth-child(3n + 1) {
grid-column-end: 4;
}
/* Dealing with single orphan */
.item:last-child:nth-child(3n - 2) {
grid-column-end: 5;
}
<div class="wrapper">
<div class="grid grid--1">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="grid grid--2">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</div>
这违背了网格系统的目的。网格是一个二维数组,其中所有东西都有一个X和Y值,就像电子表格一样。
是的,你想要一个包装物品的系统。由于flex-wrap,Flexbox符合这里的要求。
#container {
padding: 10px;
width: calc((100px + (10px * 2)) * 4); /* item width + padding on either side times number of items */
display: flex;
flex-wrap: wrap;
background: blue;
margin: 10px;
}
#container div {
width: 100px;
height: 100px;
flex-grow: 1;
background: red;
margin: 10px;
}
https://jsfiddle.net/0c0hzh8t/
这使得子元素占据了所有的可用空间,如果一行已满,则没有可用空间,这将是它的标准大小。
如果您希望容器自动调整大小,则移除width属性,容器及其项将自动调整大小。这也无妨,但我假设您想要定义一行中的项目数量。
没有特定的属性可以使最后一行的行为不同于前面的行。
不过,基于您定义了一个与视口宽度内的n个项目相匹配的集合宽度这一事实,您可以使用Flexbox及其justify-content属性。
将它设置为居中,它会将任意数量的项目的最后一行居中。
堆栈片段
html, body {
margin: 0;
}
#container {
display: flex;
flex-wrap: wrap; /* allow items to wrap */
justify-content: center; /* horizontally center items */
}
.item {
flex-basis: calc(16.666% - 20px); /* subtract the margin from the width */
background: teal;
color: white;
padding: 20px;
margin: 10px;
box-sizing: border-box; /* make padding be included in the set width */
}
<div id="container">
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
</div>
这将使行内的任何项目居中,而无需使用flex
.center-item {
grid-column: 1 / -1;
}
密码笔
<div class="grid grid-cols-3 justify-items-center gap-2">
<div class="w-5">1</div>
<div class="w-5">2</div>
<div class="w-5">3</div>
<div class="w-5">4</div>
<div class="w-5">5</div>
<div class="w-5">6</div>
<div class="w-5">7</div>
<div class="w-5">8</div>
<div class="w-5">9</div>
<div class="col-span-3">0</div>
</div>
.col-span-3 {
grid-column: span 3 / span 3;
}
.grid {
display: grid;
}
.w-5 {
width: 1.25rem;
}
.grid-cols-3 {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
.justify-items-center {
justify-items: center;
}
.gap-2 {
gap: 0.5rem;
}
对于网格项目
grid-column: 1/-1;
margin: 0 auto;
我在网格方面没做多少工作,但据我所知,如果你想使用Flex-box,就使用这段代码。
#container {
display: flex;
flex-wrap: wrap;
flex-direction: row;
}
.item {
flex: 1;
flex-basis: 16.66%;
background: teal;
color: white;
padding: 20px;
margin: 10px;
}
<div id="container">
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
<div class="item">Item</div>
</div>
这是我写HTML邮件的解决方案(截至2018年4月23日):
#wrapper {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-row-gap: 0;
grid-column-gap: 15px;
padding: 15px 15px 0 15px;
}
.item {
border: 1px solid #000;
height: 200px;
margin-bottom: 15px;
}
<div style="width: 100%; margin: 0 auto; text-align: center; vertical-align: middle;">
<div style="overflow: auto; height: 400px; margin-top: 15px;">
<div style="position: relative">
<div id="wrapper">
<div class="item"><div>Item 1</div></div>
<div class="item"><div>Item 2</div></div>
<div class="item"><div>Item 3</div></div>
<div class="item"><div>Item 4</div></div>
<div class="item"><div>Item 5</div></div>
<div class="item"><div>Item 6</div></div>
<div class="item"><div>Item 7</div></div>
<div class="item"><div>Item 8</div></div>
</div>
<div style="position: absolute; bottom: -30px; text-align: center; margin: 0 auto; width: 100%;">
<div style="background-color: #defabc; width: 300px; margin: 0 auto; text-align: center; cursor: pointer;">
<span style="color: #000;">See all items</span>
</div>
</div>
</div>
</div>
</div>