grid-auto-rows用于设置Grid布局中隐式生成行的高度,当内容超出显式定义的行时生效。例如grid-auto-rows: 80px使自动生成的行高为80px;与grid-template-rows(定义显式行)不同,它控制自动创建的隐式轨道;可配合fr单位实现剩余空间的灵活分配,适用于动态内容布局。

在CSS Grid布局中,auto-rows 属性并不是一个独立的CSS属性,而是 grid-auto-rows 的简称。它用于定义Grid容器中**自动生成的隐式行轨道(implicit grid rows)的高度**。当你放置的内容超出了显式定义的网格行时,浏览器会自动创建额外的行,这些行的高度由 grid-auto-rows 控制。
grid-auto-rows: <track-size>+
其中 <track-size> 可以是长度值(如 px、em、fr)、百分比,或关键字(如 auto、min-content、max-content)。
常见取值示例:
grid-auto-rows: auto; — 行高根据内容自动调整grid-auto-rows: 100px; — 所有隐式行高度为100pxgrid-auto-rows: 1fr; — 隐式行均分剩余空间grid-auto-rows: min-content; — 行高等于内容最小高度grid-auto-rows: max-content; — 行高等于内容最大高度当使用 grid-row 或 grid-area 将子元素定位到未显式定义的行位置时,Grid容器会自动创建新的行轨道,这些就是“隐式行”。
例如:
.container {
display: grid;
grid-template-rows: 100px; /* 只定义了一行 */
grid-auto-rows: 80px; /* 隐式行高度设为80px */
}
<p>.item {
grid-row: 3 / 4; /<em> 放置到第3行,但前两行未定义 </em>/
}</p>此时,第2行和第3行会被自动创建,它们的高度将采用 grid-auto-rows: 80px 的设定。
grid-template-rows 定义的是显式网格中的行高,即你手动规划的行。
grid-auto-rows 则控制那些因内容溢出或定位需要而自动生成的行。
举个实用场景:
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: 100px; /* 第1行固定100px */
grid-auto-rows: 60px; /* 后续自动生成的行60px */
}
如果第一个项目放在第1行,第二个项目放在第5行,那么第2、3、4行将被自动创建,每行高60px。
使用 fr 单位可以让隐式行按比例分配可用空间。
.grid {
display: grid;
height: 400px;
grid-template-rows: 100px;
grid-auto-rows: 1fr; /* 剩余空间平均分配给隐式行 */
}
假设共生成3个隐式行,它们将共享剩下的300px,每行约100px高。
基本上就这些。掌握 grid-auto-rows 能让你更灵活地处理动态内容或复杂定位场景下的Grid行高控制,避免意外空白或溢出问题。
以上就是Grid容器中auto rows属性如何应用_自动生成行高度解析的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号