主题
  • 默认模式
  • 浅蓝色模式
  • 淡绿色模式
  • 深夜模式

定义和用法

align-items是 CSS 中用于控制 flex 容器内项目在交叉轴(垂直于主轴)上对齐方式的属性。基本概念:

  • 主轴(main axis):由 flex-direction 决定的方向(默认为水平方向)
  • 交叉轴(cross axis):与主轴垂直的方向(默认为垂直方向)
  • 对齐方式:决定 flex 项目如何在交叉轴上排列

常用的实例

接下来用多种实例演示align-items属性的用法,来深化您对align-items属性的理解。

1. 默认拉伸 (stretch)

实例代码 运行代码
复制
.container {
    display: flex;
    align-items: stretch; /* 默认值,可省略 */
}

2. 顶部对齐 (flex-start)

实例代码 运行代码
复制
.container {
    display: flex;
    align-items: flex-start;
}

3. 底部对齐 (flex-end)

实例代码 运行代码
复制
.container {
    display: flex;
    align-items: flex-end;
}

4. 居中对齐 (center)

实例代码 运行代码
复制
.container {
    display: flex;
    align-items: center;
}

5. 基线对齐 (baseline)

实例代码 运行代码
复制
.container {
    display: flex;
    align-items: baseline;
}

CSS 语法

align-items: stretch|center|flex-start|flex-end|baseline|initial|inherit;

属性值

描述
stretch 项目被拉伸以适合容器(默认值)
center 项目位于容器的中央。
flex-start 项目位于容器的开头。
flex-end 项目位于容器的末端。
baseline 项目被定位到容器的基线。
initial 将此属性设置为其默认值。参阅 initial
inherit 从其父元素继承此属性。参阅 inherit

浏览器支持

表格中的数字注明了完全支持align-items属性的首个浏览器版本。

Chrome 浏览器 EDGE 浏览器 Firefox 浏览器 Safari 浏览器 Opera 浏览器
57.0 16.0 52.0 10.1 44.0


评论区 0
发表评论
教程介绍
CSS 参考手册全面涵盖选择器、盒模型,文本属性等,助您快速检索 CSS 使用方法。
95 章节
139 阅读
0 评论
教程目录

CSS 参考手册

CSS 属性(字母排序) CSS 属性(功能排序) CSS 浏览器支持 CSS 选择器 CSS 函数 CSS 可动画性 CSS Web安全字体 CSS 单位 CSS 颜色 CSS 颜色值

CSS 属性

align-content align-items align-self all animation animation-delay animation-direction animation-duration animation-fill-mode animation-iteration-count animation-name animation-play-state animation-timing-function aspect-ratio backdrop-filter backface-visibility background background-attachment background-blend-mode background-clip background-color background-image background-origin background-position background-position-x background-position-y background-repeat background-size block-size border border-block border-block-color border-block-end border-block-end-color border-block-end-style border-block-end-width border-block-start border-block-start-color border-block-start-style border-block-start-width border-block-style border-block-width border-bottom border-bottom-color border-bottom-left-radius border-bottom-right-radius border-bottom-style border-bottom-width border-collapse border-color border-end-end-radius border-end-start-radius border-image border-image-outset border-image-repeat border-image-slice border-image-source border-image-width border-inline border-inline-color border-inline-end border-inline-end-color border-inline-end-style border-inline-end-width border-inline-start border-inline-start-color border-inline-start-style border-inline-start-width border-inline-style border-inline-width border-left border-left-color border-left-style border-left-width border-radius border-right border-right-color border-right-style border-right-width border-spacing border-start-end-radius border-start-start-radius border-top-left-radius border-top-right-radius border-top-style