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

在 CSS Grid 布局中,justify-items 用于控制网格单元格(grid items)在其网格区域内的水平对齐方式。它作用于容器(grid container),影响所有子元素的对齐行为。
justify-items: start | end | center | stretch;
这个属性决定了每个网格项在所在列中的水平对齐方式。
HTML 结构:
立即学习“前端免费学习笔记(深入)”;
<div class="grid-container"> <div class="item">项目1</div> <div class="item">项目2</div> <div class="item">项目3</div> </div>
CSS 样式:
.grid-container {
display: grid;
grid-template-columns: repeat(3, 100px);
justify-items: center; /* 所有子项水平居中 */
}
<p>.item {
background: #4CAF50;
color: white;
padding: 20px;
}</p>此时三个项目会在各自的 100px 宽度列中水平居中。
基本上就这些。合理使用 justify-items 能快速实现一致的水平对齐效果,适合多数标准化布局场景。
以上就是如何用css justify-items设置单元格水平对齐的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号