CSS Flexbox 的 align-self:单独对齐一个项目
想让单个 Flex 项目在交叉轴上换一个位置,就在该项目上设置 align-self。父容器的 align-items 决定默认对齐,align-self: auto 会使用这个默认值。水平书写时,row 的交叉轴是垂直方向,column 的交叉轴是水平方向。
设置容器的默认对齐与项目的单独对齐
下列代码在父容器设置 align-items: center,在需要单独对齐的项目上设置 align-self: flex-end。如果卡片外面还有一层容器,要设置的是直接参与 Flex 布局的外层元素。给内部后代设置该属性,外层元素相对其同级元素的位置不会改变。
.container {
display: flex;
align-items: center;
}
.item-special {
align-self: flex-end;
}
| 取值 | 示例中的效果 |
|---|---|
auto |
使用父容器的 align-items: center。 |
flex-start |
位于顶部边缘。 |
center |
在所在行内垂直居中。 |
flex-end |
位于底部边缘。 |
stretch |
高度为 auto 且外边距和尺寸限制允许时填满整行高度。 |
表格以示例的父容器 align-items: center 为准。auto 使用父容器的 align-items。如果要对齐文字基线,可以使用 baseline,其结果不同于对齐盒子末端的 flex-end。参见 CSS Box Alignment · align-self。
比较一行中的五种对齐方式
把代码保存为 flex-align-self.html 后打开,无需外部文件或 JavaScript。父容器边框盒高 240px,减去两边的边框和内边距共 26px,Flex 排列中这一行的高度为 214px。标签只占一行、项目高度为 44px 时,居中项目上沿离内侧顶端 85px。起点对齐为 0px,终点为 170px,拉伸项目填满 214px。
<!doctype html>
<html lang="zh" dir="ltr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>比较 Flexbox 的 align-self 取值</title>
<style>
body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
* { box-sizing: border-box; }
h1 { font-size: 24px; }
.demo { display: flex; align-items: center; gap: 8px;
max-width: 620px; height: 240px; padding: 12px;
border: 1px solid #7795a3; background: #edf5f7; }
.item { flex: 1 1 0; min-width: 0; min-height: 44px; padding: 10px 4px;
background: #176f80; color: white; font-size: 14px; text-align: center;
overflow-wrap: anywhere; border-radius: 4px; }
.start { align-self: flex-start; }
.center { align-self: center; }
.end { align-self: flex-end; }
.stretch { align-self: stretch; }
</style>
</head>
<body>
<h1>比较 align-self</h1>
<div class="demo">
<div class="item auto">auto</div>
<div class="item start">起点</div>
<div class="item center">居中</div>
<div class="item end">终点</div>
<div class="item stretch">拉伸</div>
</div>
<p>父容器设置了 align-items: center。第一项遵从此值,其余项目使用各自的 align-self。</p>
</body>
</html>
只把父容器的 align-items 改为 flex-start。第一个 auto 项目会上移,显式设为 center 的项目仍居中。比较之后将父容器的值恢复为 center。项目盒子的位置和内部文字的垂直对齐是两项设置。
排查 stretch 和 center 没有效果的原因
把下面的 height 规则加到 style 末尾。最后一项的交叉轴尺寸固定为 60px,即使指定了 stretch,也不会拉伸到 214px。max-height 等尺寸限制也会影响拉伸。试完后删除新增规则。
.stretch {
height: 60px;
}
接着给居中项添加下面的外边距规则。顶部的 auto 外边距会吸收交叉轴上的所有剩余空间,将项目推到下方。这时 align-self: center 不参与位置计算。移除外边距规则可恢复居中。参见 CSS Flexbox · Auto margins。
.center {
margin-top: auto;
}
在原始示例中把父容器的 height 改成 auto,高度就会跟随最高的项目。如果各项目一样高,起点、居中和终点可能看不出差别。排查时可在开发者工具中查看 Flex 行尺寸、项目高度和交叉轴外边距。之后把父容器的高度恢复为 240px。
确定纵向布局和多行布局的对齐基准
把下列规则加到 style 末尾以切换为 column,交叉轴就变为水平。auto 和 center 水平居中,起点和终点分别在左、右。最后一项的 width 重新设为 auto,所以能横向拉伸。物理方向还会随书写方向改变。参见 CSS Flexbox · flex-direction。
.demo {
flex-direction: column;
height: auto;
min-height: 340px;
}
.item {
flex: 0 0 auto;
width: 60px;
}
.stretch {
width: auto;
}
如果 flex-wrap 让容器排成多行,align-self 在项目所属的行内对齐。用 align-items 设置行内项目的默认对齐,用 align-content 对齐整组行。align-content 不影响设为 nowrap 的单行容器。参见 CSS Flexbox · align-content。




