视频来源:【黑马!真的很详细!】CSS3-flex布局(flex布局的单个知识点忘记也可以复习)_哔哩哔哩_bilibili
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
display: flex;
width: 80%;
height: 300px;
background-color: pink;
}
div span{
width: 150px;
height: 100px;
background-color:rebeccapurple ;
scroll-margin-inline: 5px;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
display: flex;
width: 80%;
height: 300px;
background-color: pink;
justify-content: space-evenly;
}
div span{
width: 150px;
height: 100px;
background-color:rebeccapurple ;
margin-right: 5px;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
2.平均分为3等分:(把宽度去掉,让flex变为1)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
/* 给父级容器添加flex属性 */
display: flex;
/* 默认的主轴是x轴,行;那么y轴就是侧轴了 */
/* flex-direction: row; */
/* 我们的元素是跟着主轴来排列的 */
/* 简单了解 翻转 */
/* flex-direction: row-reverse; */
/* 我们可以把我们的主轴设置为y轴 那么x轴就成了侧轴 */
flex-direction: column;
width: 800px;
height: 300px;
background-color: rebeccapurple;
}
div span{
width: 150px;
height: 100px;
background-color: pink;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
先以主轴为x轴来看
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
display: flex;
width: 800px;
height: 300px;
background-color: pink;
/* 默认的主轴是x轴,row,相当于默认就写了下面的这句代码 */
flex-direction: row;
/* justify-content: 是设置主轴上子元素的排列方式 相当于默认就写了下面的这句代码 */
/* 左对齐 */
/* justify-content: flex-start; */
/* 右对齐 */
/* justify-content: flex-end; */
/* 居中对齐 */
/* justify-content: center; */
/* 平分剩余空间 */
/* justify-content: space-around; */
/* 先两边贴边,再平分剩余的空间 */
justify-content: space-between;
}
div span{
width: 150px;
height: 100px;
background-color: purple;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
</body>
</html>
后以主轴为y轴来看
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
display: flex;
width: 800px;
height: 300px;
background-color: pink;
/* 默认的主轴是x轴,row,现在将主轴设置为y轴 */
flex-direction: column;
/* justify-content: 是设置主轴上子元素的排列方式 相当于默认就写了下面的这句代码 */
/* 左对齐 */
/* justify-content: flex-start; */
/* 右对齐 */
/* justify-content: flex-end; */
/* 居中对齐 */
/* justify-content: center; */
/* 平分剩余空间 */
/* justify-content: space-around; */
/* 先两边贴边,再平分剩余的空间 */
justify-content: space-between;
}
div span{
width: 550px;
height: 30px;
background-color: purple;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
display: flex;
width: 600px;
height: 400px;
background-color: pink;
/* flex布局中,默认的子元素是不换行的,如果装不下就会把子元素缩小了放在父元素里面*/
/* 默认就相当于写了下面的这句话 */
/* flex-wrap: nowrap; */
/* 如果装不下就另外起一行,不改变子元素的大小 */
flex-wrap: wrap;
}
div span{
width: 150px;
height: 100px;
background-color: purple;
margin: 10px;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>4</span>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
display: flex;
width: 800px;
height: 300px;
background-color: pink;
/* 默认的主轴是x轴row */
justify-content: center;
/* 我们需要侧轴居中 */
align-items: center;
/* stretch拉伸,但是子盒子不要给高度 */
/* align-items: stretch; */
}
div span{
width: 150px;
height: 100px;
background-color: purple;
margin: 10px;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
display: flex;
width: 800px;
height: 300px;
background-color: pink;
/* 换行,默认是不换行的yo */
flex-wrap: wrap;
/* 因为有了换行,此时我们侧轴上控制子元素的对齐方式用 align-content*/
/* align-content: flex-start; */
/* align-content: center; */
/* 上面的贴上沿,下面的贴下沿 */
/* align-content: space-between; */
/* 上沿有一个距离,下沿有一个距离,中间有两个距离 */
align-content: space-around;
}
div span{
width: 150px;
height: 100px;
background-color: purple;
margin: 10px;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
display: flex;
width: 800px;
height: 300px;
background-color: pink;
/* 设置主轴为y轴,并且让它自动换行 */
/* flex-direction: column;
flex-wrap: wrap; */
/* 设置主轴方向和是否换行(换列) 简写 */
flex-flow: column wrap;
}
div span{
width: 150px;
height: 100px;
background-color: purple;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
</div>
</body>
</html>