css教程网 css教程网365css.cn
公告为您打开互联网世界的窗户关于本站
首页/属性速查/布局与定位

grid-template-columns

定义网格的列数与列宽,Grid 布局的核心属性。

语法
grid-template-columns: none | <track-list> | <auto-track-list>
常用值
repeat(3, 1fr) / 240px 1fr / repeat(auto-fit, minmax(240px, 1fr))
初始值
none
继承
否

运行效果

示例改成别的值试试,差异一眼能看出来
查看代码
HTML
<div class="gt2"><i>1</i><i>2</i><i>3</i><i>4</i><i>5</i></div>
CSS
.gt2 { display: grid; grid-template-columns: 1.4fr 1fr 1fr; gap: 6px; width: 100%; }
.gt2 i { height: 34px; border-radius: 6px; background: #2b57ff; color: #fff; display: grid; place-items: center; font: 600 10px/1 ui-monospace, monospace; font-style: normal; opacity: .92; }

使用要点

  • 1fr 实际是 minmax(auto, 1fr),内容过长会撑破;要严格等分写 minmax(0, 1fr)。
  • auto-fit + minmax 是实现响应式卡片列表最省事的写法,不需要媒体查询。