place-items
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
* Some parts of this feature may have varying levels of support.
Thuộc tính viết tắt CSS place-items căn chỉnh các mục theo cả hướng khối và hướng nội tuyến cùng lúc. Nó đặt giá trị cho các thuộc tính align-items và justify-items. Nếu giá trị thứ hai không được đặt, giá trị đầu tiên cũng sẽ được dùng cho nó.
Try it
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;
}
Các thuộc tính thành phần
Thuộc tính này là viết tắt cho các thuộc tính CSS sau:
Cú pháp
/* Căn chỉnh theo vị trí */
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;
/* Căn chỉnh theo baseline */
place-items: baseline normal;
place-items: first baseline legacy;
place-items: last baseline normal;
place-items: stretch legacy;
/* Giá trị toàn cục */
place-items: inherit;
place-items: initial;
place-items: revert;
place-items: revert-layer;
place-items: unset;
Các giá trị
Một trong các dạng sau:
- Một giá trị
align-itemsđơn, được dùng để đặt căn chỉnh theo cả hướng khối và hướng nội tuyến. - Một giá trị
align-items, đặt căn chỉnh theo hướng khối, theo sau bởi một giá trịjustify-items, đặt căn chỉnh theo hướng nội tuyến.
Định nghĩa chính thức
| Initial value | as each of the properties of the shorthand:
|
|---|---|
| Applies to | all elements |
| Inherited | no |
| Computed value | as each of the properties of the shorthand:
|
| Animation type | discrete |
Cú pháp chính thức
place-items =
<'align-items'> <'justify-items'>?
<align-items> =
normal |
stretch |
<baseline-position> |
<overflow-position>? <self-position>
<justify-items> =
normal |
stretch |
<baseline-position> |
<overflow-position>? [ <self-position> | left | right ] |
legacy |
legacy && [ left | right | center ]
<baseline-position> =
[ first | last ]? &&
baseline
<overflow-position> =
unsafe |
safe
<self-position> =
center |
start |
end |
self-start |
self-end |
flex-start |
flex-end
Ví dụ
>Đặt các mục trong flex container
Trong flexbox, justify-self hoặc justify-items không áp dụng, vì trên trục chính, các mục được coi là một nhóm. Do đó, giá trị thứ hai sẽ bị bỏ qua.
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;
}
Kết quả
Đặt các mục trong grid container
Grid container sau có các mục nhỏ hơn vùng grid mà chúng được đặt vào, do đó place-items sẽ di chuyển chúng theo hướng khối và hướng nội tuyến.
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;
}
Kết quả
Thông số kỹ thuật
| Specification |
|---|
| CSS Box Alignment Module Level 3> # place-items-property> |