Skip to content

justify-items

justify-items 属性定义 Grid 容器中 Grid 项目在行轴方向上的对齐方式。

语法

css
justify-items: start | end | center | stretch | baseline | first baseline | last baseline | safe | unsafe;

常见值

说明示例
start起始对齐justify-items: start
end结束对齐justify-items: end
center居中对齐justify-items: center
stretch拉伸对齐justify-items: stretch
baseline基线对齐justify-items: baseline
first baseline首基线对齐justify-items: first baseline
last baseline尾基线对齐justify-items: last baseline

示例

基础用法

css
/* 起始对齐 */
.start {
  justify-items: start;
}

/* 结束对齐 */
.end {
  justify-items: end;
}

/* 居中对齐 */
.center {
  justify-items: center;
}

/* 拉伸对齐 */
.stretch {
  justify-items: stretch;
}

/* 基线对齐 */
.baseline {
  justify-items: baseline;
}

高级用法

css
/* Grid 容器 */
.grid-container {
  display: grid;
  justify-items: center;
}

/* 起始对齐 */
.start {
  justify-items: start;
}

/* 结束对齐 */
.end {
  justify-items: end;
}

/* 居中对齐 */
.center {
  justify-items: center;
}

/* 拉伸对齐 */
.stretch {
  justify-items: stretch;
}

/* 基线对齐 */
.baseline {
  justify-items: baseline;
}

/* 响应式布局 */
@media (min-width: 768px) {
  .grid-container {
    justify-items: center;
  }
}

/* 卡片布局 */
.card {
  display: grid;
  justify-items: center;
}

/* 表单布局 */
.form {
  display: grid;
  justify-items: start;
}

/* 图片画廊 */
.gallery {
  display: grid;
  justify-items: center;
}

浏览器兼容性

浏览器版本
Chrome57+
Firefox52+
Safari10.1+
Edge16+
iOS Safari10.3+
Android Browser57+
Chrome for Android57+

注意事项

  • justify-items 只在 Grid 容器中有效
  • justify-items 定义 Grid 容器中 Grid 项目在行轴方向上的对齐方式
  • stretch 是默认值,表示拉伸对齐
  • baseline 表示基线对齐

相关属性

相关资源