--- title: align-self slug: Web/CSS/align-self tags: - CSS - CSS 属性 - flexbox - grid - 参考 translation_of: Web/CSS/align-self ---
CSS 属性 align-self 会对齐当前 grid 或 flex 行中的元素,并覆盖已有的 align-items 的值。In Grid, it aligns the item inside the grid area. 在 Flexbox 中,会按照 cross axis(当前 flex 元素排列方向的垂直方向)进行排列。
align-self属性不适用于块类型的盒模型和表格单元。如果任何 flexbox 元素的侧轴方向 margin 值设置为 auto,则会忽略 align-self。
/* 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;
autonormalstart,在所有其他绝对定位元素上表现为stretch。stretch。stretch, except for boxes with an aspect ratio or an intrinsic sizes where it behaves like start.self-startself-endflex-startflex-endcenterbaselinefirst baselinelast baselinefirst baseline is start, the one for last baseline is end.auto-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 auto-sized items exactly fills the alignment container along the cross axis.safestart.unsafe<section> <div>Item #1</div> <div>Item #2</div> <div>Item #3</div> </section>
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;
}
{{EmbedLiveSample('Example')}}
| 规范 | 状态 | 备注 |
|---|---|---|
| {{ SpecName('CSS3 Box Alignment', '#align-self-property', 'align-self') }} | {{ Spec2('CSS3 Box Alignment') }} |
{{Compat("css.properties.align-self.flex_context")}}
{{Compat("css.properties.align-self.grid_context")}}