交叉軸是垂直方向還是水平方向病瞳,需要根據(jù)主軸方向(
flex-direction: column
)谊迄,如果主軸是水平方向那么交叉軸為垂直方向令宿,如果主軸方向垂直方向(flex-direction: column
)寇荧,交叉軸就是水平方向刽脖。
1. align-items: flex-start
主軸為垂直排列那么交叉軸就是水平方向
flex-direction: column;
align-items: flex-start;
Jietu20191229-204126.jpg
Jietu20191229-204126.jpg
2. align-items:flex-end
主軸橫向排列交叉軸為縱向排列羞海,那么flex-end 就是交叉軸下方是排列結束位置
article {
display: flex;
border: solid 5px silver;
border-color: #8A2BE2;
width: 450px;
height: 280px;
background: red;
flex-direction: column;
align-items: flex-end;
}
主軸橫向.jpg
主軸橫向.jpg
3. align-items: center
主軸是水平橫向排列忌愚,那么交叉軸為垂直方向縱向排列 center 就是交叉軸中心位置對齊
article {
display: flex;
border: solid 5px silver;
border-color: #8A2BE2;
width: 450px;
height: 280px;
background: red;
flex-direction: row;
align-items: center;
}
Jietu20191229-194450.jpg
Jietu20191229-194450.jpg
4. align-items: stretch
主軸是水平橫向排列曲管,那么交叉軸為垂直方向縱向排列 stretch 會沿著交叉軸垂直方向拉伸填充完垂直方向容器
注意
必須是容器內(nèi)的子元素沒有設置垂直方向高度,設置尺寸則該元素屬性無法生效
article {
display: flex;
border: solid 5px silver;
border-color: #8A2BE2;
width: 450px;
height: 280px;
background: red;
flex-direction: row;
align-items: stretch;
}
article * {
width: 100px;
/* height: 100px; */
}
Jietu20191229-195349.jpg
測試代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" content="width=device-width, initial-scale=1.0">
<title></title>
<style type="text/css">
* {
padding: 0px;
margin: 0px;
}
body {
padding-left: 0px;
padding-top: 15px;
}
article {
display: flex;
border: solid 5px silver;
border-color: #8A2BE2;
width: 450px;
height: 280px;
background: red;
flex-direction: row;
align-items: flex-end;
}
article * {
width: 100px;
height: 100px;
}
.first {
background: #87CEEB;
}
.second {
background: #FFA07A;
}
.third {
background: lightpink;
}
.fourth {
background: lightgreen;
}
</style>
</head>
<body>
<article>
<div class="first">第一</div>
<div class="second">第二</div>
<div class="third">第三</div>
<div class="fourth">第四</div>
</article>
</body>
</html>