学习库CSSCSS Grid

CSS Grid

Build two-dimensional layouts with tracks, areas, minmax, and auto-fit.

更新于 适用于 Modern CSS

Grid controls rows and columns together. It is suited to page regions, galleries, dashboards, and any layout where items should share track lines.

Define flexible tracks

courses.css
.course-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(18rem, 100%), 1fr));
  gap: 1rem;
}

minmax() sets track limits. auto-fit creates as many columns as fit, then stretches them to fill the row.

Place named regions

page.css
.page {
  display: grid;
  grid-template-columns: 16rem minmax(0, 1fr);
  grid-template-areas: "sidebar content";
  gap: 2rem;
}
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }

Named areas make larger compositions readable. minmax(0, 1fr) allows long content to shrink within the available track instead of overflowing.

Align within the grid

justify-items works along the inline axis, align-items along the block axis, and place-items sets both. Individual items can override alignment with justify-self and align-self.

Keep this

Think in tracks, use intrinsic sizing to reduce breakpoints, and choose Grid when rows and columns must coordinate.

学习位置已保存在此设备上。