CSS place-items 属性

定义和用法

place-items 属性用于网格布局,是以下属性的简写:

如果 place-items 属性有两个值:

place-items: start center;
  • align-items 属性的值为 'start'
  • justify-items 属性的值为 'center'

如果 place-items 属性只有一个值:

place-items: end;
  • 则 align-items 和 justify-items 属性的值均为 'end'

实例

例子 1

将每个 <div> 元素在其网格单元格的行内方向和块方向上都放置在开始位置:

#container {
  place-items: start;
}

亲自试一试

例子 2:书写模式

当网格容器的 writing-mode 属性值设置为 'vertical-rl' 时,块方向的结束位置从底部移动到左侧,行内方向的结束位置从右侧移动到底部:

#container {
  place-items: end;
  writing-mode: vertical-rl;
}

亲自试一试

例子 3:弹性盒布局

justify-items 属性对弹性盒布局不适用。因此,如果在弹性盒布局中使用 place-items 属性,justify-items 的值(即第二个值)会被忽略。

#wrapper {
  place-items: stretch end;
}

亲自试一试

CSS 语法

place-items: normal legacy|value|initial|inherit;

属性值

描述
normal legacy

默认。元素的默认 place-items 值。

align-items 的默认值是 'normal',justify-items 的默认值是 'legacy'。

baseline 项目定位在容器的基线上。
center 将项目对齐到网格单元格的中心。
end 将项目对齐到网格单元格的结束位置。
start 将项目对齐到网格单元格的开始位置。
stretch 如果未设置网格项目的大小,则拉伸网格项目以填满网格容器。
initial 将此属性设置为其默认值。参阅 initial
inherit 从其父元素继承此属性。参阅 inherit

技术细节

默认值: normal legacy
继承性:
动画制作: 不支持。请参阅:动画相关属性
版本: CSS3
JavaScript 语法: object.style.placeItems="stretch center"

浏览器支持

表格中的数字表示首个完全支持该属性的浏览器版本。

Chrome Edge Firefox Safari Opera
59.0 79.0 45.0 11.0 46.0

相关页面

教程:CSS 网格布局

教程:CSS 弹性盒布局

参考:CSS align-items 属性

参考:CSS justify-items 属性

参考:CSS writing-mode 属性