五,align-items - cross轴-垂直布局
- 设置弹性盒子元素在侧轴(纵轴)方向上的对齐方式
- stretch:默认值,如果项目未设置高度过auto,子元素会被拉伸以适应容器
- flex-start:元素位于容器开头
- flex-end:元素位于容器结尾
- center:元素位于容器中心
- baseline:元素位于容器的基线上
- 使用这个之前要先把flex-direction:column调成纵向
5.1,align-items:center;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box{ width: 100%; height: 600px; border: 1px solid black; display: flex; flex-direction:column; align-items:center; } .box>div{ border: 1px solid black; } </style> </head> <body> <div class="box"> <div>第一个子元素</div> <div>第二个子元素</div> <div>第三个子元素</div> </div> </body> </html>
垂直方向居中
5.2,align-items:flex-end;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box{ width: 100%; height: 600px; border: 1px solid black; display: flex; flex-direction:column; align-items:flex-end; } .box>div{ border: 1px solid black; } </style> </head> <body> <div class="box"> <div>第一个子元素</div> <div>第二个子元素</div> <div>第三个子元素</div> </div> </body> </html>
cross轴底部对齐
5.3,align-items:flex-end;
使用这个之前要先把flex-direction:row调成默认
然后再div里加上高度
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box{ width: 100%; height: 600px; border: 1px solid black; display: flex; flex-direction:row; align-items:flex-end; } .box>div{ border: 1px solid black; } </style> </head> <body> <div class="box"> <div style="height: 100px;">第一个子元素</div> <div style="height: 200px;">第二个子元素</div> <div style="height: 300px;">第三个子元素</div> </div> </body> </html>
默认值,如果项目未设置高度过auto,子元素会被拉伸以适应容器
5.4,align-items:flex-start;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box{ width: 100%; height: 600px; border: 1px solid black; display: flex; flex-direction:row; align-items:flex-start; } .box>div{ border: 1px solid black; } </style> </head> <body> <div class="box"> <div style="height: 100px;">第一个子元素</div> <div style="height: 200px;">第二个子元素</div> <div style="height: 300px;">第三个子元素</div> </div> </body> </html>
元素位于容器开头
5.5,align-items:center;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box{ width: 100%; height: 600px; border: 1px solid black; display: flex; flex-direction:row; align-items:center; } .box>div{ border: 1px solid black; } </style> </head> <body> <div class="box"> <div style="height: 100px;">第一个子元素</div> <div style="height: 200px;">第二个子元素</div> <div style="height: 300px;">第三个子元素</div> </div> </body> </html>
元素位于容器中心
5.6,align-items:baseline;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box{ width: 100%; height: 600px; border: 1px solid black; display: flex; flex-direction:row; align-items:baseline; } .box>div{ border: 1px solid black; } </style> </head> <body> <div class="box"> <div style="height: 100px;">第一个子元素</div> <div style="height: 200px;">第二个子元素</div> <div style="height: 300px;">第三个子元素</div> </div> </body> </html>
元素位于容器的基线上
六,flex-wrap - 换行、换列
6.1,align-items:stretch;;
注意
在div加上宽度
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box{ width: 100%; height: 600px; border: 1px solid black; display: flex; flex-direction:row; align-items:stretch; } .box>div{ border: 1px solid black; } </style> </head> <body> <div class="box"> <div style="height: 100px; width: 40%;">第一个子元素</div> <div style="height: 200px; width: 40%;">第二个子元素</div> <div style="height: 300px; width: 40%;">第三个子元素</div> </div> </body> </html>
6.2,flex-wrap:wrap;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box{ width: 100%; height: 600px; border: 1px solid black; display: flex; flex-direction:row; align-items:stretch; flex-wrap: nowrap; } .box>div{ border: 1px solid black; } </style> </head> <body> <div class="box"> <div style="height: 100px; width: 40%;">第一个子元素</div> <div style="height: 200px; width: 40%;">第二个子元素</div> <div style="height: 300px; width: 40%;">第三个子元素</div> </div> </body> </html>
默认值。意思是不换行、换列。
当flex项目的总宽度超过容器的宽度,flex项目将按照原有比例强行等分,且不换行。
6.3,flex-wrap:wrap;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .box{ width: 100%; height: 600px; border: 1px solid black; display: flex; flex-direction:row; align-items:stretch; flex-wrap: wrap; } .box>div{ border: 1px solid black; } </style> </head> <body> <div class="box"> <div style="height: 100px; width: 40%;">第一个子元素</div> <div style="height: 200px; width: 40%;">第二个子元素</div> <div style="height: 300px; width: 40%;">第三个子元素</div> </div> </body> </html>
换行、换列。当flex项目的总宽度超过容器的宽度,将自然换行、换列。且每一行(列)等分容器。