

居中对齐弹性对象元素内的某个项:
#myBluep
{
align-self:center;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>手册网(www.shouce.ren)</title>
<style>
#main {
width: 220px;
height: 300px;
border: 1px solid black;
display: -webkit-flex; /* Safari */
-webkit-align-items: flex-start; /* Safari 7.0+ */
display: flex;
align-items: flex-start;
}
#main div {
-webkit-flex: 1; /* Safari 6.1+ */
flex: 1;
}
#myBlueDiv {
-webkit-align-self: center; /* Safari 7.0+ */
align-self: center;
}
</style>
</head>
<body>
<div id="main">
<div style="background-color:coral;">红色</div>
<div style="background-color:lightblue;" id="myBlueDiv">蓝色</div>
<div style="background-color:lightgreen;">带有更多内容的绿色 div</div>
</div>
<p><b>注意:</b> align-self 属性重写了容器的 align-items 属性。</p>
<p><b>注意:</b> Internet Explorer 10 及更早版本浏览器不支持 align-items 属性。</p>
<p><b>注意:</b> Safari 7.0 及更新版本通过 -webkit-align-items 属性支持该属性。</p>
</body>
</html>效果预览
表格中的数字表示支持该属性的第一个浏览器的版本号。
紧跟在 -webkit-, -ms- 或 -moz- 后的数字为支持该前缀属性的第一个版本。
| 属性 | |||||
|---|---|---|---|---|---|
| align-self | 21.0 | 11.0 | 20.0 | 9.0 7.0 -webkit- | 12.1 |
align-self 属性定义flex子项单独在侧轴(纵轴)方向上的对齐方式。
注意:align-self 属性可重写灵活容器的 align-items 属性。
| 默认值: | auto |
|---|---|
| 继承: | 否 |
| 可动画化: | 否。请参阅 CSS3动画属性、CSS3动画实例。 |
| 版本: | CSS3 |
| JavaScript 语法: | object.style.alignSelf="center" 效果预览 |
align-self: auto|stretch|center|flex-start|flex-end|baseline|initial|inherit;
| 值 | 描述 | 测试 |
|---|---|---|
| auto | 默认值。元素继承了它的父容器的 align-items 属性。如果没有父容器则为 "stretch"。 | 效果预览 |
| stretch | 元素被拉伸以适应容器。 | 效果预览 |
| center | 元素位于容器的中心。 | 效果预览 |
| flex-start | 元素位于容器的开头。 | 效果预览 |
| flex-end | 元素位于容器的结尾。 | 效果预览 |
| baseline | 元素位于容器的基线上。 | 效果预览 |
| initial | 设置该属性为它的默认值。请参阅 initial。 | 效果预览 |
| inherit | 从父元素继承该属性。请参阅 inherit。 |
