place-items - CSS: Cascading Style Sheets 编辑
The CSS place-items
shorthand property allows you to align items along both the block and inline directions at once (i.e. the align-items
and justify-items
properties) in a relevant layout system such as Grid or Flexbox. If the second value is not set, the first value is also used for it.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Constituent properties
This property is a shorthand for the following CSS properties:
Syntax
/* Keyword values */
place-items: auto center;
place-items: normal start;
/* Positional alignment */
place-items: center normal;
place-items: start auto;
place-items: end normal;
place-items: self-start auto;
place-items: self-end normal;
place-items: flex-start auto;
place-items: flex-end normal;
place-items: left auto;
place-items: right normal;
/* Baseline alignment */
place-items: baseline normal;
place-items: first baseline auto;
place-items: last baseline normal;
place-items: stretch auto;
/* Global values */
place-items: inherit;
place-items: initial;
place-items: unset;
Values
auto
- The value used is the value of the
justify-items
property of the parents box, unless the box has no parent, or is absolutely positioned, in these cases,auto
representsnormal
. normal
- The effect of this keyword is dependent of the layout mode we are in:
- In block-level layouts, the keyword is a synonym of
start
. - In absolutely-positioned layouts, the keyword behaved like
start
on replaced absolutely-positioned boxes, and asstretch
on all other absolutely-positioned boxes. - In table cell layouts, this keyword has no meaning as this property is ignored.
- In flexbox layouts, this keyword has no meaning as this property is ignored.
- In grid layouts, this keyword leads to a behavior similar to the one of
stretch
, except for boxes with an aspect ratio or an intrinsic sizes where it behaves likestart
.
- In block-level layouts, the keyword is a synonym of
start
- The item is packed flush to each other toward the start edge of the alignment container in the appropriate axis.
end
- The item is packed flush to each other toward the end edge of the alignment container in the appropriate axis.
flex-start
- The item is packed flush to each other toward the edge of the alignment container depending on the flex container's main-start or cross-start side.
This only applies to flex layout items. For items that are not children of a flex container, this value is treated likestart
. flex-end
- The item is packed flush to each other toward the edge of the alignment container depending on the flex container's main-end or cross-end side.
This only applies to flex layout items. For items that are not children of a flex container, this value is treated likeend
. self-start
- The item is packed flush to the edge of the alignment container of the start side of the item, in the appropriate axis.
self-end
- The item is packed flush to the edge of the alignment container of the end side of the item, in the appropriate axis.
center
- The items are packed flush to each other toward the center of the of the alignment container.
left
- The items are packed flush to each other toward the left edge of the alignment container. If the property’s axis is not parallel with the inline axis, this value behaves like
start
. right
- The items are packed flush to each other toward the right edge of the alignment container in the appropriate axis. If the property’s axis is not parallel with the inline axis, this value behaves like
start
. baseline
first baselinelast baseline
- Specifies participation in first- or last-baseline alignment: aligns the alignment baseline of the box’s first or last baseline set with the corresponding baseline in the shared first or last baseline set of all the boxes in its baseline-sharing group.
The fallback alignment forfirst baseline
isstart
, the one forlast baseline
isend
. stretch
- If the combined size of the items is less than the size of the alignment container, any
auto
-sized items have their size increased equally (not proportionally), while still respecting the constraints imposed bymax-height
/max-width
(or equivalent functionality), so that the combined size exactly fills the alignment container.
Formal definition
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 |
Formal syntax
<'align-items'> <'justify-items'>?
Examples
Placing items in a flex container
div > div {
box-sizing: border-box;
border: 2px solid #8c8c8c;
width: 50px;
display: flex;
align-items: center;
justify-content: center;
}
#item1 {
background-color: #8cffa0;
min-height: 30px;
}
#item2 {
background-color: #a0c8ff;
min-height: 50px;
}
#item3 {
background-color: #ffa08c;
min-height: 40px;
}
#item4 {
background-color: #ffff8c;
min-height: 60px;
}
#item5 {
background-color: #ff8cff;
min-height: 70px;
}
#item6 {
background-color: #8cffff;
min-height: 50px;
font-size: 30px;
}
select {
font-size: 16px;
}
.row {
margin-top: 10px;
}
HTML
<div id="container" class="flex">
<div id="item1">1</div>
<div id="item2">2</div>
<div id="item3">3</div>
<div id="item4">4</div>
<div id="item5">5</div>
<div id="item6">6</div>
</div>
<div class="row">
<label for="display">display: </label>
<select id="display">
<option value="flex">flex</option>
<option value="grid">grid</option>
</select>
</div>
<div class="row">
<label for="values">place-items: </label>
<select id="values">
<option value="start">start</option>
<option value="center">center</option>
<option value="end">end</option>
<option value="left">left</option>
<option value="right">right</option>
<option value="auto center">auto center</option>
<option value="normal start">normal start</option>
<option value="center normal">center normal</option>
<option value="start auto">start auto</option>
<option value="end normal">end normal</option>
<option value="self-start auto">self-start auto</option>
<option value="self-end normal">self-end normal</option>
<option value="flex-start auto">flex-start auto</option>
<option value="flex-end normal">flex-end normal</option>
<option value="left auto">left auto</option>
<option value="right normal">right normal</option>
<option value="baseline normal">baseline normal</option>
<option value="first baseline auto">first baseline auto</option>
<option value="last baseline normal">last baseline normal</option>
<option value="stretch auto">stretch auto</option>
</select>
</div>
JavaScript
var values = document.getElementById('values');
var display = document.getElementById('display');
var container = document.getElementById('container');
values.addEventListener('change', function (evt) {
container.style.placeItems = evt.target.value;
});
display.addEventListener('change', function (evt) {
container.className = evt.target.value;
});
CSS
#container {
height:200px;
width: 240px;
place-items: center; /* You can change this value by selecting another option in the list */
background-color: #8c8c8c;
}
.flex {
display: flex;
flex-wrap: wrap;
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, 50px);
}
Result
Specifications
Specification | Status | Comment |
---|---|---|
CSS Box Alignment Module Level 3 The definition of 'place-items' in that specification. | Working Draft | Initial definition |
Browser compatibility
BCD tables only load in the browser
See also
- CSS Flexbox Guide: Basic Concepts of Flexbox
- CSS Flexbox Guide: Aligning items in a flex container
- CSS Grid Guide: Box alignment in CSS Grid layouts
- CSS Box Alignment
- The
align-items
property - The
align-self
property - The
justify-items
property - The
justify-self
property
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论