flex弹性布局(display: flex;)
添加在父容器上
1. display: flex;
2. flex-direction 布局的排列方向
- flex-direction: row;默认值,显示为行。方向为当前文档水平流方向,默认情况下是从左往右。
<style>
#main ul{
margin: 100px;
display: flex;
list-style: none;
flex-direction: row;
}
#main ul li:nth-of-type(1){ width: 100px; height: 100px; background: blue;}
#main ul li:nth-of-type(2){ width: 100px; height: 100px; background: red;}
#main ul li:nth-of-type(3){ width: 100px; height: 100px; background: yellow;}
#main ul li:nth-of-type(4){ width: 100px; height: 100px; background: pink;}
#main ul li:nth-of-type(5){ width: 100px; height: 100px; background: green;}
#main ul li:nth-of-type(6){ width: 100px; height: 100px; background: gray;}
#main ul li:nth-of-type(7){ width: 100px; height: 100px; background: orange;}
#main ul li:nth-of-type(8){ width: 100px; height: 100px; background: orangered;}
</style>
- flex-direction: row-reverse;显示为行。但方向和row属性值是反的
#main ul{
display: flex;
list-style: none;
flex-direction: row-reverse;
}
- flex-direction: column;显示为列
#main ul{
display: flex;
list-style: none;
flex-direction: column;
}
- flex-direction: column-reverse; 显示为列。但方向和column属性值是反的
#main ul{
display: flex;
list-style: none;
flex-direction: column-reverse;
}
3.flex-wrap 是否进行换行处理.
- flex-wrap: nowrap;默认值,不换行处理
#main ul{
width: 500px;
display: flex;
list-style: none;
flex-wrap: nowrap;
}
- flex-wrap: wrap;换行处理
#main ul{
width: 500px;
display: flex;
list-style: none;
flex-wrap: wrap;
}
- wrap-reverse; 反向换行
#main ul{
width: 500px;
display: flex;
list-style: none;
flex-wrap: wrap-reverse;
}
4.justify-content 主轴方向上子项的对齐和分布方式
- justify-content: flex-start; 子项都去起始位置对齐
#main ul{
width: 1000px;
height: 200px;
margin: 100px;
background: cyan;
display: flex;
list-style: none;
justify-content: flex-start;
}
- justify-content: flex-end; 子项都去结束位置对齐。
#main ul{
width: 1000px;
height: 200px;
margin: 100px;
background: cyan;
display: flex;
list-style: none;
justify-content: flex-end;
}
- justify-content: center; 子项都去中心位置对齐
#main ul{
width: 1000px;
height: 200px;
margin: 100px;
background: cyan;
display: flex;
list-style: none;
justify-content: center;
}
- justify-content: space-between; 两端对齐。
#main ul{
width: 1000px;
height: 200px;
margin: 100px;
background: cyan;
display: flex;
list-style: none;
justify-content: space-between;
}
- justify-content: space-around;around是环绕的意思,意思是每个flex子项两侧都环绕互不干扰的等宽的空白间距,最终视觉上边缘两侧的空白只有中间空白宽度一半。
#main ul{
width: 1000px;
height: 200px;
margin: 100px;
background: cyan;
display: flex;
list-style: none;
justify-content: space-around;
}
- justify-content: space-evenly; evenly是匀称、平等的意思。也就是视觉上,每个flex子项两侧空白间距完全相等。
#main ul{
width: 1000px;
height: 200px;
margin: 100px;
background: cyan;
display: flex;
list-style: none;
justify-content: space-evenly;
}
5.align-items 每一行中的子元素上下对齐方式
- align-items: flex-start; 起始位置对齐
#main ul{
width: 1000px;
height: 200px;
margin: 100px;
background: cyan;
display: flex;
list-style: none;
align-items: flex-start;
}
- align-items: center; 中心对齐
#main ul{
width: 1000px;
height: 200px;
margin: 100px;
background: cyan;
display: flex;
list-style: none;
align-items: center;
}
- align-items: flex-end; 结束位置对齐
#main ul{
width: 1000px;
height: 200px;
margin: 100px;
background: cyan;
display: flex;
list-style: none;
align-items: flex-end;
}
5.align-content 属性定义了在交叉轴方向的对齐方式及额外空间分配(侧轴的对齐方式)。一行看不出效果
- align-content: flex-start; 从起始线开始排列
#main ul{
width: 500px;
height: 400px;
margin: 100px;
background: cyan;
display: flex;
list-style: none;
flex-wrap: wrap;
align-items: flex-end;
align-content: flex-start;
}
- align-content: flex-end; 从终点线开始排列
#main ul{
width: 500px;
height: 400px;
margin: 100px;
background: cyan;
display: flex;
list-style: none;
flex-wrap: wrap;
align-items: flex-end;
align-content: flex-end;
}
- align-content:center; 居中排列
#main ul{
width: 500px;
height: 400px;
margin: 100px;
background: cyan;
display: flex;
list-style: none;
flex-wrap: wrap;
align-items: flex-end;
align-content: center;
}
- align-content: space-around; 边缘两侧的空白只有中间空白宽度一半(相邻项目之间的距离是两个项目之间留白的和)
#main ul{
width: 500px;
height: 400px;
margin: 100px;
background: cyan;
display: flex;
list-style: none;
flex-wrap: wrap;
align-items: flex-end;
align-content: space-around;
}
- align-content:space-between; 项目均匀分布,第一项在启点线,最后一项在终点线
#main ul{
width: 500px;
height: 400px;
margin: 100px;
background: cyan;
display: flex;
list-style: none;
flex-wrap: wrap;
align-items: flex-end;
align-content: space-between;
}
- align-content: stretch; 默认值
#main ul{
width: 500px;
height: 400px;
margin: 100px;
background: cyan;
display: flex;
list-style: none;
flex-wrap: wrap;
align-items: flex-end;
align-content: stretch;
}
添加在子容器上
1.order 排序: 值从小到大顺序排列,可以为负值,缺省为0。
<style>
*{padding: 0; margin: 0;}
#main ul{
/* width: 500px; */
height: 400px;
margin: 100px;
background: cyan;
display: flex;
list-style: none;
flex-wrap: wrap;
align-content: flex-start;
}
#main ul li:nth-of-type(1){ width: 100px; height: 100px; background: blue; order: 8;}
#main ul li:nth-of-type(2){ width: 100px; height: 100px; background: red; order: 7;}
#main ul li:nth-of-type(3){ width: 100px; height: 100px; background: yellow; order: 6;}
#main ul li:nth-of-type(4){ width: 100px; height: 100px; background: pink; order: 5;}
#main ul li:nth-of-type(5){ width: 100px; height: 100px; background: green; order: 4;}
#main ul li:nth-of-type(6){ width: 100px; height: 100px; background: gray; order: 3;}
#main ul li:nth-of-type(7){ width: 100px; height: 100px; background: orange; order: 2;}
#main ul li:nth-of-type(8){ width: 100px; height: 100px; background: orangered; order: 1;}
</style>
<body>
<div id="main">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
</ul>
</div>
</body>
2.flex-grow 扩展
- flex-grow: 0;默认值,不扩展
- flex-grow: 1; 扩展,会把空白区全部占满
#main ul li{
flex-grow: 0;
}
#main ul li{
flex-grow: 1;
}
2.flex-shrink 收缩
正常默认值是1
0表示不收缩,.5收缩小一些,2收缩大一些。(大小是跟正常缩放1进行比较的)
#main ul li{
flex-shrink: 1;
}
#main ul li{
flex-shrink: .5;
}
3.flex-basis (跟flex-shrink/flex-grow很像)。
flex-shrink/flex-grow是设置一个比例值,flex-basis是设置一个具体值。
#main ul li{
flex-basis: auto;
}
#main ul li{
flex-basis: 60px;
}
#main ul li{
flex-basis: 160px;
}
4.align-shef 控制单独某一个子项的对齐方式(和align-items类似)
#main ul{
height: 400px;
margin: 100px;
background: cyan;
display: flex;
list-style: none;
}
#main ul li:nth-of-type(1){
width: 100px;
height: 100px;
background: blue;
align-self: center;
}