推荐用 grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)) 实现自动响应式卡片列表,auto-fit 自适应列数,配合 gap 和 box-sizing 保证布局整洁与兼容性。
用 CSS Grid 实现响应式卡片列表(比如从桌面端 2 列、移动端自动变为 1 列),关键在于用 minmax() 搭配 auto-fit 或 auto-fill,再结合媒体查询微调,既简洁又健壮。
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr))
这是最推荐的“流动列数”方案,无需手动写断点也能自适应:
auto-fit 会在空间不足时自动折叠列数(比如只剩一列宽度时,就只排 1 列)display: grid 和该规则,卡片子元素不用额外设置如果希望严格在某宽度(如 768px)切换单列,可以加媒体查询覆盖:
grid-template-columns: repeat(2, 1fr)
@media (max-width: 768px) 中改为 grid-template-columns: 1fr
margin 或用 gap 控制间距,避免小屏挤在一起让卡片真正“像卡片”,还需几处细节:
gap: 1rem 替代 margin,更干净可控width: 100%(防止内容撑宽)和 box-sizing: border-box
不复杂但容易忽略的是:Grid 响应

minmax() + auto-fit 就能自然适配各种宽度,比硬写多个 repeat(2)/repeat(1) 更灵活可靠。