怪异盒模型 box-sizing: border-box;
- 我希望我的盒子像
button一样懂事,但凡设置了宽高,那么无论再怎么设置padding、border我都是之前的宽高 box-sizing: content-box;标准盒模型(默认值)
- 你设置的
width从内容区算 ```html <!DOCTYPE html>
<a name="KHUZp"></a># 弹性盒<a name="mpXJd"></a>## 先感受一下弹性盒的美好```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>nav {display: flex;width: 1200px;height: 100px;line-height: 100px;margin: 0 auto;text-align: center;background: pink;}section {border-right: 1px solid #000;flex: 1;}</style></head><body><nav><section>全部商品</section><section>全商品</section><section>商品</section><section>全部的商品</section><section>全商品</section><section>全部商品</section><section>全部商品</section></nav></body></html>
组成
<div id="box">
<div class="small">
<div class="little"></div>
</div>
</div>
容器
- 你给谁设置了
display:flex;,那么谁就是容器 - 一般可以视为父元素
- 参考上面案例
- 容器里面的子元素,称为“项目”(
.small就是#box的项目) - 当然了,项目也可以设置
display:flex;那么此时项目就是孙子级的容器 .small也可以设置display:flex;那么.little就是.small的项目主轴
项目排列的方向
-
垂直交叉轴
就是和主轴成90°的轴,默认是Y轴
- 如果你将主轴更改了,假设将主轴更改为Y轴,那么,此时垂直交叉轴就是X轴
可以给父元素设置哪些属性呢?(弹性盒的属性)
设置弹性盒 display: flex;
更改主轴的方向 flex-direction
flex-direction: row; /* 默认值 可以理解为X轴 */
/* 12345 */
flex-direction: row-reverse; /* 主轴为水平反向 */
/* 54321 */
flex-direction: column; /* 将主轴方向设置为纵向 */
/*
1
2
3
4
5
*/
flex-direction: column-reverse; /* 将主轴方向设置为纵向反向 */
/*
5
4
3
2
1
*/
项目是否折行 flex-wrap
flex-wrap: nowrap;/* 默认值 项目多的时候不折行 */
flex-wrap: wrap;/* 折行 */
flex-wrap: wrap-reverse; /* 反向折行 */
复合属性 flex-flow(了解)
主轴的方向与项目是否折行的简写
flex-flow: column wrap;项目在主轴上的分布 justify-content
注意!设置给容器,是容器的属性
justify-content: flex-start; /* 默认值 主轴的开端 */ justify-content: center; /* 让项目整体居中于主轴 */ justify-content: flex-end; /* 让项目整体居于主轴末端 */ justify-content: space-around; /* 项目在主轴上两端环绕对齐 左右间距等宽 */ justify-content: space-between; /* 项目在主轴上两端顶格对齐 */ justify-content: space-evenly;/* 项目在主轴上两端等距对齐 */项目在垂直交叉轴的分布情况 align-items
注意!设置给容器,是容器的属性
/* 假设 你现在没有给项目(子元素)设置高度 */ align-items: stretch;/* 默认值 项目没有设置高度 高度和容器一致 */ align-items: flex-start; /* 项目们居于垂直交叉轴上方 */ align-items: center; /* 项目们居中于垂直交叉轴 */ align-items: flex-end; /* 项目们居于垂直交叉轴末端 */ align-items: baseline;多行项目在垂直交叉轴的排列方式 align-content
注意!首先,项目要多;其次,需要设置折行
flex-wrap: wrap;```css flex-wrap: wrap;/ 先设置让它折行 /
align-content: stretch;/ 默认值 项目没有设置高度 会均分容器高度 / align-content: flex-start; / 项目们都在垂直交叉轴上方 / align-content: center;/ 项目们都在垂直交叉轴中间 / align-content: flex-end;/ 项目都在垂直交叉轴末端 / align-content: space-around;/ 项目们在垂直交叉轴两端环绕 / align-content: space-between;/ 项目们在垂直交叉轴两端顶格对齐 / align-content: space-evenly;/ 等距对齐 /

<a name="FQq8Z"></a>
### 完整代码
```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>
.big {
width: 1000px;
height: 800px;
background: green;
margin: 0 auto;
/* 1、设置弹性盒 */
display: flex;
/* 2、更改主轴的方向 */
flex-direction: row;/* 默认值 可以理解为X轴 */
flex-direction: row-reverse;/* 主轴为水平反向 */
flex-direction: column;/* 将主轴方向设置为纵向 */
flex-direction: column-reverse;/* 将主轴方向设置为纵向反向 */
/* 3、项目是否折行 */
flex-wrap: nowrap;/* 默认情况下 项目多的时候 我是不折行的 */
flex-wrap: wrap;/* 折行 */
flex-wrap: wrap-reverse;/* 反向折行 */
/* 4、了解 复合属性 主轴的方向 与 项目是否折行的简写 */
flex-flow: column wrap;
/* 5、项目在主轴上的分布 注意 设置给容器 是容器的属性 */
justify-content: flex-start;/* 默认值 主轴的开端 */
justify-content: center;/* 让项目整体居中于主轴 */
justify-content: flex-end;/* 让项目整体居于主轴末端 */
justify-content: space-around;/* 项目在主轴上两端环绕对齐 */
justify-content: space-between;/* 项目在主轴上两端顶格对齐 */
justify-content: space-evenly;/* 项目在主轴上两端等距对齐 */
/* 6、项目在垂直交叉轴的分布情况 注意 设置给容器 是容器的属性 */
/* 假设 你现在没有给项目(子元素)设置高度 */
align-items: stretch;/* 默认值 项目没有设置高度 高度和容器一致 */
align-items: flex-start;/* 项目们居于垂直交叉轴上方 */
align-items: center;/* 项目们居中于垂直交叉轴 */
align-items: flex-end;/* 项目们居于垂直交叉轴末端 */
align-items: baseline;
/* 7、多行项目在垂直交叉轴的排列方式 那你注意 首先 项目要多 其次 需要设置折行 */
flex-wrap: wrap;
align-content: stretch;/* 默认值 项目没有设置高度 会均分容器高度 */
align-content: flex-start;/* 项目们都在垂直交叉轴上方 */
align-content: center;/* 项目们都在垂直交叉轴中间 */
align-content: flex-end;/* 项目都在垂直交叉轴末端 */
align-content: space-around;/* 项目们在垂直交叉轴两端环绕 */
align-content: space-between;/* 项目们在垂直交叉轴两端顶格对齐 */
align-content: space-evenly;/* 等距对齐 */
}
section {
width: 100px;
height: 100px;
border: 1px solid #000;
/* 我希望它依旧是100*100的 */
box-sizing: border-box;
background: skyblue;
}
</style>
</head>
<body>
<div class="big">
<section style="line-height: 20px;">1</section>
<section style="line-height: 40px;">2</section>
<section style="line-height: 50px;">3</section>
<section style="line-height: 30px;">4</section>
<section style="line-height: 70px;">5</section>
<section>6</section>
<section>7</section>
<section>8</section>
<section>9</section>
<section>10</section>
<section>11</section>
<section>12</section>
<section>13</section>
<section>14</section>
<section>15</section>
<section>16</section>
<section>17</section>
<section>18</section>
<section>19</section>
<section>20</section>
<section>21</section>
<section>22</section>
<section>23</section>
</div>
</body>
</html>
行内弹性盒 display: inline-flex;
display: inline-flex;行内弹性盒(类似行内块)- 可以让其他元素与弹性盒在一行内显示 ```html <!DOCTYPE html>
弹性盒
弹性盒
弹性盒
弹性盒
弹性盒
行内元素

<a name="K5dZb"></a>
## 可以给子元素设置哪些属性呢?(弹性盒项目的属性)
<a name="YKqRt"></a>
### 项目的排列顺序 order: auto/0/1/-1;
1. `order: auto/0;` 默认值
- 值越大越在后面,可以为负值
- 在代码中,与其他代码依次排序
2. `order: -1;` 置前
2. `order: 1;` 置后
")")")
<a name="Dqi1U"></a>
### 项目在垂直交叉轴的排列方向 align-self
```css
align-self: stretch;/* 默认值 不设置高度 高度和容器一致 */
align-self: flex-start;/* 居于垂直交叉轴上方 */
align-self: center;/* 居于垂直交叉轴中间 */
align-self: flex-end;/* 居于垂直交叉轴末端 */
完整代码
<!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>
.big {
width: 1000px;
height: 500px;
background: green;
margin: 0 auto;
/* 设置弹性盒 */
display: flex;
}
span {
width: 100px;
height: 100px;
border: 1px solid #000;
background: hotpink;
}
span:nth-of-type(2) {
/* 1、项目的排列顺序 order:auto;默认值 值越大越在后面 可以为负值 */
order: -1;
background: skyblue;
/* 2、项目在垂直交叉轴的排列方向 */
align-self: stretch; /* 默认值 不设置高度 高度和容器一致 */
align-self: flex-start; /* 居于垂直交叉轴上方 */
align-self: center; /* 居于垂直交叉轴中间 */
align-self: flex-end; /* 居于垂直交叉轴末端 */
}
</style>
</head>
<body>
<div class="big">
<span>1</span><!-- span标签在弹性盒内可设置宽高 -->
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
</div>
</body>
</html>
超赞的 flex:1;
flex:1;均分父元素宽度 ```html <!DOCTYPE html>

<a name="Yz3IK"></a>
#### 复合属性 flex:1; 都代表了什么
1. `flex-grow: 1;` 放大
- 将剩余空间均分后,平均分配给每个元素
```css
b:nth-of-type(2) {
flex-grow: 3;/* 将剩余空间分成四份 取三份 */
}
b:nth-of-type(3) {
flex-grow: 1;/* 将剩余空间分成四份 取一份 */
}
flex-shrink: 1;缩小
- 默认值,项目在超出容器宽度时,都统一缩小

flex-basis: 50%/300px;剩余空间
50%将剩余空间一半都给了它300px将剩余空间中的300px分给它 ```html <!DOCTYPE html>

<a name="yyWav"></a>
#### flex 补充
| 序号 | flex | 放大 | 缩小 | 剩余空间 |
| --- | --- | --- | --- | --- |
| 0 | flex: 1; | 1 | 1 | 0% |
| 1 | flex: auto; | 1 | 1 | auto |
| 2 | flex: none; | 0 | 0 | auto |
<a name="BXCPd"></a>
## demo-骰子布局
```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>骰子布局</title>
<style>
div {
/* 设置弹性盒 */
display: flex;
width: 200px;
height: 300px;
margin: 50px auto;
background: #DDD;
/* 水平 垂直 模糊 阴影 颜色 */
box-shadow: 0px 0px 20px 3px #888;
border-radius: 10px;
}
span {
width: 80px;
height: 80px;
background: red;
border-radius: 50%;
}
/* --------一筒-------- */
div:nth-of-type(1) {
/* 项目在主轴上水平居中 */
justify-content: center;
/* 项目在垂直交叉轴居中 */
align-items: center;
}
/* --------二筒-------- */
div:nth-of-type(2) {
/* 更改主轴的方向 */
flex-direction: column;
/* 项目在垂直交叉轴居中 */
align-items: center;
/* 项目在主轴上两端环绕对齐 */
justify-content: space-around;
}
/* --------三筒-------- */
div:nth-of-type(3) {
/* 更改主轴的方向 */
flex-direction: column;
/* 项目在主轴上两端环绕对齐 */
justify-content: space-around;
}
div:nth-of-type(3) span:nth-of-type(2) {
/* 单独在垂直交叉轴居中 */
align-self: center;
}
div:nth-of-type(3) span:last-of-type {
/* 单独在垂直交叉轴居中 */
align-self: flex-end;
}
/* --------四筒-------- */
div:last-of-type {
/* 项目折行 */
flex-wrap: wrap;
justify-content: space-around;
/* 多行项目 在垂直交叉轴两端环绕 */
align-content: space-around;
}
</style>
</head>
<body>
<!-- 一筒 -->
<div>
<span></span>
</div>
<!-- 二筒 -->
<div>
<span></span><span></span>
</div>
<!-- 三筒 -->
<div>
<span></span><span></span><span></span>
</div>
<!-- 四筒 -->
<div>
<span></span><span></span><span></span><span></span>
</div>
</body>
</html>
多列布局
column-count: 5;列数
- 你想要横向排几列(设置给父元素)

column-width: 600px;列宽
- 与列数不可同时使用
- 要么,设置列数(就是有几列)
- 要么,设置列宽
column-gap: 20px;列间距

column-rule: 2px dotted #0f0;列分割线

- 注意 一定是给每一列小块设置
column-span: all;横跨所有行 ```html <!DOCTYPE html>
五福临门喜迎财神五福临门喜迎财神五福临门喜迎财神

<a name="I1r95"></a>
## demo-京东

```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>
main {
width: 1275px;
height: auto;
margin: 0 auto;
background: palevioletred url(../images/a.png);
column-count: 5;
}
.small {
width: 200px;
height: 300px;
border: 1px solid #000;
margin-bottom: 20px;
background: pink;
}
</style>
</head>
<body>
<main>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
</main>
</body>
</html>
媒体查询@media
参考下面的网站
<!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>
div {
width: 100%;
height: 200px;
}
/* 媒体查询 @media 注意!and前后有空格 */
@media screen and (min-width:1200px) {
div {
background: orangered;
}
}
/* ----------------700~1200---- */
@media screen and (max-width:1200px) and (min-width:700px) {
div {
background: blueviolet;
}
}
/* ----------------700以下---- */
@media screen and (max-width:700px) {
div {
background: orange;
}
}
</style>
</head>
<body>
<div></div>
</body>
</html>
响应式
参考下面的网站
<!-- 练习:960以上 700~960一个 700以下一个 -->
<!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>
/* -------这里写的 就是全局状态下 也就是大于1000 ------- */
* {
margin: 0;
padding: 0;
}
nav {
display: flex;/* 设置弹性盒 让头部左右两个块 横向展示 */
justify-content: space-between;/* 项目(头部左右)两端顶格/环绕对齐 */
width: 100%;/* 设置占一行宽度 */
height: 60px;/* 设置高度 */
padding: 0 10%;/* 设置内容距离左右有个间距 */
box-sizing: border-box;/* 因为设置padding会长胖 还希望是100% 就设置了怪异盒模型 */
background: #444;/* 设置背景色 */
line-height: 60px;/* 设置行高=高度值 文字垂直居中 */
/* 文字大小和颜色可以继承 直接设置给nav */
font-size: 12px;
color: #ccc;
}
.left {
display: flex;/* 设置弹性盒子 目的是让左边的7个section横向展示 */
/* background: #f90; */
}
section {
padding: 0 10px;/* 让每个文字左右有间距 */
}
.left section:first-of-type {
font-size: 18px;/* 左边第一个文字字体大 */
}
aside {
display: flex;/* 右侧设置弹性盒子 */
align-items: center;/* 让项目水平居中 其实就是为了让图片垂直居中 */
/* background: skyblue; */
}
img {
display: none;/* 图片默认不显示 */
}
/* 开始媒体查询 页面宽度到1000的时候 左侧第一个和右侧第一个消失 */
@media screen and (max-width:1000px) and (min-width:700px) {
/* 这个时候怎么了 左边第一个和右边第一个没啦 */
section:first-of-type {
display: none;
}
}
/* -------700以下的时候 左边只剩下第一个section 右边呢 出现一张图片 剩下其他的都没啦*/
@media screen and (max-width:700px) {
section {
display: none;/* 所有的section都消失 */
}
.left section:first-of-type {
display: block;/* 左边第一个section是出现的 我们再设置一下 让人家出现 */
}
img {
display: block;/* 右边的图片显示 */
}
}
</style>https://www.bootcss.com/
</head>
<body>
<nav>
<!-- 头部左侧 -->
<div class="left">
<section>Bootstrap中文网</section>
<section>Bootstrap3中文文档</section>
<section>Bootstrap4中文文档</section>
<section>Sass 教程</section>
<section>Less 教程</section>
<section>jQuery API</section>
<section>网站实例</section>
</div>
<!-- 头部右侧 -->
<aside>
<!-- 用div标签也可以 -->
<section>关于</section>
<img src="../images/4.png" alt="">
</aside>
</nav>
</body>
</html>
响应式布局特点
- 设计特点:
- 面对不同分辨率设备灵活性强
- 能够快捷解决多设备显示适应问题
- 缺点
- 兼容各种设备工作量大,效率低下
- 代码累赘,会出现隐藏无用的元素,加载时间加长
- 其实这是一种折中性质的设计解决方案,多方面因素影响而达不到最佳效果
- 一定程度上改变了网站原有的布局结构,会出现用户混淆的情况



