在线文档教程
CSS

对齐项 | align-items

align-items

CSSalign-items属性定义浏览器如何沿其容器的交叉轴在Flex项之间和周围分配空间。这意味着它的作用就像justify-content,而不是在垂直方向。

/* Basic keywords */ align-items: normal; align-items: stretch; /* Positional alignment */ align-items: center; /* Pack items around the center */ align-items: start; /* Pack items from the start */ align-items: end; /* Pack items from the end */ align-items: flex-start; /* Pack flex items from the start */ align-items: flex-end; /* Pack flex items from the end */ align-items: self-start; align-items: self-end; align-items: left; /* Pack items from the left */ align-items: right; /* Pack items from the right */ /* Baseline alignment */ align-items: baseline; align-items: first baseline; align-items: last baseline; /* Overflow alignment (for positional alignment only) */ align-items: safe center; align-items: unsafe center; /* Global values */ align-items: inherit; align-items: initial; align-items: unset;

align-content属性的区别,在于它指定了当前 flex 容器的行中的项的对齐方式, 而对齐内容指定行本身的对齐方式。

初始值stretch
适用于柔性容器
遗传没有
媒体视觉
计算值作为指定
动画类型离散的
规范的顺序形式语法定义的独特的非模糊顺序

语法

normal此关键字的效果取决于我们所处的布局模式:

  • 关键字的行为类似于已替换的绝对定位框的start, 以及其他所有绝对定位框上的stretch

  • 在绝对定位布局的静态位置中,关键字表现为stretch.

  • 对于Flex项,关键字表现为stretch.

  • 对于网格项,此关键字将导致类似于stretch,但具有高宽比或内部大小的盒子除外,其将被处理为start.

  • 该属性不适用于块级框和表格单元格。

flex-startFlex项目的交叉开始边缘边缘与线条的交叉起始边缘一起被刷新。

last baseline所有弹性项目都对齐,以使其基线对齐。交叉开始边缘边缘与其基线之间距离最大的项目与线的交叉起始边缘齐平。

形式语法

flex-start | flex-end | center | baseline | stretch

CSS

#container {   height:200px;   width: 240px;   align-items: center; /* Can be changed in the live sample */   background-color: #8c8c8c; } .flex {   display: flex;   flex-wrap: wrap; } .grid {   display: grid;   grid-template-columns: repeat(auto-fill, 50px } div > div {   box-sizing: border-box;   border: 2px solid #8c8c8c;   width: 50px;   display: flex;   align-items: center;   justify-content: center; } #item1 {   background-color: #8cffa0;   min-height: 30px; } #item2 {   background-color: #a0c8ff;   min-height: 50px; } #item3 {   background-color: #ffa08c;   min-height: 40px; } #item4 {   background-color: #ffff8c;   min-height: 60px; } #item5 {   background-color: #ff8cff;   min-height: 70px; } #item6 {   background-color: #8cffff;   min-height: 50px;   font-size: 30px; } select {   font-size: 16px; } .row {   margin-top: 10px; }

HTML

<div id="container" class="flex">   <div id="item1">1</div>   <div id="item2">2</div>   <div id="item3">3</div>   <div id="item4">4</div>   <div id="item5">5</div>   <div id="item6">6</div> </div> <div class="row">   <label for="display">display: </label>   <select id="display">     <option value="flex">flex</option>     <option value="grid">grid</option>   </select> </div> <div class="row">   <label for="values">align-items: </label>   <select id="values">     <option value="normal">normal</option>     <option value="flex-start">flex-start</option>     <option value="flex-end">flex-end</option>     <option value="center" selected>center</option>     <option value="baseline">baseline</option>     <option value="stretch">stretch</option>     <option value="start">start</option>     <option value="end">end</option>     <option value="self-start">self-start</option>     <option value="self-end">self-end</option>     <option value="left">left</option>     <option value="right">right</option>     <option value="first baseline">first baseline</option>     <option value="last baseline">last baseline</option>     <option value="safe center">safe center</option>     <option value="unsafe center">unsafe center</option>     <option value="safe right">safe right</option>     <option value="unsafe right">unsafe right</option>     <option value="safe end">safe end</option>     <option value="unsafe end">unsafe end</option>     <option value="safe self-end">safe self-end</option>     <option value="unsafe self-end">unsafe self-end</option>     <option value="safe flex-end">safe flex-end</option>     <option value="unsafe flex-end">unsafe flex-end</option>   </select> </div>

结果

规范

SpecificationStatusComment
CSS Box Alignment ModuleThe definition of 'align-items' in that specification.Working DraftAdds the first | last ? baseline, self-start, self-end, start, end, left, right, unsafe | safe values.
CSS Flexible Box Layout ModuleThe definition of 'align-items' in that specification.Candidate RecommendationInitial definition

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support21.0-webkit1(Yes)-webkit (Yes)20.0 (20.0)211.0312.107.0-webkit
space-evenly, first/last baseline, start, end, left, right??45 (45)???

FeatureAndroidAndroid WebviewEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari MobileChrome for Android
Basic support4.4(Yes)(Yes)-webkit (Yes)20.0 (20.0)2No support12.107.0-webkit?
space-evenly, first/last baseline, start, end, left, right???45.0 (45)????