这是一篇阮一峰Flex博客的阅读笔记 原文在此
单项目 9个位置都可以通过justify-content和align-items设置
/* 首行左对齐 */ .box{ display: flex; } /* 改变项目对齐方式 */ .box{ display: flex; justify-content: center | flex-start | flex-end; } /* 交叉轴,垂直方向 */ .box{ display: flex; align-items: center; } /* 垂直水平居中 */ .box{ display: flex; justify-content: center; align-items: center; }双项目
/* 两端对齐 */ .box{ display: flex; justify-content: space-between; } /* 垂直两端对齐,借助改变主轴方向 */ .box{ display: flex; flex-direction: column; justify-content: space-between; } /* 允许单个项目独立于其他项目的不一样的对齐方式 */ .box{ display: flex; /* 这里默认flex-start */ } .item:nth-child(2) { align-self: center; }三项目
/* 利用align-slef,各自独立的对齐方式 */ .box{ display: flex; } .item:nth-child(2){ align-self: center; } .item:nth-child(3){ align-self: flex-end; }四项目
/* 默认自动压缩,wrap换行 */ .box{ display: flex; flex-wrap: wrap; justify-content: flex-end; align-content: space-between; } <div class="box"> <div class="column"> <span class="item"></span> <span class="item"></span> </div> <div class="column"> <span class="item"></span> <span class="item"></span> </div> </div> <style> .box { display: flex; flex-wrap: wrap; align-content: space-between; } .column{ /* basis表示单个项目占据主轴多少;auto表示本来大小;100%表示充满主轴 */ flex-basis: 100%; display: flex; justify-content: space-between; } </style>等比布局(平均布局)
<div class="grid"> <div class="cell"></div> </div> <style> .grid{ display: flex; } .cell{ /* 等比例填充 */ flex: 1; } </style>百分比布局
<div class="grid"> <div class="grid-cell u-1of4"></div> <div class="grid-cell"></div> <div class="grid-cell u-1of2"></div> </div> <style> .grid { display: flex; } .grid-cell { border: 1px red solid; height: 50px; flex: 1; } .grid-cell.u-1of4{ flex: 0 0 25%; } .grid-cell.u-1of3{ flex: 0 0 33.33%; } .grid-cell.u-1of2{ flex: 0 0 50%; } .grid-cell.u-full{ flex: 0 0 100%; } </style>圣杯布局(holy grail layout)
<body class="HolyGrail"> <style> *{ border: 1px solid red; } .HolyGrail{ display: flex; min-height: 100vh; flex-direction: column; } .HolyGrail-body{ flex: 1; display: flex; } .HolyGrail-content{ flex: 1; } .HolyGrail-nav, .HolyGrail-ads{ flex: 0 0 20%; } .HolyGrail-nav{ /* 可以调整项目顺序 */ order: -1; } @media(max-width: 768px){ .HolyGrail-body{ flex-direction: column; flex: 1; } .HolyGrail-content, .HolyGrail-ads, .HolyGrail-nav{ flex: auto; } } </style> <header>Header</header> <div class="HolyGrail-body"> <main class="HolyGrail-content"></main> <nav class="HolyGrail-nav"></nav> <aside class="HolyGrail-ads"></aside> </div> <footer>Footer</footer> </body>在输入框前后需要缀上提示信息和图标按钮等
<div class="InputAddOn"> <span class="InputAddOn-item">请输入姓名</span> <input class="InputAddOn-field" /> <button class="InputAddOn-item">确认提交</button> </div> <style> .InputAddOn{ display: flex; } .InputAddOn-item{ /* 不会因为空间不足而缩小 */ flex: 0 0 auto; } .InputAddOn-field{ flex: 1; } </style>页面内容撑不满屏幕,底栏会抬高
<div class="Site"> <header>Header</header> <main class="main"></main> <footer>Footer</footer> </div> <style> *{ border: 1px solid red; } .Site{ display: flex; flex-direction: column; /* 充满屏幕 */ min-height: 100vh; } .main{ /* 填充剩余空间 */ flex: 1; } </style>每行项目数目固定,自动分行
<body> <div class="container"> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> <div class="item"></div> </div> <style> *{ border: 1px solid red; } .container{ display: flex; flex-wrap: wrap; align-content: flex-start; min-height: 100vh; width: 100px; } .item{ width: 23px; height: 23px; } </style> </body>