CSS Grid对齐核心在于分清对齐对象(网格项或轨道)和轴向(行轴/列轴)。justify-用于行轴(水平),align-用于列轴(垂直)。justify-items和align-items控制网格项在单元格内的默认对齐,place-items为其简写;justify-self、align-self、place-self可覆盖单个网格项的对齐方式;justify-content和align-content则在容器有额外空间时分配网格轨道的位置,常见值包括start、center、space-between等,place-content为二者简写。通过组合这些属性,可实现精确的二维布局控制。

CSS Grid的对齐控制,核心在于理解它在两个维度(行轴和列轴)上如何处理“内容”与“空间”。简单来说,我们通过
justify-items
align-items
justify-content
align-content
justify-self
align-self
要真正掌握CSS Grid的对齐,我认为首先得把概念理清楚:是对齐“网格项”(Grid Items)还是对齐“网格轨道”(Grid Tracks),以及是在“行轴”(inline axis,通常是水平方向)还是“列轴”(block axis,通常是垂直方向)上进行对齐。
我们主要用到的属性可以分为几类:
控制网格项在各自单元格内的对齐(作用于网格容器):
立即学习“前端免费学习笔记(深入)”;
justify-items
justify-items: center;
align-items
align-items: center;
place-items
justify-items
align-items
place-items: center;
justify-items: center; align-items: center;
这些属性的常见值包括
start
end
center
stretch
控制单个网格项在各自单元格内的对齐(作用于网格项本身):
justify-self
justify-items
align-self
align-items
place-self
justify-self
align-self
使用场景通常是,你希望大部分网格项保持一种对齐方式,但其中一两个需要特殊处理。比如,你可能希望所有图片都居中,但某个文字块需要左对齐。
控制网格轨道在网格容器内的对齐(作用于网格容器,当容器有额外空间时):
justify-content
align-content
place-content
justify-content
align-content
这些属性的值更丰富,除了
start
end
center
space-around
space-between
space-evenly
理解了这些,你会发现Grid的对齐逻辑其实非常清晰,就是分层控制。
.grid-container {
display: grid;
grid-template-columns: repeat(3, 100px); /* 3列,每列100px */
grid-template-rows: repeat(2, 100px); /* 2行,每行100px */
width: 500px; /* 容器比内容宽 */
height: 300px; /* 容器比内容高 */
border: 1px solid blue;
/* 1. 对齐网格项在各自单元格内 */
justify-items: center; /* 所有项水平居中 */
align-items: center; /* 所有项垂直居中 */
/* 2. 对齐网格轨道在容器内 */
justify-content: space-around; /* 行轨道在容器内水平分散 */
align-content: space-evenly; /* 列轨道在容器内垂直均匀分散 */
}
.grid-item {
background-color: lightcoral;
border: 1px solid red;
width: 80px; /* 小于单元格宽度 */
height: 80px; /* 小于单元格高度 */
}
.grid-item:nth-child(2) {
/* 3. 对齐单个网格项 */
justify-self: start; /* 第二个项水平左对齐 */
align-self: end; /* 第二个项垂直底对齐 */
}justify-items
align-items
这是初学者常问的问题,也是理解Grid对齐的关键一步。简单来说,它们都作用于网格容器,用于控制所有直接子网格项在各自的单元格内部如何对齐。但它们作用的轴向不同:
justify-items
text-align
justify-items: start;
justify-items: end;
justify-items: center;
justify-items: stretch;
align-items
align-items: start;
align-items: end;
align-items: center;
align-items: stretch;
我个人理解,
justify
align
要让单个网格项在其分配到的单元格内居中显示,我们有几种方法,最直接且推荐的是使用
place-self: center;
使用 justify-self
align-self
justify-self
align-self
.grid-item-specific {
justify-self: center; /* 水平居中 */
align-self: center; /* 垂直居中 */
}这个属性是应用在具体的网格项上的,它会覆盖其父容器(网格容器)上设置的
justify-items
align-items
使用 place-self
place-self
align-self
justify-self
.grid-item-specific {
place-self: center; /* 同时实现水平和垂直居中 */
}如果提供两个值,第一个值用于
align-self
justify-self
.grid-item-specific {
place-self: start end; /* 垂直顶部对齐,水平右对齐 */
}如果容器已经设置了 justify-items
align-items
justify-items: center;
align-items: center;
justify-self
align-self
选择哪种方式取决于你的具体需求。通常,我倾向于在容器级别设置一个通用的对齐规则(比如
justify-items: stretch; align-items: stretch;
place-self
当网格容器的尺寸大于所有网格轨道(行或列)的总和时,就会出现“额外空间”。这时,
justify-content
align-content
justify-content
justify-content
justify-content: start;
justify-content: end;
justify-content: center;
justify-content: space-between;
justify-content: space-around;
justify-content: space-evenly;
align-content
align-content
justify-content
.grid-container-with-extra-space {
display: grid;
grid-template-columns: repeat(2, 100px); /* 总宽200px */
grid-template-rows: repeat(2, 80px); /* 总高160px */
width: 400px; /* 容器宽于内容 */
height: 300px; /* 容器高于内容 */
border: 2px solid green;
/* 水平方向,将200px额外空间均匀分布在列轨道之间和两端 */
justify-content: space-evenly;
/* 垂直方向,将140px额外空间居中分配 */
align-content: center;
}
.grid-item {
background-color: lightblue;
border: 1px dashed blue;
width: 90px;
height: 70px;
}我发现很多时候,
space-evenly
space-between
以上就是CSS网格对齐怎么控制_CSS网格对齐方式指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号