1 弹性盒子换行

1.1 实现重点
·使用justify-content: space-between;实现主轴对齐
·使用margin-bottom: 14px;实现小li上下间隙
·使用flex-wrap: wrap;实现弹性盒子换行
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>
/* margin: 0 和 padding: 0 用于清除所有元素的默认外边距和内边距 */
/* box-sizing: border-box 设置盒模型的计算方式,使得元素的宽度和高度包含内边距和边框 */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.mi {
/* 父控子(布局) */
display: flex;
/* 主轴分布(两个子盒子贴两边) */
justify-content: space-between;
width: 1226px;
height: 614px;
/* background-color: pink; */
margin: 100px auto;
}
.left {
width: 234px;
height: 614px;
background-color: skyblue;
}
.right {
width: 978px;
height: 614px;
background-color: purple;
}
.right ul {
display: flex;
/* 弹性盒子换行 */
flex-wrap: wrap;
justify-content: space-between;
}
.right li {
width: 234px;
height: 300px;
background-color: orange;
/* 去除小li的小点 */
list-style: none;
margin-bottom: 14px;
}
</style>
</head>
<body>
<div class="mi">
<div class="left"></div>
<div class="right">
<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>
</div>
</body>
</html>
结果:

2 多行主轴对齐(align-content)
align-content与align-items(测轴对齐)的区别是前者是多行对齐,后者是单行对齐

2.1 实现重点
·使用justify-content: space-between;实现左右没缝隙(主轴对齐)
·使用align-content: space-between;实现上下没缝隙(测轴对齐)
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>
.box {
display: flex;
flex-wrap: wrap;
/* 左右没缝隙(主轴对齐) */
justify-content: space-between;
/* 上下没缝隙(测轴对齐) */
align-content: space-between;
width: 1000px;
height: 500px;
border: 1px solid #000;
margin: 100px auto;
}
.box div {
width: 300px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div class="box">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
</body>
</html>
结果:

3 某个弹性盒子测轴对齐方式(align-self)
3.1 实现重点
·使用align-self实现只移动一个子盒子
·再在css中使用.box:nth-child(n)选中某个盒子给他添加样式
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>
.box {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
/* 只让其中一个移动 */
width: 600px;
height: 600px;
border: 1px solid #000;
margin: 100px auto;
}
.box div {
width: 100px;
height: 100px;
background-color: pink;
border-radius: 50%;
}
.box div:nth-child(2) {
/* 只移动某一个盒子 */
align-self: center;
}
.box div:nth-child(3) {
align-self: flex-end;
}
</style>
</head>
<body>
<div class="box">
<div></div>
<div></div>
<div></div>
</div>
</body>
</html>
结果:

4 修改主轴方向(flex-direction)

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>
.box {
display: flex;
/* 默认主轴是横向排列(row),此时要更改主轴方向 */
flex-direction: column;
width: 300px;
height: 300px;
border: 1px solid #000;
/* margin: 100px auto; */
justify-content: center;
align-items: center;
}
.box div {
width: 100px;
height: 100px;
background-color: pink;
}
</style>
</head>
<body>
<div class="box">
<div>1</div>
<div>2</div>
</div>
</body>
</html>
结果:

5 弹性伸缩比(align-content)

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>
.box {
display: flex;
/* 宽度占浏览器的百分之八十 */
width: 80%;
height: 150px;
border: 1px solid #000;
margin: 50px auto;
}
.box div {
background-color: pink;
/* 给子盒子添加 */
/* 子盒子平分父盒子的宽度 写∏就代表分几份*/
flex: 1;
}
.box div:nth-child(2) {
background-color: purple;
flex: 2;
margin: 0 10px;
}
</style>
</head>
<body>
<div class="box">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</body>
</html>
结果:
