place-items

Baseline 广泛可用 *

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2020 年 1 月⁩ 起,所有主流浏览器均已支持。

* 此特性的某些部分可能存在不同级别的支持。

CSS place-items 简写属性同时沿着块级和内联方向对齐项目。它设置了 align-itemsjustify-items 属性的值。如果第二个值未设置,则第一个值也用于第二个值。

试一试

place-items: center stretch;
place-items: center start;
place-items: start end;
place-items: end center;
<section class="default-example" id="default-example">
  <div class="example-container">
    <div class="transition-all" id="example-element">
      <div>One</div>
      <div>Two</div>
      <div>Three</div>
    </div>
  </div>
</section>
#example-element {
  border: 1px solid #c5c5c5;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-auto-rows: 80px;
  grid-gap: 10px;
  width: 220px;
}

#example-element > div {
  background-color: rgb(0 0 255 / 0.2);
  border: 3px solid blue;
}

构成属性

此属性是以下 CSS 属性的简写:

语法

css
/* Positional alignment */
place-items: center;
place-items: normal start;
place-items: center normal;
place-items: start legacy;
place-items: end normal;
place-items: self-start legacy;
place-items: self-end normal;
place-items: flex-start legacy;
place-items: flex-end normal;
place-items: anchor-center;

/* Baseline alignment */
place-items: baseline normal;
place-items: first baseline legacy;
place-items: last baseline normal;
place-items: stretch legacy;

/* Global values */
place-items: inherit;
place-items: initial;
place-items: revert;
place-items: revert-layer;
place-items: unset;

以下形式之一

  • 单个 align-items 值,用于设置块级和内联方向的对齐方式。
  • 一个 align-items 值,用于设置块级方向的对齐方式,后跟一个 justify-items 值,用于设置内联方向的对齐方式。

正式定义

初始值作为简写中的每个属性
应用于所有元素
继承性
计算值作为简写中的每个属性
动画类型离散

正式语法

place-items = 
<'align-items'> <'justify-items'>?

<align-items> =
normal |
stretch |
<baseline-position> |
[ <overflow-position>? <self-position> ] |
anchor-center |
dialog

<justify-items> =
normal |
stretch |
<baseline-position> |
<overflow-position>? [ <self-position> | left | right ] |
legacy |
legacy && [ left | right | center ] |
anchor-center |
dialog

<baseline-position> =
[ first | last ]? &&
baseline

<overflow-position> =
unsafe |
safe

<self-position> =
center |
start |
end |
self-start |
self-end |
flex-start |
flex-end

示例

在弹性容器中放置项目

在 flexbox 中,justify-selfjustify-items 不适用,因为主轴上的项目被视为一个组。因此,第二个值将被忽略。

CSS

css
#container {
  height: 200px;
  width: 240px;
  place-items: stretch; /* You can change this value by selecting another option in the list */
  background-color: #8c8c8c;
  display: flex;
}

结果

在网格容器中放置项目

以下网格容器中的项目小于它们所放置的网格区域,因此 place-items 将在块级和内联尺寸上移动它们。

CSS

css
#grid-container {
  height: 200px;
  width: 240px;
  place-items: stretch; /* You can change this value by selecting another option in the list */
  background-color: #8c8c8c;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}

#grid-container > div {
  width: 50px;
}

结果

规范

规范
CSS Box Alignment Module Level 3
# place-items-property

浏览器兼容性

另见