css教程网 css教程网365css.cn
公告为您打开互联网世界的窗户关于本站
首页/属性速查/布局与定位

align-items

交叉轴上的对齐方式,Flex 里最常用的「垂直居中」。

语法
align-items: stretch | flex-start | flex-end | center | baseline
常用值
stretch / flex-start / flex-end / center / baseline
初始值
normal(表现为 stretch)
继承
否

运行效果

示例改成别的值试试,差异一眼能看出来
查看代码
HTML
<div class="ai"><i class="tall">高</i><i>中</i><i class="short">低</i></div>
CSS
.ai { display: flex; align-items: center; justify-content: center; gap: 8px; height: 110px; width: 100%; background: #f6f8fc; border-radius: 10px; }
.ai i { width: 44px; background: #2b57ff; color: #fff; border-radius: 7px; display: grid; place-items: center; font: 600 10px/1 ui-monospace, monospace; font-style: normal; height: 34px; }
.ai .tall { height: 62px; background: #0d1117; }
.ai .short { height: 24px; background: #7a4bff; }

使用要点

  • 默认 stretch 会把子项拉满交叉轴高度,这就是等高卡片的原理。
  • 做居中时 align-items 管交叉轴,justify-content 管主轴,方向由 flex-direction 决定。