justify-items用于设置网格容器内所有子项的水平对齐方式,可选值为start、end、center、stretch,默认为stretch;在grid容器上使用,统一作用于所有子元素,可通过justify-self或margin:auto局部覆盖,仅适用于Grid布局。

在 CSS Grid 布局中,justify-items 用于控制网格单元格(grid items)在其网格区域内的水平对齐方式。它作用于容器(grid container),影响所有子元素的对齐行为。
1. justify-items 的基本语法
在父容器上设置:justify-items: start | end | center | stretch;
这个属性决定了每个网格项在所在列中的水平对齐方式。
2. 可选值及其效果
- start:内容靠左对齐(在从左到右的语言环境中)
- end:内容靠右对齐
- center:内容居中对齐
- stretch:默认值,拉伸填充整个网格区域宽度
3. 实际使用示例
HTML 结构:
立即学习“前端免费学习笔记(深入)”;
项目1项目2项目3
CSS 样式:
.grid-container {
display: grid;
grid-template-columns: repeat(3, 100px);
justify-items: center; /* 所有子项水平居中 */
}
.item {
background: #4CAF50;
color: white;
padding: 20px;
}
此时三个项目会在各自的 100px 宽度列中水平居中。
4. 注意事项
- justify-items 对整个容器的所有子项统一生效,不能单独设置某一个项
- 如果某个项设置了 margin: auto 或使用 justify-self,会覆盖 justify-items 的行为
- 该属性只在 Grid 容器上有效,Flex 布局中应使用 justify-content
基本上就这些。合理使用 justify-items 能快速实现一致的水平对齐效果,适合多数标准化布局场景。










