aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/align-self/index.html
blob: 757f5d48bc63a9aa2d929622bdaa29b5460f343b (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
---
title: align-self
slug: Web/CSS/align-self
tags:
  - CSS
  - CSS 属性
  - flexbox
  - grid
  - 参考
translation_of: Web/CSS/align-self
---
<div>{{CSSRef}}</div>

<p><a href="/zh-CN/docs/CSS">CSS</a> 属性 <strong><code>align-self</code></strong> 会对齐当前 grid 或 flex 行中的元素,并覆盖已有的 <a href="/zh-CN/docs/Web/CSS/align-items"><code>align-items</code></a> 的值。In Grid, it aligns the item inside the <a href="/zh-CN/docs/Glossary/Grid_Areas">grid area</a>. 在 Flexbox 中,会按照 <a href="/zh-CN/docs/Glossary/Cross_Axis">cross axis</a>(当前 flex 元素排列方向的垂直方向)进行排列。</p>

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



<p>align-self属性不适用于块类型的盒模型和表格单元。如果任何 flexbox 元素的侧轴方向 margin 值设置为 auto,则会忽略 <code>align-self</code></p>

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

<pre class="brush: css no-line-numbers notranslate">/* Keyword values */
align-self: auto;
align-self: normal;

/* Positional alignment */
/* align-self does not take left and right values */
align-self: center; /* Put the item around the center */
align-self: start;  /* Put the item at the start */
align-self: end;    /* Put the item at the end */
align-self: self-start; /* Align the item flush at the start */
align-self: self-end;   /* Align the item flush at the end */
align-self: flex-start; /* Put the flex item at the start */
align-self: flex-end;   /* Put the flex item at the end */

/* Baseline alignment */
align-self: baseline;
align-self: first baseline;
align-self: last baseline;
align-self: stretch; /* Stretch 'auto'-sized items to fit the container */

/* Overflow alignment */
align-self: safe center;
align-self: unsafe center;

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

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

<dl>
 <dt><code>auto</code></dt>
 <dd>设置为父元素的 {{cssxref("align-items") }} 值。</dd>
 <dt><code>normal</code></dt>
</dl>

<dl>
 <dd>效果取决于当前的布局模式:</dd>
 <dd>
 <ul>
  <li>绝对定位布局中,normal在绝对定位的替代元素上表现为<code>start</code>,在<em>所有其他</em>绝对定位元素上表现为<code>stretch</code></li>
  <li>在绝对定位的静态元素上表现为<code>stretch</code></li>
  <li>flex布局中表现为stretch。</li>
  <li>For grid items, this keyword leads to a behavior similar to the one of <code>stretch</code>, except for boxes with an aspect ratio or an intrinsic sizes where it behaves like <code>start</code>.</li>
  <li>在块级和表格单元中无效。</li>
 </ul>
 </dd>
 <dt><code>self-start</code></dt>
 <dd>Aligns the items to be flush with the edge of the alignment container corresponding to the item's start side in the cross axis.</dd>
 <dt><code>self-end</code></dt>
 <dd>Aligns the items to be flush with the edge of the alignment container corresponding to the item's end side in the cross axis.</dd>
 <dt><code>flex-start</code></dt>
 <dd>flex 元素会对齐到 cross-axis 的首端。</dd>
 <dt><code>flex-end</code></dt>
 <dd>flex 元素会对齐到 cross-axis 的尾端。</dd>
 <dt><code>center</code></dt>
 <dd>flex 元素会对齐到 cross-axis 的中间,如果该元素的 cross-size 尺寸大于 flex 容器,将在两个方向均等溢出。</dd>
 <dt><code>baseline</code><br>
 <code>first baseline</code><br>
 <code>last baseline</code></dt>
 <dd>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.<br>
 The fallback alignment for <code>first baseline</code> is <code>start</code>, the one for <code>last baseline</code> is <code>end</code>.</dd>
 <dd>flex 元素将会基于容器的宽和高,按照自身 margin box 的 cross-size 拉伸。If the combined size of the items along the cross axis is less than the size of the alignment container and the item is <code>auto</code>-sized, its size is increased equally (not proportionally), while still respecting the constraints imposed by {{cssxref("max-height")}}/{{cssxref("max-width")}} (or equivalent functionality), so that the combined size of all <code>auto</code>-sized items exactly fills the alignment container along the cross axis.</dd>
 <dt><code>safe</code></dt>
 <dd>If the size of the item overflows the alignment container, the item is instead aligned as if the alignment mode were <code>start</code>.</dd>
 <dt><code>unsafe</code></dt>
 <dd>Regardless of the relative sizes of the item and alignment container, the given alignment value is honored.</dd>
</dl>

<h3 id="形式化语法">形式化语法</h3>

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

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

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

<pre class="brush: html notranslate">&lt;section&gt;
  &lt;div&gt;Item #1&lt;/div&gt;
  &lt;div&gt;Item #2&lt;/div&gt;
  &lt;div&gt;Item #3&lt;/div&gt;
&lt;/section&gt;</pre>

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

<pre class="brush: css notranslate">section {
  display: flex;
  align-items: center;
  height: 120px;
  background: beige;
}

div {
  height: 60px;
  background: cyan;
  margin: 5px;
}

div:nth-child(3) {
  align-self: flex-end;
  background: pink;
}</pre>

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

<p>{{EmbedLiveSample('Example')}}</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', '#align-self-property', 'align-self') }}</td>
   <td>{{ Spec2('CSS3 Box Alignment') }}</td>
   <td></td>
  </tr>
 </tbody>
</table>

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



<h3 id="在_Flex_布局中的兼容性">在 Flex 布局中的兼容性</h3>

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

<h3 id="在_Grid_布局中的兼容性">在 Grid 布局中的兼容性</h3>

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

<h2 id="参考">参考</h2>

<ul>
 <li>CSS Flexbox 指南:<em><a href="/zh-CN/docs/Web/CSS/CSS_Flexible_Box_Layout/Basic_Concepts_of_Flexbox">Basic Concepts of Flexbox</a></em></li>
 <li>CSS Flexbox 指南:<em><a href="/zh-CN/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 指南:<em><a href="/zh-CN/docs/Web/CSS/CSS_Grid_Layout/Box_Alignment_in_CSS_Grid_Layout">Box alignment in CSS Grid layouts</a></em></li>
 <li><a href="/zh-CN/docs/Web/CSS/CSS_Box_Alignment">CSS Box Alignment</a></li>
 <li>{{cssxref("align-items")}} 属性</li>
</ul>