flex布局
flex布局体验
- 移动端应用很广泛
- 网页端不用考虑兼容问题也可以使用flex布局
<!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>flex布局体验</title>
<style>
div {
display: flex;
width: 300px;
height: 300px;
background-color: pink;
justify-content: space-around;
}
div span {
width: 50px;
height: 50px;
background-color: green;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
flex布局原理(给父盒子添加属性 控制子盒子)
- 弹性布局,任何一个容器都可以指定为flex布局
- 当设置flex时,子元素的的float clear vertical-align属性失效
- 伸缩布局=弹性布局=伸缩盒布局=弹性布局=flex布局
- 可以横向排列也可以竖向排列
flex布局父项常见属性
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0a13ec6d0e15ab4e06a9cb968e62ec38.png)
flex-direction: column;
flex-direction: row;
justify-content: space-between;
flex-wrap: wrap;
align-items: flex-start;
- align-content 侧轴对齐方式
只适合多行
align-content: center ;
flex-flow: row wrap;
flex布局子项常见属性
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b35c978b5f0711aaca9d8f335b495de1.png)
- flex属性定义项目分配剩余空间 用flex来表示占多少份数
可以写百分比
<!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>flex布局子项的属性</title>
<style>
p {
display: flex;
width: 60%;
height: 150px;
background-color:pink;
}
p span {
flex: 1;
}
p span:nth-child(2) {
flex: 2;
background-color: purple;
}
</style>
</head>
<body>
<p>
<span>1</span>
<span>2</span>
<span>3</span>
</p>
</body>
</html>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bd3edc355a978ef09bcd93c91defa996.png)
- align-self 控制子项自己在侧轴的排列方式
<!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>flex布局子项的属性</title>
<style>
section {
display: flex;
width: 60%;
height: 150px;
background-color:pink;
}
section div {
width: 100px;
height: 100px;
margin-right: 10px;
background-color: purple;
}
section div:last-child {
align-self: flex-end;
}
</style>
</head>
<body>
<section>
<div></div>
<div></div>
<div></div>
</section>
</body>
</html>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bd629b30b3919aaa1ce9d611b1c156d3.png)
- order属性定义项目的排列顺序
- 数值越小,排列越靠前,默认为0,可以为负数
- 注意和z-index不一样
<!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>flex布局子项的属性</title>
<style>
section {
display: flex;
width: 60%;
height: 150px;
background-color:pink;
}
section div {
width: 100px;
height: 100px;
margin-right: 10px;
background-color: purple;
}
section div:last-child {
order: -1;
align-self: flex-end;
}
</style>
</head>
<body>
<section>
<div></div>
<div></div>
<div></div>
</section>
</body>
</html>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0856f6f7d45048fd7d8d7ed7a84a97b6.png)