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;

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

形式化语法

auto | normal | stretch | <baseline-position> | <overflow-position>? <self-position>

where
<baseline-position> = [ first | last ]? baseline
<overflow-position> = unsafe | safe
<self-position> = center | start | end | self-start | self-end | flex-start | flex-end

示例

HTML

<section>
  <div>Item #1</div>
  <div>Item #2</div>
  <div>Item #3</div>
</section>

CSS

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;
}

结果

规范

规范 状态 备注
CSS Box Alignment Module Level 3
align-self
Working Draft

浏览器兼容性

在 Flex 布局中的兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Supported in Flex LayoutChrome Full support 36
Full support 36
Partial support 29
Notes
Notes Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Chrome 52.
Full support 21
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Edge Full support 12Firefox Full support 20
Notes
Full support 20
Notes
Notes Before Firefox 27, only single-line flexbox is supported.
No support 18 — 20
Disabled
Disabled From version 18 until version 20 (exclusive): this feature is behind the layout.css.flexbox.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Full support 49
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Full support 48
Prefixed Disabled
Prefixed Implemented with the vendor prefix: -webkit-
Disabled From version 48: this feature is behind the layout.css.prefixes.webkit preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE Full support 11Opera Full support 12.1Safari Full support 9
Full support 9
Full support 6.1
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
WebView Android Full support 37
Full support 37
Partial support 4.4
Notes
Notes Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Chrome 52.
Full support ≤37
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Chrome Android Full support 36
Full support 36
Partial support 29
Notes
Notes Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Chrome 52.
Full support 25
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Firefox Android Full support 20
Notes
Full support 20
Notes
Notes Before Firefox 27, only single-line flexbox is supported.
No support 18 — 20
Disabled
Disabled From version 18 until version 20 (exclusive): this feature is behind the layout.css.flexbox.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Full support 49
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Full support 48
Prefixed Disabled
Prefixed Implemented with the vendor prefix: -webkit-
Disabled From version 48: this feature is behind the layout.css.prefixes.webkit preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 12.1Safari iOS Full support 9
Full support 9
Full support 6.1
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Samsung Internet Android Full support 3.0
Full support 3.0
Partial support 2.0
Notes
Notes Older versions of the specification treat absolute positioned children as though they are a 0 by 0 flex item. Later specification versions take the children out of the flow and set their positions based on align and justify properties. Chrome implements the new behavior beginning with Samsung Internet 6.0.
Full support 1.5
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
baselineChrome Full support 57Edge Full support 79Firefox Full support 45IE No support NoOpera Full support 44Safari No support NoWebView Android Full support 57Chrome Android Full support 57Firefox Android Full support 45Opera Android Full support 43Safari iOS No support NoSamsung Internet Android Full support 7.0
first baseline and last baselineChrome Full support 57Edge Full support 79Firefox Full support 52IE No support NoOpera Full support 44Safari No support NoWebView Android Full support 57Chrome Android Full support 57Firefox Android Full support 52Opera Android Full support 43Safari iOS No support NoSamsung Internet Android Full support 7.0
left and rightChrome No support NoEdge No support NoFirefox Full support 52IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android Full support 52Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
safe and unsafeChrome No support No
Notes
No support No
Notes
Notes This value is recognized, but has no effect.
Edge No support No
Notes
No support No
Notes
Notes This value is recognized, but has no effect.
Firefox Full support 63IE No support NoOpera No support No
Notes
No support No
Notes
Notes This value is recognized, but has no effect.
Safari No support No
Notes
No support No
Notes
Notes This value is recognized, but has no effect.
WebView Android No support No
Notes
No support No
Notes
Notes This value is recognized, but has no effect.
Chrome Android No support No
Notes
No support No
Notes
Notes This value is recognized, but has no effect.
Firefox Android Full support 63Opera Android No support No
Notes
No support No
Notes
Notes This value is recognized, but has no effect.
Safari iOS No support No
Notes
No support No
Notes
Notes This value is recognized, but has no effect.
Samsung Internet Android No support No
Notes
No support No
Notes
Notes This value is recognized, but has no effect.
start and endChrome Full support 57Edge Full support 79Firefox Full support 45IE No support NoOpera Full support 44Safari No support NoWebView Android Full support 57Chrome Android Full support 57Firefox Android Full support 45Opera Android Full support 43Safari iOS No support NoSamsung Internet Android Full support 7.0
stretchChrome Full support 57Edge Full support 79Firefox Full support 52IE No support NoOpera Full support 44Safari No support NoWebView Android Full support 57Chrome Android Full support 57Firefox Android Full support 52Opera Android Full support 43Safari iOS No support NoSamsung Internet Android Full support 7.0

Legend

Full support  
Full support
No support  
No support
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

在 Grid 布局中的兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Supported in Grid LayoutChrome Full support 57Edge Full support 16Firefox Full support 52IE Partial support 10
Prefixed Notes
Partial support 10
Prefixed Notes
Prefixed Implemented with the vendor prefix: -ms-
Notes Internet Explorer 10 and 11 have the property -ms-grid-row-align, which acts in a similar way to align-self.
Opera Full support 44Safari Full support 10.1WebView Android Full support 57Chrome Android Full support 52Firefox Android Full support 52Opera Android Full support 43Safari iOS Full support 10.3Samsung Internet Android Full support 6.2

Legend

Full support  
Full support
Partial support  
Partial support
See implementation notes.
See implementation notes.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

参考