aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/align-items/index.html
blob: c534cc0d2b9259f90441ba049df076dc54c28cfc (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
282
283
284
285
286
287
---
title: align-items
slug: Web/CSS/align-items
tags:
  - CSS
  - CSS Flexible Boxes
  - CSS 属性
  - 参考
translation_of: Web/CSS/align-items
---
<div>{{ CSSRef}}</div>

<div></div>

<p>CSS align-items属性将所有直接子节点上的align-self值设置为一个组。 align-self属性设置项目在其包含块中在交叉轴方向上的对齐方式。</p>

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



<p>目前,Flexbox和CSS网格布局支持此属性。在Flexbox中,它控制十字轴上项目的对齐方式,在网格布局中,它控制块轴上项目的对齐方式。</p>

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

<pre class="brush: css notranslate">/* Basic keywords */
align-items: normal;
align-items: stretch;

/* Positional alignment */
align-items: center; /* Pack items around the center */
align-items: start; /* Pack items from the start */
align-items: end; /* Pack items from the end */
align-items: flex-start; /* Pack flex items from the start */
align-items: flex-end; /* Pack flex items from the end */
align-items: self-start;
align-items: self-end;

/* Baseline alignment */
align-items: baseline;
align-items: first baseline;
align-items: last baseline; /* Overflow alignment (for positional alignment only) */
align-items: safe center;
align-items: unsafe center;

/* Global values */
align-items: inherit;
align-items: initial;
align-items: unset;</pre>

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

<dl>
 <dt><code>normal</code></dt>
 <dd>这个关键字的效果取决于我们处在什么布局模式中:
 <ul>
  <li>在绝对定位的布局中,对于被替代的绝对定位盒子,这个效果和<code>start?</code>的效果的一样;对于其他所有绝对定位的盒子,这个效果和<code>stretch</code>的效果一样。 </li>
  <li>在绝对定位布局的静态位置上,效果和<code>stretch</code>一样。</li>
  <li>对于那些弹性项目而言,效果和<code>stretch</code>一样。</li>
  <li>对于那些网格项目而言,效果和<code>stretch</code>一样,除了有部分比例或者一个固定大小的盒子的效果像<code>start</code></li>
  <li>这个属性不适用于会计盒子和表格。</li>
 </ul>
 </dd>
</dl>

<dl>
 <dt><code>flex-start</code></dt>
 <dd>元素向侧轴起点对齐。</dd>
 <dt><code>flex-end</code></dt>
 <dd>元素向侧轴终点对齐。</dd>
 <dt><code>start</code></dt>
 <dd>The item is packed flush to each other toward the start edge of the alignment container in the appropriate axis.</dd>
 <dt><code>end</code></dt>
 <dd>The item is packed flush to each other toward the end edge of the alignment container in the appropriate axis.</dd>
 <dt><code>center</code></dt>
 <dd>元素在侧轴居中。如果元素在侧轴上的高度高于其容器,那么在两个方向上溢出距离相同。</dd>
 <dt><code>left</code></dt>
 <dd>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 <code>start</code>.</dd>
 <dt><code>right</code></dt>
 <dd>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 <code>start</code>.</dd>
 <dt><code>self-start</code></dt>
 <dd>The items is packed flush to the edge of the alignment container of the start side of the item, in the appropriate axis.</dd>
 <dt><code>self-end</code></dt>
 <dd>The item is packed flush to the edge of the alignment container of the end side of the item, in the appropriate axis.</dd>
 <dt><code>baseline</code></dt>
 <dt><code>first baseline<br>
 last baseline</code></dt>
 <dd>所有元素向基线对齐。侧轴起点到元素基线距离最大的元素将会于侧轴起点对齐以确定基线。</dd>
 <dt><code>stretch</code></dt>
 <dd>弹性元素被在侧轴方向被拉伸到与容器相同的高度或宽度。</dd>
 <dt><code>safe</code></dt>
 <dd>Used alongside an alignment keyword. If the chosen keyword means that the item overflows the alignment container causing data loss, the item is instead aligned as if the alignment mode were <code>start</code>.</dd>
 <dt><code>unsafe</code></dt>
 <dd>Used alongside an alignment keyword. Regardless of the relative sizes of the item and alignment container and whether overflow which causes data loss might happen, the given alignment value is honored.</dd>
 <dt>
 <h3 id="语法格式">语法格式</h3>
 </dt>
</dl>

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

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

<h3 id="CSS">CSS</h3>

<pre class="brush: css; highlight[4] notranslate">#container {
  height:200px;
  width: 240px;
  align-items: center; /* Can be changed in the live sample */
  background-color: #8c8c8c;
}

.flex {
  display: flex;
  flex-wrap: wrap;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, 50px);
}

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>

<h3 id="HTML">HTML</h3>

<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;align-items: &lt;/label&gt;
  &lt;select id="values"&gt;
    &lt;option value="normal"&gt;normal&lt;/option&gt;
    &lt;option value="flex-start"&gt;flex-start&lt;/option&gt;
    &lt;option value="flex-end"&gt;flex-end&lt;/option&gt;
    &lt;option value="center" selected&gt;center&lt;/option&gt;
    &lt;option value="baseline"&gt;baseline&lt;/option&gt;
    &lt;option value="stretch"&gt;stretch&lt;/option&gt;

    &lt;option value="start"&gt;start&lt;/option&gt;
    &lt;option value="end"&gt;end&lt;/option&gt;
    &lt;option value="self-start"&gt;self-start&lt;/option&gt;
    &lt;option value="self-end"&gt;self-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="first baseline"&gt;first baseline&lt;/option&gt;
    &lt;option value="last baseline"&gt;last baseline&lt;/option&gt;

    &lt;option value="safe center"&gt;safe center&lt;/option&gt;
    &lt;option value="unsafe center"&gt;unsafe center&lt;/option&gt;
    &lt;option value="safe right"&gt;safe right&lt;/option&gt;
    &lt;option value="unsafe right"&gt;unsafe right&lt;/option&gt;
    &lt;option value="safe end"&gt;safe end&lt;/option&gt;
    &lt;option value="unsafe end"&gt;unsafe end&lt;/option&gt;
    &lt;option value="safe self-end"&gt;safe self-end&lt;/option&gt;
    &lt;option value="unsafe self-end"&gt;unsafe self-end&lt;/option&gt;
    &lt;option value="safe flex-end"&gt;safe flex-end&lt;/option&gt;
    &lt;option value="unsafe flex-end"&gt;unsafe flex-end&lt;/option&gt;
  &lt;/select&gt;
&lt;/div&gt;
</pre>

<div class="hidden">
<h3 id="JavaScript">JavaScript</h3>

<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.alignItems = evt.target.value;
});

display.addEventListener('change', function (evt) {
  container.className = evt.target.value;
});
</pre>
</div>

<h3 id="Result">Result</h3>

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

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

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">说明</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName("CSS3 Box Alignment", "#propdef-align-items", "align-items")}}</td>
   <td>{{Spec2("CSS3 Box Alignment")}}</td>
   <td>Update to latest syntax definitions.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS3 Flexbox', '#propdef-align-items', 'align-items')}}</td>
   <td>{{Spec2('CSS3 Flexbox')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

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

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



<h3 id="在弹性盒子布局中的兼容性">在弹性盒子布局中的兼容性</h3>

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

<h3 id="在网格布局中的兼容性">在网格布局中的兼容性</h3>

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

<h2 id="相关链接">相关链接</h2>

<ul>
 <li><a href="/en-US/docs/CSS/Using_CSS_flexible_boxes" title="/en-US/docs/CSS/Using_CSS_flexible_boxes">使用CSS弹性框</a></li>
</ul>