Skip to content

justify-self

justify-self 属性定义单个 Grid 项目在行轴方向上的对齐方式。

语法

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

常见值

说明示例
auto继承父容器的 justify-itemsjustify-self: auto
start起始对齐justify-self: start
end结束对齐justify-self: end
center居中对齐justify-self: center
stretch拉伸对齐justify-self: stretch
baseline基线对齐justify-self: baseline
first baseline首基线对齐justify-self: first baseline
last baseline尾基线对齐justify-self: last baseline

示例

基础用法

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

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

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

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

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

高级用法

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

/* 项目单独对齐 */
.item-1 {
  justify-self: start;
}

.item-2 {
  justify-self: center;
}

.item-3 {
  justify-self: end;
}

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

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

.card-header {
  justify-self: start;
}

.card-body {
  justify-self: center;
}

.card-footer {
  justify-self: end;
}

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

.form-group {
  display: grid;
  justify-items: center;
}

.form-label {
  justify-self: center;
}

.form-input {
  justify-self: stretch;
}

浏览器兼容性

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

注意事项

  • justify-self 只在 Grid 容器中有效
  • justify-self 定义单个 Grid 项目在行轴方向上的对齐方式
  • auto 是默认值,表示继承父容器的 justify-items
  • stretch 表示拉伸对齐

相关属性

相关资源