aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/place-items/index.html
blob: 914901a09c512c2a69c8ecd0371e0e008985f752 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
---
title: place-items
slug: Web/CSS/place-items
translation_of: Web/CSS/place-items
---
<div>{{CSSRef}}</div>



<p><a href="/en-US/docs/Web/CSS">CSS</a> 中的 <strong><code>place-items</code></strong> 是一个<a href="/en-US/docs/Web/CSS/Shorthand_properties">简写属性</a> ,它允许你在相关的布局(如 <a href="https://wiki.developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout">Grid</a> 或 <a href="https://wiki.developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout">Flexbox</a>)中可以同时沿着块级和内联方向对齐元素 (例如:{{CSSxRef("align-items")}} 和 {{CSSxRef("justify-items")}} 属性) 。如果未提供第二个值,则第一个值作为第二个值的默认值。</p>

<div>{{EmbedInteractiveExample("pages/css/place-items.html")}}</div>



<h2 id="构成属性">构成属性</h2>

<p>该属性是以下两个 CSS 属性的简写:</p>

<ul>
 <li><a href="/en-US/docs/Web/CSS/align-items"><code>align-items</code></a></li>
 <li><a href="/en-US/docs/Web/CSS/justify-items"><code>justify-items</code></a></li>
</ul>

<h2 id="语法">语法</h2>

<pre class="brush:css notranslate">/* 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;
</pre>

<h3 id="值"></h3>

<dl>
 <dt><code>auto</code></dt>
 <dd><code>auto</code> 实际的值继承父自元素的 <code>justify-items</code> 值,除非该元素没有父元素或是用了绝对定位。在这些示例中,<code>auto</code> 表示 <code>normal</code> 。</dd>
 <dt><code>normal</code></dt>
 <dd><code>normal</code> 的效果取决于我们使用哪种布局方式:
 <ul>
  <li>在块级布局中,<code>normal</code><code>start</code> 一样。</li>
  <li>在绝对定位布局中,关键字在<em>被替换的</em>绝对定位元素上的行为类似于 <code>start</code>,在<em>所有其他</em>绝对定位的元素上表现类似 <code>stretch</code></li>
  <li>在表格单元格布局中,此关键字没有意义,因为该属性<em>被忽略</em></li>
  <li>在 flexbox 布局中,此关键字没有意义,因为该属性<em>被忽略</em></li>
  <li>在 grid 布局中,此关键字和 <code>stretch</code> 的行为相似,但是具有宽高比和固有尺寸的元素行为和 <code>start</code> 相似。</li>
 </ul>
 </dd>
 <dt><code>start</code></dt>
 <dd>在适当的轴线上,元素块沿着对齐容器的起始边缘对齐。</dd>
 <dt><code>end</code></dt>
 <dd>在适当的轴线上,元素块沿着对齐容器的结束边缘对齐。</dd>
 <dt><code>flex-start</code></dt>
 <dd>对齐方式取决于 flex 容器的的开始方向。(水平和垂直两个方向)<br>
 这只对 flex 布局元素生效,对于不是 flex 容器的子元素,它被视为 <code>start</code></dd>
 <dt><code>flex-end</code></dt>
 <dd>对齐方式取决于 flex 容器的的结束方向。(水平和垂直两个方向)<br>
 这只对 flex 布局元素生效,对于不是 flex 容器的子元素,它被视为 <font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">end</span></font> 。</dd>
 <dt><code>self-start</code></dt>
 <dd>沿着轴线的头部对齐。</dd>
 <dt><code>self-end</code></dt>
 <dd>沿着轴线的尾部对齐。</dd>
 <dt><code>center</code></dt>
 <dd>沿着对齐容器的中心对齐。</dd>
 <dt><code>left</code></dt>
 <dd>沿着对齐容器的左侧对齐,如果属性的轴不与内联轴平行,则该值的行为和 <code>start</code> 类似。</dd>
 <dt><code>right</code></dt>
 <dd>沿着对齐容器的右侧对齐,如果属性的轴不与内联轴平行,则该值的行为和 <code>start</code> 类似。</dd>
 <dt><code>baseline<br>
 first baseline</code><br>
 <code>last baseline</code></dt>
 <dd>指定参与第一个或最后一个基线对齐:元素的第一个或最后一个基线集的对齐基线与基线共享组中所有框共享的第一个或最后一个基线集中相应的基线对齐。</dd>
 <dd><code>first baseline</code> 的回退对齐方式为 <code>start</code><code>last baseline</code> 则为 <code>end</code></dd>
 <dt><code>stretch</code></dt>
 <dd>如果子项加起来的尺寸小于对齐容器的尺寸,则任何未尺寸为 <code>auto</code> 的项将增加同等的大小(不是按比例),但也会受到 {{CSSxRef("max-height")}}/{{CSSxRef("max-width")}} (或等同的功能)的限制,因此所有项刚好能填满对齐容器。</dd>
</dl>

<h2 id="形式定义">形式定义</h2>

<p>{{cssinfo}}</p>

<h2 id="形式语法">形式语法</h2>

<pre class="syntaxbox notranslate">{{csssyntax}}</pre>

<h2 id="示例">示例</h2>

<h3 id="在弹性容器中排列元素">在弹性容器中排列元素</h3>

<div class="hidden">
<pre class="brush: css; notranslate">div &gt; 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;
}</pre>

<h4 id="HTML">HTML</h4>

<pre class="brush: html notranslate">&lt;div id="container" class="flex"&gt;
  &lt;div id="item1"&gt;1&lt;/div&gt;
  &lt;div id="item2"&gt;2&lt;/div&gt;
  &lt;div id="item3"&gt;3&lt;/div&gt;
  &lt;div id="item4"&gt;4&lt;/div&gt;
  &lt;div id="item5"&gt;5&lt;/div&gt;
  &lt;div id="item6"&gt;6&lt;/div&gt;
&lt;/div&gt;

&lt;div class="row"&gt;
  &lt;label for="display"&gt;display: &lt;/label&gt;
  &lt;select id="display"&gt;
    &lt;option value="flex"&gt;flex&lt;/option&gt;
    &lt;option value="grid"&gt;grid&lt;/option&gt;
  &lt;/select&gt;
&lt;/div&gt;

&lt;div class="row"&gt;
  &lt;label for="values"&gt;place-items: &lt;/label&gt;
  &lt;select id="values"&gt;
    &lt;option value="start"&gt;start&lt;/option&gt;
    &lt;option value="center"&gt;center&lt;/option&gt;
    &lt;option value="end"&gt;end&lt;/option&gt;
    &lt;option value="left"&gt;left&lt;/option&gt;
    &lt;option value="right"&gt;right&lt;/option&gt;
    &lt;option value="auto center"&gt;auto center&lt;/option&gt;
    &lt;option value="normal start"&gt;normal start&lt;/option&gt;
    &lt;option value="center normal"&gt;center normal&lt;/option&gt;
    &lt;option value="start auto"&gt;start auto&lt;/option&gt;
    &lt;option value="end normal"&gt;end normal&lt;/option&gt;
    &lt;option value="self-start auto"&gt;self-start auto&lt;/option&gt;
    &lt;option value="self-end normal"&gt;self-end normal&lt;/option&gt;
    &lt;option value="flex-start auto"&gt;flex-start auto&lt;/option&gt;
    &lt;option value="flex-end normal"&gt;flex-end normal&lt;/option&gt;
    &lt;option value="left auto"&gt;left auto&lt;/option&gt;
    &lt;option value="right normal"&gt;right normal&lt;/option&gt;
    &lt;option value="baseline normal"&gt;baseline normal&lt;/option&gt;
    &lt;option value="first baseline auto"&gt;first baseline auto&lt;/option&gt;
    &lt;option value="last baseline normal"&gt;last baseline normal&lt;/option&gt;
    &lt;option value="stretch auto"&gt;stretch auto&lt;/option&gt;
  &lt;/select&gt;
&lt;/div&gt;
</pre>

<h4 id="JavaScript">JavaScript</h4>

<pre class="brush: js notranslate">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;
});
</pre>
</div>

<h4 id="CSS">CSS</h4>

<pre class="brush: css; highlight[4] notranslate">#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);
}
</pre>

<h4 id="结果">结果</h4>

<p>{{EmbedLiveSample("Placing_items_in_a_flex_container", 260, 290)}}</p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName("CSS3 Box Alignment", "#place-items-property", "place-items")}}</td>
   <td>{{Spec2("CSS3 Box Alignment")}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("css.properties.place-items")}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>CSS Flexbox Guide: <em><a href="/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Basic_Concepts_of_Flexbox">Basic Concepts of Flexbox</a></em></li>
 <li>CSS Flexbox Guide: <em><a href="/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Aligning_Items_in_a_Flex_Container">Aligning items in a flex container</a></em></li>
 <li>CSS Grid Guide: <em><a href="/en-US/docs/Web/CSS/CSS_Grid_Layout/Box_Alignment_in_CSS_Grid_Layout">Box alignment in CSS Grid layouts</a></em></li>
 <li><a href="/en-US/docs/Web/CSS/CSS_Box_Alignment">CSS Box Alignment</a></li>
 <li>The {{CSSxRef("align-items")}} property</li>
 <li>The {{CSSxRef("align-self")}} property</li>
 <li>The {{CSSxRef("justify-items")}} property</li>
 <li>The {{CSSxRef("justify-self")}} property</li>
</ul>