flex 是 flexible Box 的缩写,意为弹性布局,
用来为盒状模型提供最大的灵活性,任何一个容器都可以指定flex布局。
- 当父盒子设置为flex布局以后,子元素的float、clear和vertical属性将失效
- 伸缩布局=弹性布局=伸缩盒布局=弹性盒布局=flex布局
flex布局原理:
通过给父盒子添加flex属性,来控制子盒子的位置和排列方式
flex布局实现一个div元素水平垂直居中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
}
body {
display: flex;
justify-content: center;
align-items: center;
}
div {
width: 200px;
height: 200px;
background: linear-gradient(skyblue 30%, deeppink 70%);
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
效果如图:
1.1 常见的父项属性
- flex-direction: 设置主轴的方向
- justify-content: 设置主轴上的子元素排列方式
- flex-wrap: 设置子元素是否换行
- align-content: 设置侧轴上的子元素的排列方式(多行)
- align-items:设置侧轴上的子元素排列方式(当行)
- flex-flow: 符合属性,相当于同时设置了flex-direction和flex-wrap
1.2 flex-direction 设置主轴的方向
1.主轴与侧轴
在flex布局中,是分为主轴和侧轴两个方向,同样的叫法有 行和列
● 默认主轴方向就是x轴的方向,水平向右
● 默认侧轴方向就是y轴方向, 水平向下
2. 属性值
flex-direction 属性决定主轴的方向
注意:主轴和侧轴是会变化的,就看flex-direction设置哪个为主轴,剩下的就是侧轴。
属性值 | 说明 |
---|---|
row | 默认值, 从左向右排列 |
row-reverse | 从右向左排列 |
column | 从上到下排列 |
column-reverse | 从下到上排列 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
display: flex;
width: 666px;
height: 333px;
background: linear-gradient(skyblue, deeppink);
margin: 50px auto;
/* 默认属性 */
flex-direction: row;
}
.box > span {
width: 200px;
height: 100px;
background: linear-gradient(orange, purple);
color: #fff;
}
</style>
</head>
<body>
<div class="box">
<span>one</span>
<span>two</span>
<span>three</span>
</div>
</body>
</html>
flex-direction: column;
1.3 justify-content 设置主轴子元素排列 ★
justify-content 属性定义了项目在主轴上的对齐方式
注意:使用这个属性之前一定要确认好主轴是哪个
属性值 | 说明 |
---|---|
flex-start | 默认值从头部开始 如果主轴是x轴,则从左到右 |
flex-end | 从尾部开始 |
center | 在主轴居中对齐(如果主轴是x轴则水平居中) |
space-around | 平分剩余空间 |
space-between | 先两边贴边 再平分剩余空间(重点) |
justify-content: flex-start
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
display: flex;
width: 666px;
height: 400px;
margin: 10px auto;
background: linear-gradient(45deg, #fff, orange);
/* justify-content: flex-start; */
/* justify-content: flex-end; */
/* justify-content: center; */
/* justify-content: space-around; */
justify-content: space-between;
}
.box > span {
width: 150px;
height: 100px;
background: linear-gradient(45deg, #fff, deeppink);
}
</style>
</head>
<body>
<div class="box">
<span>aaa</span>
<span>bbb</span>
<span>ccc</span>
</div>
</body>
</html>
justify-content: flex-end
justify-content: center
justify-content: space-between
justify-content: space-around
1.4 flex-wrap 子元素是否换行
默认情况下,项目都排在一条线(又称“轴线”)上。flex默认子元素不换行。
属性值 | 说明 |
---|---|
nowrap | 默认值,不换行 |
wrap | 换行 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
display: flex;
width: 660px;
height: 400px;
background: linear-gradient(skyblue, deeppink);
margin: 50px auto;
justify-content: flex-start;
/* 默认 flex-wrap: nowrap; 不换行 */
/* flex-wrap: nowrap; */
flex-wrap: wrap;
}
.box > span {
width: 150px;
height: 100px;
margin: 0 5px;
background: linear-gradient(#fff, purple)
}
</style>
</head>
<body>
<div class="box">
<span>aaa</span>
<span>bbb</span>
<span>ccc</span>
<span>ddd</span>
<span>eee</span>
<span>fff</span>
</div>
</body>
</html>
1.5 align-items 设置侧轴子元素排列
该属性是控制 子项在侧轴(默认是y轴)上的排列方式 在子项为单项的时候使用
flex-start | 从上到下 |
---|---|
flex-start | 从上到下 |
flex-end | 从下到上 |
center | 挤在一起居中(垂直居中) |
stretch | 拉伸(默认值)元素不设置高度 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
display: flex;
width: 600px;
height: 400px;
background: pink;
margin: 0 auto;
justify-content: space-between;
align-items: flex-start;
/* align-items: flex-end; */
/* align-items: center; */
/* align-items: stretch; */
}
span {
width: 150px;
height: 100px;
background: orange;
margin: 5px;
}
</style>
</head>
<body>
<div class="box">
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
align-items: flex-start
align-items: flex-end
align-items: center
align-items: stretch
注意:没有设置高度才可以生效
1.6 align-content 设置侧轴上的子元素的排列方式是(多行)
设置子项在侧轴上的排列方式并且只能用于子项出现换行的情况(多行),注意:在单行下是没有效果的
属性值 | 说明 |
---|---|
flex-start | 默认在侧轴的头部开始排列 |
flex-end | 在侧轴尾部开始排列 |
center | 在侧轴中间显示 |
space-around | 子项在侧轴平分剩余空间 |
space-between | 子项在侧轴先分布在两头,再平分剩余空间 |
stretch | 设置子项元素高度平分父元素高度 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
display: flex;
justify-content: flex-start;
width: 600px;
height: 450px;
background: orange;
margin: 20px auto;
flex-wrap: wrap;
align-content: flex-start;
/* align-content: flex-end; */
/* align-content: center; */
/* align-content: space-around; */
/* align-content: space-between; */
}
span {
width: 150px;
height: 100px;
background: skyblue;
margin: 5px;
}
</style>
</head>
<body>
<div class="box">
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</body>
</html>
align-content: flex-start
align-content: flex-end
align-content: center
align-content: space-around
align-content: space-between
align-content: stretch (没有高度才生效)
align-content 和 align-items 区别
● align-items 适用于单行情况下、只有上对齐、下对齐、居中和拉伸
● align-content 适用于换行(多行)情况下(单行情况下失效),可以设置上对齐、下对齐、居中、拉伸、以及平均分配剩余空间等属性值。
● 总结就是单行找align-items 多行找align-content
1.7 flex-flow
flex-flow属性是flex-direction 和 flex-wrap 属性的复合属性
flex-flow: row wrap;
2 flex布局子项常见的属性
2.1 flex属性
flex属性定义子项目分配剩余空间,用flex来表示占多少份数。
item {
flex: <number>; /*default 0*/
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
section {
display: flex;
width: 400px;
height: 50px;
background: skyblue;
margin: 20px auto;
}
section div:nth-child(1),
section div:nth-child(3) {
width: 40px;
height: 100%;
}
section div:nth-child(2) {
flex: 1;
}
section div:nth-child(1) {
background: purple;
}
section div:nth-child(3) {
background: orange;
}
</style>
</head>
<body>
<section>
<div></div>
<div></div>
<div></div>
</section>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
li {
list-style-type: none;
}
ul {
display: flex;
width: 400px;
height: 50px;
margin: 10px auto;
background: pink;
}
li {
display: flex;
flex: 1;
background: orange;
flex-direction: column;
}
li > span {
flex: 1;
background: purple;
text-align: center;
color: #fff;
}
.top {
border-bottom: 1px solid #fff;
}
.last>span {
background: linear-gradient(45deg, deeppink, skyblue)
}
</style>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>
<span class="top">span</span>
<span>span</span>
</li>
<li class="last">
<span class="top">span</span>
<span>span</span>
</li>
</ul>
</body>
</html>
flex属性总结:
① flex 的分配 与主轴的方向有关,当主轴为 x 轴 那么子元素 和 父元素等高 ,水平方向上按flex值均分,当主轴为y轴则相反。
2.2 align-self 控制子项自己在侧轴上的排列方式
align-self 属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性,默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
div {
display: flex;
width: 600px;
height: 450px;
background: pink;
justify-content: space-between;
}
div > span {
width: 150px;
height: 100px;
background: purple;
}
div span:nth-child(2) {
align-self: center;
}
</style>
</head>
<body>
<div>
<span></span>
<span></span>
<span></span>
</div>
</body>
</html>
2.3 order 属性定义项目的排列顺序
数值越小,排列越靠前,默认为0
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
display: flex;
width: 600px;
height: 400px;
background: skyblue;
}
.box > span {
width: 150px;
height: 100px;
background: orangered;
order: 1;
}
.box span:nth-child(2) {
order: 0;
}
</style>
</head>
<body>
<div class="box">
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>