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

瀏覽器相容性

另見