flex布局原理
flex即flexible Box,意为弹性布局。用来为盒装模型提供最大的灵活性。
- 为父元素设置flex布局(display:flex)后,子元素的float,clear,vertical-align属性失效
- 采用flex布局的元素称为flex容器,它所有子元素称为flex项目。
- flex布局分为主轴和侧轴,类似于行和列或x轴和y轴。主轴,水平向右;侧轴,水平向下。
一.flex布局父项常见属性
1.flex-direction 设置主轴方向
属性 | 说明 |
---|---|
row | 从左到右(默认值) |
row-reverse | 从右到左 |
column | 从上到下 |
column-reverse | 从下到上 |
示例代码:
<style>
div {
display: flex; /* 为父盒子设置flex布局 */
width: 800px;
height: 300px;
background-color: pink;
/* 设置主轴方向 */
/* flex-direction: row; 默认值 x轴为主轴*/
flex-direction: column; /*设置y轴(从上到下)为主轴*/
}
div span {
width: 150px;
height: 100px;
background-color: blue;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
运行结果:
2.justify-content 设置主轴上的子元素的排列方式
使用此属性前一定要先确认主轴
属性 | 说明 |
---|---|
flex-start | 从头部开始(默认值) |
flex-end | 从尾部开始 |
center | 在主轴居中对齐 |
space-around | 平分剩余空间 |
space-between | 先两边贴边,再平分剩余空间 |
space-around属性 示例代码:
<style>
div {
display: flex;
width: 800px;
height: 300px;
background-color: pink;
justify-content: space-around;
}
div span {
width: 150px;
height: 100px;
background-color: blue;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
运行结果:
space-between属性 示例代码:
<style>
div {
display: flex;
width: 800px;
height: 300px;
background-color: pink;
justify-content: space-between;
}
div span {
width: 150px;
height: 100px;
background-color: blue;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
运行结果:
3.flex-wrap 设置子元素是否换行
在默认情况下,项目都排在一条线上,如果放不下,则会缩小子元素的宽度。
属性 | 说明 |
---|---|
nowrap | 不换行(默认值) |
wrap | 自动换行 |
4.align-items 设置侧轴上的子元素的排列方式(单行)
一般和justify-content搭配使用,实现子元素水平垂直居中
注:只能在子元素单行显示时使用
属性 | 说明 |
---|---|
flex-start | 从上到下(默认值) |
flex-end | 从下到上 |
center | 垂直居中 |
stretch | 拉伸 |
代码示例:
<style>
div {
display: flex;
width: 800px;
height: 300px;
background-color: pink;
/* 默认主轴方向从左到右 */
justify-content: center;/*在主轴中间居中对齐*/
align-items: center;/*在侧轴中间居中对齐*/
}
div span {
width: 150px;
height: 100px;
background-color: blue;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
运行结果:
5.align-content 设置侧轴上的子元素的排列方式(多行)
在子元素多行显示时使用,即出现换行的情况使用,否则无效
属性 | 说明 |
---|---|
flex-start | 从侧轴头部开始(默认值) |
flex-end | 从侧轴尾部开始 |
center | 在侧轴中间显示 |
space-around | 子项平分剩余空间 |
space-between | 先两边贴边,再平分剩余空间 |
stretch | 设置子项元素高度平分父元素高度 |
center属性 示例代码:
<style>
div {
display: flex;
width: 800px;
height: 300px;
background-color: pink;
/* 默认主轴方向从左到右,那么侧轴为从上至下 */
flex-wrap: wrap;/*换行*/
align-content: center;
}
div span {
width: 150px;
height: 100px;
background-color: blue;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
</div>
</body>
运行结果:
ps:单行找align-items,多行找align-content
6.flex-flow 是flex-direction和flex-wrap的复合属性
示例:flex-flow:row wrap;
二.flex布局子项常见属性
1.flex属性 定义子项目分配剩余空间
用flex表示占多少份数
两边固定宽度,中间自适应的三栏布局(圣杯布局)
语法:flex:<number>(默认值为0)
示例代码:
<style>
div {
display: flex;
width: 800px;
height: 300px;
background-color: pink;
}
div span {
width: 150px;
height: 100px;
background-color: blue;
}
div span:nth-child(2) {
flex:2; /*表示第二个孩子占两份*/
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
运行结果:
2.align-self 控制子项自己在侧轴上的排列顺序
允许单个项目与其他项目不一样的对齐方式
示例代码:
<style>
div {
display: flex;
width: 800px;
height: 300px;
background-color: pink;
}
div span {
width: 150px;
height: 100px;
background-color: blue;
}
div span:nth-child(2) {
align-self: center;/*第二个孩子在侧轴上居中对齐*/
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
运行结果:
3.order属性 定义项目的排列顺序
数值越小,排列越靠前(默认值为0)
实例代码:
<style>
div {
display: flex;
width: 800px;
height: 300px;
background-color: pink;
}
div span {
width: 150px;
height: 100px;
background-color: blue;
}
div span:nth-child(2) {
align-self: center;
order: -1;/*将第二个孩子排在第一个孩子前面*/
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>