--- title: place-items slug: Web/CSS/place-items translation_of: Web/CSS/place-items ---
CSS 中的 place-items
是一个简写属性 ,它允许你在相关的布局(如 Grid 或 Flexbox)中可以同时沿着块级和内联方向对齐元素 (例如:{{CSSxRef("align-items")}} 和 {{CSSxRef("justify-items")}} 属性) 。如果未提供第二个值,则第一个值作为第二个值的默认值。
该属性是以下两个 CSS 属性的简写:
/* 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;
auto
auto
实际的值继承父自元素的 justify-items
值,除非该元素没有父元素或是用了绝对定位。在这些示例中,auto
表示 normal
。normal
normal
的效果取决于我们使用哪种布局方式:
normal
和 start
一样。start
,在所有其他绝对定位的元素上表现类似 stretch
。stretch
的行为相似,但是具有宽高比和固有尺寸的元素行为和 start
相似。start
end
flex-start
start
。flex-end
self-start
self-end
center
left
start
类似。right
start
类似。baseline
first baseline
last baseline
first baseline
的回退对齐方式为 start
,last baseline
则为 end
。stretch
auto
的项将增加同等的大小(不是按比例),但也会受到 {{CSSxRef("max-height")}}/{{CSSxRef("max-width")}} (或等同的功能)的限制,因此所有项刚好能填满对齐容器。{{cssinfo}}
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; }
<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>
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; });
#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); }
{{EmbedLiveSample("Placing_items_in_a_flex_container", 260, 290)}}
Specification | Status | Comment |
---|---|---|
{{SpecName("CSS3 Box Alignment", "#place-items-property", "place-items")}} | {{Spec2("CSS3 Box Alignment")}} | Initial definition |
{{Compat("css.properties.place-items")}}