查缺补漏
HTML
一、表格标签:(表格是用来展示数据的)
<table></table>:用于定义表格的标签
( align:规定表格相对周围的对齐方式<可选left、center、right>
border:规定表格单元是否有边框<1或0>
cellpadding:规定文字与单元格之间的距离<默认1像素>
cellspacing:规定单元格与单元格之间的距离<默认2像素>
width:规定表格的宽度
height:规定表格的高度
rules:”all”:细线表格
)
<caption></caption>
:放在table里,整个表的标题
<thead></thead>
:用于定义表格头部,内必须有,一般位于第一行
<tbody></tbody>
:用于定义表格主体,主要用于存放数据
<tfoot></tfoot>
:定义表格底部
<th></th>
:表头单元格,文字会加粗居中(用于替换td)
<tr></tr>
:用于定义表格中的行,必须嵌套在
标签中,只能包含td和th
<td></td>
:用于定义表格中的单元格,必须嵌套在
标签中,里面可以包含任何内容
合并单元格:
跨行合并:rowspan=“合并单元格个数”
跨列合并:colspan=“合并单元格个数”
(在目标单元格写合并代码:跨行(最上侧单元格)、跨列(最左侧单元格))
合并三步曲:
1、先确定跨行还是跨列合并
2、在目标单元格写合并方式,如:
3、删除合并后多余的单元格
二、列表标签:(列表是用来布局的)
1、无序列表 <ul></ul>
(重点)
(1)无序列表中各列表项之间没有顺序级别之分,是并列的
(2)<ul>
中只能嵌套<li></li>
,输入其他标签或文字都不行
(3)<li></li>
相当于一个容器,可以放任何元素(文字、链接、图片)
(4)无序列表会带有自己的样式属性,但在实际开发中会使用css来设置
(5)去掉li前面的小圆点:list-style: none;
2、有序列表 <ol></ol>
(理解有这个列表就好,用的不是很多)
(1)<ul>
中只能嵌套<li></li>
,输入其他标签或文字都不行
(2)其余与无序列表一致
3、自定义列表
(重点)
(1)<dl></dl>
里面只能包含<dt>
和<dd>
(2)<dt>
和<dd>
没有个数限制,经常是一个<dt>
对应多个<dd>
(3)<dt>
和<dd>
不是嵌套关系,是“兄弟”关系
(4)<dt>
和<dd>
里可以放任何元素
基本语法:
<dl>
<dt>`名词1`</dt>
<dd>`名词1解释1`</dd>
<dd>`名词1解释2`</dd>
</dl>
语义化标签
<header>
:头部标签<nav>
:导航标签<article>
:内容标签<section>
:定义文档的某个区域<aside>
:侧边栏标签<footer>
:尾部标签
三、表单标签 (完整的表单由 表单域、表单控件<表单元素>、提示信息构成)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QzNVPhJf-1658241798629)(C:\Users\86135\AppData\Roaming\Typora\typora-user-images\image-20220612093354451.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6T1Ket0J-1658241798631)(C:\Users\86135\AppData\Roaming\Typora\typora-user-images\image-20220612093525546.png)]
1、表单域<form>:
包含表单元素的区域
表单默认值
placeholder=“请输入邮箱”
<form>
会把它范围内的表单元素信息提交给服务器
如: <form action="url地址" method="提交方式" name="表单域名称">
各种表单元素控件
</form>
(action:用于指定接收并处理表单数据的服务器程序的url地址
method:用于设置表单数据的提交方式,取值为get或post或put
name:用于指定表单的名称,以区分同一个页面中的多个表单域)
*2、表单元素:*
(1)<input>:
用于收集用户信息
<input type="属性值" name="" value="" />
<input />
为单标签
type可以设置不同类型的控件(文本字段、复选框、掩码后的文本控件、按钮等)
type属性的属性值:
button:定义可点击按钮(多数情况下,后期通过Javascript启动脚本)
file:定义输入字段和浏览按钮供文件上传(如上传头像)
<input type=”file” multiple>
多文件上传
hidden:定义隐藏的输入字段
image:定义图像形式的提交按钮
password:定义密码字段,该字段中的字符被掩码
radio:定义单选按钮(多选一:必须有相同名字的name属性才能实现多选一)
如:性别:男<input type="radio">
女<input type="radio">
checkbox:定义复选框(多选多)
reset:定义重置按钮(重置按钮会清除表单中所有数据)
submit:定义提交按钮(提交按钮会把表单数据发送到服务器)
text :定义单行输入字段,可在其中输入文本(默认宽度为20字符)
name属性:定义input元素的名称
(单选按钮radio和复选框checkbox要有相同的name值)
value属性:表单元素中默认出现的字,如密码框中的”请输入密码“、又或者是按钮中显示的文字
checked属性:规定此input元素首次加载时应当被选中
(主要针对单选按钮和复选框,一打开页面就默认选中某个表单元素)
maxlength属性:规定输入字段中的字符最大长度
标签
标签用于绑定一个表单元素,当点击标签内的文本时,相当于点到相对于的表单元素,用来增加用户体验
方法一: 语法:<label for=”sex”>
男</label>
<input type=”radio” name=”sex” id=”sex”>
(<label>
标签的for属性应与相关元素的id属性相同)(用label把内容包裹起来)
方法二:直接用label把内容和标签一起包裹起来(需要去掉for和id)
(2)select下拉表单元素
语法:
<select>
<option>选项1</option>
<option>选项2</option>
<option>选项3</option>
…
</select>
1、<select>
中至少包含一对<option>
2、在<option>
里定义selected时,当前项即位默认选中项
(3)textarea文本域元素
语法:
<textarea row=”3” cols=”20”>文本内容<textarea>
1、通过文本域标签可轻松的创建多行文本输入框
2、row和cols不需要记,实际开发中通过css改变
3、文本域代码最好写在一行上,不然用户输入时会有空白缝隙
音视频标签
音视频标签: 在html5中,controls的意思为“控制”,是audio和video标签的一个属性,用于规定浏览器为视频或音频提供播放控件(例播放、暂停、定位、音量等),语法“<video controls>”或“<audio controls>”。
<video width="320" height="240" controls>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
您的浏览器不支持 video 标签。
</video>
<video width="320" height="240">
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
您的浏览器不支持 video 标签。
</video>
<audio src="./img/majiko - 心做し.mp3" controls></audio>
<hr>
<audio controls height="100" width="100">
<source src="./img/majiko - 心做し.mp3" type="audio/mp3">
</audio>
<hr>
<video src="./img/1.mp4" controls></video>
<hr>
<video width="320" height="240" controls="controls">
<source src="./img/1.mp4" type="video/mp4" />
<object data="movie.mp4" width="320" height="240">
<embed src="movie.swf" width="320" height="240" />
</object>
</video>
CSS
取消无序列表样式:
取消方法:
1、利用“list-style”属性取消无序列表的小黑点样式,语法为“无序列表{list-style:none}”;
2、利用display属性取消无序列表成行显示样式,语法为“无序列表{display:inline}”。
取消 a 默认的下划线
a {
/* 取消 a 默认的下划线 */
text-decoration: none;
color: #333333;
/* 字体大小 */
font-size: 20px;
/* 文字水平居中 */
text-align: center;
/* 文字垂直居中 行号等于高度*/
line-height:300px ;
}
设置鼠标的显示方式
li:hover {
color:#ff6700
cursor:pointer
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ywiIbWaa-1658241798632)(C:\Users\86135\AppData\Roaming\Typora\typora-user-images\image-20220608162905841.png)]
设置a标签的列表排序
让a转换为块级元素,给个高度,给
a {
display: block;
width: 230px;
height: 40px;
background-color: #55585a;
font-size: 14px;
color: #fff;
//去掉下划线
text-decoration: none;
距离左边的距离 也可以用padding
text-indent: 2em;
/* 一个小技巧:单行文字垂直居中的代码,让文字的行高等于盒子的高度 */
line-height: 40px;
}
/* 2、鼠标经过链接变换背景颜色 */
a:hover {
background-color: #ff6700;
}
文字水平居中
div {
text-align: center;
}
zhoujiruizhoujirui
text-align
属性用于设置元素内文本内容的水平对齐方式。
text-indent` 属性用来指定文本的第一行的缩进,通常是将段落的首行缩进。
通过设置该属性,所有元素的第一行都可以缩进一个给定的长度,甚至该长度可以是负值。
p {
text-indent: 2em;
}
em 是一个相对单位,就是当前元素 (font-size) 1 个文字的大小,如果当前元素没有设置大小,则会按照父元素的 1 个文字大小。
水平菜单
把 li
元素修改成 inline,制作成水平菜单
渐变背景
#grad1 {
height: 200px;
/* 浏览器不支持时显示 */
background-color: red;
/* 线性渐变 - 从上到下(默认情况下)*/
background-image: linear-gradient(#e66465, #9198e5);
}
计算布局不需要计算盒子的内外边框
* { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; }
表格的编写
<table align="center" cellspacing="0">
<!-- cellspacing="0" 是让表格的 边框为0 也就是直线分割 -->
<thead>
<tr>
<th>排名</th>
<th>关键词</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>鬼吹灯</td>
</tr>
<style>
table,
th,
td {
border: 1px solid;
text-align: center;
/* 合并相邻的单元格 */
border-collapse: collapse;
}
</style>
清除相邻的框框
border-collapse
属性控制浏览器绘制表格边框的方式。
它控制相邻单元格的边框。
语法:
border-collapse: collapse;
collapse
单词是合并的意思border-collapse: collapse;
表示相邻边框合并在一起
table,
td,
th {
border: 1px solid black;
/* 合并相邻的边框 */
border-collapse: collapse;
font-size: 14px;
text-align: center;
}
实现旋转过渡
img {
height: 300px;
width: 300px;
border-radius: 150px;
border: 5px solid skyblue;
transition: all 0.5s
}
img:hover {
transform: rotate(360deg)
}
3.2 边框会影响盒子实际大小
边框会额外增加盒子的实际区域大小。因此我们有两种方案解决:
- 测量盒子大小的时候,不量边框
- 如果测量的时候包含了边框,则需要 width、height 减去边框宽度(注意减单边还是双边)
注意:盒子实际区域大小 = 内容区大小 + 内边距大小 + 边框大小 + 外边距大小
2.8 小技巧:单行文字垂直居中
CSS 没有给我们提供文字垂直居中的代码,这里我们可以使用一个小技巧来实现。
**解决方案:**让 文字的行高
等于 盒子的高度
就可以让文字在当前盒子内垂直居中。
**简单理解:**行高的上空隙和下空隙把文字挤到中间了,如果行高小于盒子高度,文字会偏上,如果行高大于盒子高度,则文字偏下。
3.3 字体粗细
CSS 使用 font-weight 属性设置文本字体的粗细。
p {
font-weight: bold;
}
属性值 | 描述 |
---|---|
normal | 默认值(不加粗的) |
bold | 定义粗体(加粗的) |
100-900 | 400 等同于 normal,而 700 等同于 bold,其它值一般不使用,注意这个数字后面不跟单位 |
- 学会让加粗标签(比如 h 和 strong 等)变为不加粗,或者让其他标签加粗
- 实际开发时,我们更喜欢用数字表示粗细
3.4 文字样式
CSS 使用 font-style 属性设置文本的风格。
p {
font-style: normal;
}
属性值 | 作用 |
---|---|
normal | 默认值,浏览器会显示标准的字体样式 font-style: normal; |
italic | 浏览器会显示斜体的字体样式 |
3.5 字体复合属性
字体属性可以把以上文字样式综合来写,这样可以更节约代码。
body {
font: font-style font-weight font-size/line-height font-family;
}
body {
font: normal 400 font-size/line-height "Microsoft YaHei", Arial, sans-serif;
}
- 使用 font 属性时,必须按上面语法格式中的顺序书写,不能更换顺序,并且各个属性间以空格隔开
- 不需要设置的属性可以省略(取默认值),但必须保留 font-size 和 font-family 属性,否则 font 属性将不起作用
3.6 字体属性总结
属性 | 表示 | 注意点 |
---|---|---|
font-size | 字号 | 我们通常用的单位是 px 像素,一定要跟上单位 |
font-family | 字体 | 实际工作中按照团队约定来写字体 |
font-weight | 字体属性 | 记住加粗是 700 或者 bold 不加粗 是 normal 或者 400 记住数字不要跟单位 |
font-style | 字体样式 | 记住倾斜是 italic 不倾斜是 normal 工作中我们最常用 normal |
font | 字体连写 | 1、字体连写是有顺序的不能随意换位置,2、其中字号和字体必须同时出现 |
4.2 文本对齐
text-align
属性用于设置元素内文本内容的水平对齐方式。
div {
text-align: center;
}
属性值 | 解释 |
---|---|
left | 左对齐(默认值) |
rigth | 右对齐 |
center | 居中对齐 |
注意:
text-align
属性只能作用于 块级元素
,并让该块级元素内的 行内元素
实现居中(不一定是文字)。
上述例子中:h1 为块级元素,所以给 h1 设置 text-align,便会作用于里面的文本(如果里面还有行内元素的话,也会一同作用)。
4.3 文本装饰
text-decoration
属性规定添加到文本的修饰,可以给文本添加 下划线
、删除线
、上划线
等。
div {
text-decoration: underline;
}
属性值 | 描述 |
---|---|
none | 默认,没有装饰线(最常用) |
underline | 下划线,链接 a 自带下划线(常用) |
overline | 上划线(几乎不用) |
line-through | 删除线(不常用) |
text-indent` 属性用来指定文本的第一行的缩进,通常是将段落的首行缩进。
div {
text-indent: 10px;
}
通过设置该属性,所有元素的第一行都可以缩进一个给定的长度,甚至该长度可以是负值。
p {
text-indent: 2em;
}
em 是一个相对单位,就是当前元素 (font-size) 1 个文字的大小,如果当前元素没有设置大小,则会按照父元素的 1 个文字大小。
line-height
属性用于设置行间的距离(行高),可以控制文字行与行之间的距离。
p {
line-height: 26px;
}
-
行间距 = 上间距 + 文本高度 + 下间距
-
上下间距 = (行间距 - 文本高度)/ 2
-
文本高度 = font-size
2.2 块元素
常见的块元素有 <h1> ~ <h6>
、<p>
、<div>
、<ul>
、<ol>
、<li>
、<dl>
、<dt>
、<dd>
、<table>
、<tr>
、<form>
等,其中 <div>
标签是最典型的块元素。
块级元素的特点:
- 比较霸道,自己独占一行
- 高度,宽度、外边距以及内边距都可以控制
- 宽度默认是容器(父级宽度)的 100%
- 是一个容器及盒子,里面可以放行内或者块级元素
注意:
- 文字类的块级元素内不能放置块级元素,会发生语法错误
<p>
标签主要用于存放文字,因此<p>
里面不能放块级元素,特别是不能放<div>
- 同理,
<h1> ~ <h6>
等都是文字类块级标签,里面也不能放其他块级元素
2.3 行内元素
常见的行内元素有 <a>
、<span>
、<em>
、<strong>
等,其中 <span>
标签是最典型的行内元素,有的地方也将行内元素称为内联元素。
行内元素的特点:
-
相邻行内元素在一行上,一行可以显示多个
-
高、宽直接设置是无效的
-
默认宽度就是它本身内容的宽度
-
行内元素只能容纳文本或其他行内元素(a 除外)
注意:
- 链接里面不能再放链接
- 特殊情况链接
<a>
里面可以放块级元素,但是给<a>
转换一下块级模式最安全
2.4 行内块元素
在行内元素中有几个特殊的标签:<img>
、<input>
、<th>
、<td>
,它们同时具有 块元素
和 行内元素
的特点,有些资料称它们为 行内块元素
。
行内块元素的特点:
- 和相邻行内元素(行内块)在一行上,但是他们之间会有空白缝隙。一行可以显示多个(行内元素特点)
- 默认宽度就是它本身内容的宽度(行内元素特点)
- 高度,行高、外边距以及内边距都可以控制(块级元素特点)
2.5 元素显示模式总结
元素模式 | 元素排列 | 设置样式 | 默认宽度 | 包含 |
---|---|---|---|---|
块级元素 | 一行只能放一个块级元素 | 可以设置宽度和高度 | 容器的 100% | 容量级可以包含任何标签 |
行内元素 | 一行可以放多个行内元素 | 不可以直接设置宽度和高度 | 它本身内容的宽度 | 容纳文本或其他行内元素 |
行内块元素 | 一行放多个行内块元素 | 可以设置宽度和高度 | 它本身内容的宽度 |
学习元素显示模式的主要目的是分清它们各自的特点,当我们网页布局的时候,在合适的地方用合适的标签元素。
2.6 元素显示模式转换
特殊情况下,我们需要元素模式的转换,简单理解: 一个模式的元素需要另外一种模式的特性
比如:想要增加链接 <a>
的触发范围。
- 转换为块元素:
display: block;
(由于经常需要设置宽高,所以通常会将行内元素转换为块元素) - 转换为行内元素:
display: inline;
- 转换为行内块:
display: inline-block;
(常用)
优先级
- 选择器相同,则执行层叠性
- 选择器不同,则根据选择器权重执行
选择器权重如下表所示:
选择器 | 选择器权重 |
---|---|
继承 或 * | 0,0,0,0 |
元素选择器 | 0,0,0,1 |
类选择器、伪类选择器 | 0,0,1,0 |
ID 选择器 | 0,1,0,0 |
行内样式 style=“” | 1,0,0,0 |
!important 重要的 | ∞ 无穷大 |
**规则:**比较位级别,位级别相同时比较位大小。
2.2.1 属性选择器
属性选择器可以根据元素特定属性来选择元素。这样就可以不用借助于类或者 id 选择器。
选择符 | 简介 |
---|---|
E[att] | 选择具有 att 属性的 E 元素 |
E[att="val"] | 选择具有 att 属性且属性值等于 val 的 E 元素 |
E[att^="val"] | 匹配具有 att 属性且值以 val 开头的 E 元素 |
E[arr$="val"] | 匹配具有 att 属性且值以 val 结尾的 E 元素 |
E[att*="val"] | 匹配具有 att 属性且值中含有 val 的 E 元素 |
注意:类选择器、属性选择器、伪类选择器,权重为 10。
2.2.2 结构伪类选择器
nth-child(n)
选择某个父元素的一个或多个特定的子元素(重点)。
- n 可以是数字,关键字和公式
- n 如果是数字,就是选择第 n 个子元素,里面数字从 1 开始……
- n 可以是关键字:even 偶数,odd 奇数
- n 可以是公式:常见的公式如下(如果 n 是公式,则从 n = 0 开始计算,但是第 0 个元素和超出了元素的个数会被忽略)
公式 | 取值 |
---|---|
2n | 偶数(2、4、6、……) |
2n+1 | 奇数(1、3、5、……) |
5n | 5 10 15… |
n+5 | 从第 5 个开始(包含第 5 个)到最后 |
-n+5 | 前 5 个(包含第 5 个) |
结构伪类选择器主要根据文档结构来选择元素,常用于根据父级来选择其子元素。
选择器 | 简介 |
---|---|
E:first-child | 匹配父元素中的第一个子元素 E |
E:last-child | 匹配父元素中最后一个 E 元素 |
E:nth-child(n) | 匹配父元素中的第 n 个子元素 E |
E:first-of-type | 指定类型 E 的第一个 |
E:last-of-type | 指定类型 E 的最后一个 |
E:nth-of-type(n) | 指定类型 E 的第 n 个 |
区别:
- nth-child 对父元素里面所有孩子排序选择(序号是固定的) 先找到第 n 个孩子,然后看看是否和 E 匹配
- nth-of-type 对父元素里面指定子元素进行排序选择。 先去匹配 E,然后再根据 E 找第 n 个孩子
小结:
- 结构伪类选择器一般用于选择父级里面的第几个孩子
- nth-child 对父元素里面所有孩子排序选择(序号是固定的) 先找到第 n 个孩子,然后看看是否和 E 匹配
- nth-of-type 对父元素里面指定子元素进行排序选择。 先去匹配 E,然后再根据 E 找第 n 个孩子
- 若父元素内都是同一种标签(如:列表),优先用 nth-child,否则就只能使用 nth-of-type
- 类选择器、属性选择器、伪类选择器,权重为 10
2.2.3 伪元素选择器(重点)
伪元素选择器可以帮助我们利用 CSS 创建新标签元素,而不需要 HTML 标签,从而简化 HTML 结构。
选择器 | 简介 |
---|---|
::before | 在元素内容的前面插入内容 |
::after | 在元素内容的后面插入内容 |
注意:
- before 和 after 创建一个元素,属于行内元素
- 新创建的这个元素在文档树中是找不到的,所以我们称为伪元素
- 语法:
element::before{}
- before 和 after 必须有 content 属性
- before 在父元素内容的前面创建元素,after 在父元素内容的后面创建元素
- 伪元素选择器和标签选择器一样,权重为 1
(1)伪元素选择器使用场景1:伪元素字体图标
p::before {
position: absolute;
right: 20px;
top: 10px;
content: '\e91e';
font-size: 20px;
}
(2)伪元素选择器使用场景2:仿土豆效果
/* 当我们鼠标经过了 土豆这个盒子,就让里面 before 遮罩层显示出来 */
.tudou:hover::before {
/* 而是显示元素 */
display: block;
}
(3)伪元素选择器使用场景3:伪元素清除浮动
- 额外标签法也称为隔墙法,是 W3C 推荐的做法
- 父级添加 overflow 属性
- 父级添加 after 伪元素
- 父级添加双伪元素
额外标签法也称为隔墙法,是 W3C 推荐的做法。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NtNjzLo8-1658241798633)(mark-img/20210423160929744.png)]
注意:要求这个新的空标签必须是块级元素。
后面两种伪元素清除浮动算是第一种额外标签法的一个升级和优化。
.clearfix:after {
content: ""; /* 伪元素必须写的属性 */
display: block; /* 插入的元素必须是块级 */
height: 0; /* 不要看见这个元素 */
clear: both; /* 核心代码清除浮动 */
visibility: hidden; /* 不要看见这个元素 */
}
.clearfix:before,
.clearfix:after {
content: "";
display: table; /* 转换为块级元素并且一行显示 */
}
.clearfix:after {
clear: both;
}
案例:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>伪元素选择器before和after</title>
<style>
div {
width: 200px;
height: 200px;
background-color: salmon;
}
/* div::before 权重是 2 */
div::before {
/* 这个 content 是必须要写的 */
/* display: inline-block; */
content: '我';
/* width: 30px;
height: 40px;
background-color: purple; */
}
div::after {
content: '小猪佩奇';
}
</style>
</head>
<body>
<div>
是
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eZ7ogcpn-1658241798633)(mark-img/20210423153316679.gif)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>伪元素选择器使用场景-字体图标</title>
<style>
@font-face {
font-family: 'icomoon';
src: url('fonts/icomoon.eot?1lv3na');
src: url('fonts/icomoon.eot?1lv3na#iefix') format('embedded-opentype'),
url('fonts/icomoon.ttf?1lv3na') format('truetype'),
url('fonts/icomoon.woff?1lv3na') format('woff'),
url('fonts/icomoon.svg?1lv3na#icomoon') format('svg');
font-weight: normal;
font-style: normal;
font-display: block;
}
div {
position: relative;
width: 200px;
height: 35px;
border: 1px solid red;
}
div::after {
position: absolute;
top: 10px;
right: 10px;
font-family: 'icomoon';
/* content: ''; */
content: '\e91e';
color: red;
font-size: 18px;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-y7FhA55n-1658241798634)(mark-img/20210423153507576.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>伪元素选择器使用场景2-仿土豆网显示隐藏遮罩案例</title>
<style>
.tudou {
position: relative;
width: 444px;
height: 320px;
background-color: pink;
margin: 30px auto;
}
.tudou img {
width: 100%;
height: 100%;
}
.tudou::before {
content: '';
/* 隐藏遮罩层 */
display: none;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, .4) url(images/arr.png) no-repeat center;
}
/* 当我们鼠标经过了土豆这个盒子,就让里面 before 遮罩层显示出来 */
.tudou:hover::before {
/* 而是显示元素 */
display: block;
}
</style>
</head>
<body>
<div class="tudou">
<img src="images/tudou.jpg" alt="">
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bX2OVpTe-1658241798634)(mark-img/20210423153911315.gif)]
2.3 CSS3盒子模型
CSS3 中可以通过 box-sizing 来指定盒模型,有 2 个值:即可指定为 content-box、border-box,这样我们计算盒子大小的方式就发生了改变。
可以分成两种情况:
- box-sizing: content-box 盒子大小为 width + padding + border (以前默认的)
- box-sizing: border-box 盒子大小为 width
如果盒子模型我们改为了 box-sizing: border-box, 那 padding 和 border 就不会撑大盒子了(前提 padding 和 border 不会超过 width 宽度)
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>CSS3盒子模型</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
div {
width: 200px;
height: 200px;
background-color: pink;
border: 20px solid red;
padding: 15px;
box-sizing: content-box;
}
p {
width: 200px;
height: 200px;
background-color: pink;
border: 20px solid red;
padding: 15px;
/* css3 盒子模型 盒子最终的大小就是 width 200 的大小 */
box-sizing: border-box;
}
</style>
</head>
<body>
<div>
小猪乔治
</div>
<p>
小猪佩奇
</p>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aMdhT7iD-1658241798635)(mark-img/image-20220117012150036.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2cEAgYRK-1658241798635)(mark-img/image-20220117012211829.png)]
2.4 CSS3其他特性(了解)
- 图片变模糊
- 计算盒子宽度 width:calc 函数
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>图片模糊处理filter</title>
<style>
img {
/* blur 是一个函数 小括号里面数值越大,图片越模糊 注意数值要加 px 单位 */
filter: blur(15px);
}
img:hover {
filter: blur(0);
}
</style>
</head>
<body>
<img src="images/pink.jpg" alt="">
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PQoKl022-1658241798636)(mark-img/20210423155143395.gif)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>CSS3属性calc函数</title>
<style>
.father {
width: 500px;
height: 500px;
background-color: black;
}
.son {
/* width: 300px; */
/* width: calc(500px - 100px); */
width: calc(100% - 100px);
height: 200px;
background-color: salmon;
}
</style>
</head>
<body>
<!-- 需求:我们的子盒子宽度永远比父盒子小 100 像素 -->
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
2.4.1 CSS3滤镜 filter
filter CSS 属性将模糊或颜色偏移等图形效果应用于元素。
filter: 函数(); 例如:filter: blur(5px); blur 模糊处理,数值越大越模糊
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LuGX3yri-1658241798636)(mark-img/20210423161020807.png)]
2.4.2 CSS3 calc 函数
calc() 此 CSS 函数让你在声明 CSS 属性值时执行一些计算。
width: calc(100% - 80px);
括号里面可以使用 +
-
*
/
来进行计算。
2.5 CSS3过渡(重点)
2.5.1 过渡
过渡(transition)是 CSS3 中具有颠覆性的特征之一,我们可以在不使用 Flash 动画或 JavaScript 的情况下,当元素从一种样式变换为另一种样式时为元素添加效果。
过渡动画:是从一个状态渐渐的过渡到另外一个状态。
可以让我们页面更好看,更动感十足,虽然低版本浏览器不支持(IE9 以下版本) 但是不会影响页面布局。
我们现在经常和 :hover
一起搭配使用。
语法:
transition: 要过渡的属性 花费时间 运动曲线 何时开始;
- 属性:想要变化的 css 属性,宽度高度、背景颜色、内外边距都可以 。如果想要所有的属性都变化过渡,写一个
all
就可以 - 花费时间:单位是秒(必须写单位)比如
0.5s
- 运动曲线:默认是
ease
(可以省略) - 何时开始:单位是秒(必须写单位)可以设置延迟触发时间默认是
0s
(可以省略)
记住过渡的使用口诀:谁做过渡给谁加!
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>CSS3过渡效果</title>
<style>
div {
width: 200px;
height: 100px;
background-color: black;
/* transition: 变化的属性 花费时间 运动曲线 何时开始; */
/* transition: width .5s ease 0s, height .5s ease 1s; */
/* 如果想要写多个属性,利用逗号进行分割 */
/* transition: width .5s, height .5s; */
/* 如果想要多个属性都变化,属性写 all 就可以了 */
/* transition: height .5s ease 1s; */
/* 谁做过渡,给谁加 */
transition: all 0.5s;
}
div:hover {
width: 400px;
height: 200px;
background-color: gray;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-x1nVpSrT-1658241798637)(mark-img/20210423160022413.gif)]
2.5.2 进度条案例
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>CSS3过渡练习-进度条</title>
<style>
.bar {
width: 150px;
height: 15px;
border: 1px solid red;
border-radius: 7px;
padding: 1px;
}
.bar_in {
width: 50%;
height: 100%;
border-radius: 7px;
background-color: red;
/* 谁做过渡给谁加 */
transition: all .7s;
}
.bar:hover .bar_in {
width: 100%;
}
</style>
</head>
<body>
<div class="bar">
<div class="bar_in"></div>
</div>
</body>
</html>
2.4 CSS3其他特性(了解)
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>图片模糊处理filter</title>
<style>
img {
/* blur 是一个函数 小括号里面数值越大,图片越模糊 注意数值要加 px 单位 */
filter: blur(15px);
}
img:hover {
filter: blur(0);
}
</style>
</head>
<body>
<img src="images/pink.jpg" alt="">
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EtYz1RpY-1658241798637)(mark-img/20210423155143395.gif)]
三、边框(border)
border
可以设置元素的边框。
边框有三部分组成:边框宽度(粗细)
、边框样式
、边框颜色
。
语法:
border: border-width || border-style || border-color
属性 | 作用 |
---|---|
border-width | 定义边框粗细,单位是 px |
border-style | 边框的样式 |
border-color | 边框颜色 |
边框样式 border-style 可以设置如下值:
none
:没有边框,即忽略所有边框的宽度(默认值)solid
:边框为单实线(最为常用的)dashed
:边框为虚线dotted
:边框为点线
边框简写:
border: 1px solid red; /* 没有顺序 */
边框分开写法:
border-top: 1px solid red; /* 只设定上边框,其余同理 */
四、内边距(padding)
padding 属性用于设置内边距,即边框与内容之间的距离。
属性 | 作用 |
---|---|
padding-left | 左内边距 |
padding-rigth | 右内边距 |
padding-top | 上内边距 |
padding-bottom | 下内边距 |
padding 属性(简写属性)可以有一到四个值。
值的个数 | 表达意思 |
---|---|
padding: 5px; | 1 个值,代表上下左右都有 5 像素内边距 |
padding: 5px 10px; | 2 个值,代表上下内边距是 5 像素,左右内边距是 10 像素 |
padding: 5px 10px 20px; | 3 个值,代码上内边距 5 像素,左右内边距 10 像素,下内边距 20 像素 |
padding: 5px 10px 20px 30px; | 4 个值,上是 5 像素,右 10 像素,下 20 像素,左是 30 像素(顺时针) |
以上 4 种情况,我们实际开发都会遇到。
当我们给盒子指定 padding
值之后,发生了 2 件事情:
- 内容和边框有了距离,添加了内边距
padding
影响了盒子实际区域大小
也就是说,如果盒子已经有了宽度和高度,此时再指定内边距,会撑大盒子区域!
解决方案:
- 如果保证盒子跟效果图大小保持一致,则让 width、height 减去多出来的内边距大小即可
- 如果盒子本身没有指定 width、height 属性,则此时 padding 不会撑开盒子区域大小
五、外边距(margin)
margin
属性用于设置外边距,即控制盒子和盒子之间的距离。
属性 | 作用 |
---|---|
margin-left | 左外边距 |
margin-right | 右外边距 |
margin-top | 上外边距 |
margin-bottom | 下外边距 |
margin
简写方式代表的意义跟 padding
完全一致。
外边距典型应用:
外边距可以让块级盒子水平居中,但是必须满足两个条件:
- 盒子必须指定了宽度
width
- 盒子左右的外边距都设置为
auto
.header { width: 960px; margin: 0 auto;}
常见的写法,以下三种都可以:
margin-left: auto; margin-right: auto;
margin: auto;
margin: 0 auto;
注意:以上方法是让块级元素水平居中,行内元素或者行内块元素水平居中给其父元素添加 text-align: center
即可。
5.1 外边距合并
使用 margin
定义块元素的垂直外边距时,可能会出现外边距的合并。
注意:内边距没有合并一说!浮动的盒子不会发生外边距合并!
主要有两种情况:
- 相邻块元素垂直外边距的合并
- 嵌套块元素垂直外边距的塌陷
5.1.1 相邻块元素垂直外边距的合并
当上下相邻的两个块元素(兄弟关系)相遇时,如果上面的元素有下外边距 margin-bottom
,下面的元素有上外边距 margin-top
,则他们之间的垂直间距不是 margin-bottom
与 margin-top
之和。而是取两个值中的较大者,这种现象被称为相邻块元素垂直外边距的合并(准确的描述应该是:大的外边距覆盖小的)。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mUwoJCia-1658241798638)(mark-img/20210406134634404.jpg)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eALG0Gik-1658241798638)(mark-img/20210412112840415.jpg)]
解决方案:
尽量只给一个盒子添加 margin
值。
<!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>
.one {
width: 200px;
height: 200px;
background-color: hotpink;
margin-bottom: 100px;
}
.two {
width: 200px;
height: 200px;
background-color: skyblue;
margin-top: 100px;
}
</style>
</head>
<body>
<div class="one">one</div>
<div class="two">two</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DGotqOWE-1658241798639)(mark-img/20210410205508662.gif)]
5.1.2 嵌套块元素垂直外边距的塌陷
对于两个嵌套关系(父子关系)的块元素,当子元素有上外边距,此时父元素会塌陷较大的外边距值(外边距效果显示在父元素之上)。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fd1FqwTZ-1658241798639)(mark-img/image-20220116024700165.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CUH3wnKF-1658241798640)(mark-img/20210412112840510.jpg)]
解决方案:
- 可以为父元素定义上边框(比如:可以给一个透明 transparent 边框)
- 可以为父元素定义上内边距
- 可以为父元素添加
overflow: hidden
还有其他方法,比如浮动、固定,绝对定位的盒子不会有塌陷问题,后面咱们再总结。
案例:
<!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>
.father {
width: 400px;
height: 400px;
background-color: black;
margin-top: 50px;
}
.son {
width: 200px;
height: 200px;
background-color: pink;
margin-top: 100px;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hnDZwUZE-1658241798640)(mark-img/20210410205223833.gif)]
<!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>
.father {
width: 400px;
height: 400px;
background-color: black;
margin-top: 50px;
/* border: 1px solid transparent; */
overflow: hidden;
}
.son {
width: 200px;
height: 200px;
background-color: pink;
margin-top: 100px;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
注意:外边距的合并在利用盒子布局页面的时候是经常发生的!
/* 给个上边框 */
/* padding-top: 10px; */
/* 给个border 颜色为透明 */
/* border: 1px solid transparent; */
/* overflow:hidden就是溢出隐藏的意思,顾名思义,overflow:hidden最直白的作用就是当元素内的内容溢出的时候使它隐藏溢出的部分 */
overflow: hidden;
二、盒子阴影
CSS 3 新增了盒子阴影。
box-shadow 属性用于为盒子添加阴影。
语法:
box-shadow: h-shadow v-shadow blur spread color inset;
值 | 描述 |
---|---|
h-shadow | 必须。水平阴影的位置,允许负值。 |
v-shadow | 必须。垂直阴影的位置,允许负值。 |
blur | 可选。模糊距离(虚实程度)。 |
spread | 可选。阴影的尺寸(大小)。 |
color | 可选。阴影的颜色,请参阅 CSS 颜色值(阴影多为半透明颜色)。 |
inset | 可选。将外部阴影(outset)改为内部阴影(outset 不能指定,默认为空即可)。 |
div {
height: 300px;
width: 300px;
background-color: pink;
box-shadow: 300px 300px 10px 100px rgba(0, 0, 0, .3);
}
/* box-shadow: h-shadow v-shadow blur spread color inset; */
box-shadow: h-shadow v-shadow blur spread color inset;
第一个参数:h-shadow 控制阴影在X方向上移动的距离
第二个参数:v-shadow控制阴影在Y方向上移动的距离
第三个参数:blur控制阴影的模糊程度 值越大越模糊 反之则清晰
第四个参数:spread 控制阴影的大小 0就是和原模板大小一样 值是多少四各边分别就加多少
第五个参数:color颜色 控制阴影的颜色 常用rgba(0,0,0,.3)这样的写法 当然 也可以指定颜色 透明度会修改一定的颜色
第六个参数:可选。将外部阴影(outset)改为内部阴影(outset 不能指定,默认为空即可)。
- 盒子阴影不占用空间,不会影响其他盒子排列
三边阴影、双边阴影、单边阴影的设置方法:
<!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 {
width: 100px;
height: 100px;
background-color: #000;
margin: 25px auto;
color: white;
}
.a {
box-shadow: 0 0 25px 5px red;
}
/* 三边阴影就是直接把整个阴影部分往下边移 */
.b {
box-shadow: 0 6px 10px 0 red;
}
/* 两边阴影要用盒子嵌套来解决 */
.c1 {
box-shadow: 0 10px 10px -5px red;
}
.c2 {
width: 100%;
box-shadow: 0 -10px 10px -5px red;
}
/* 单边阴影就是直接把整个阴影部分往下边移,并且减小阴影大小 */
.d {
box-shadow: 0 10px 10px -5px red;
}
</style>
</head>
<body>
<div class="a">四边阴影</div>
<div class="b">三边阴影</div>
<div class="c1">
<div class="c2">两边阴影</div>
</div>
<div class="d">一边阴影</div>
</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>
.box {
/* height: 600px; */
background-color: pink;
margin: 0 auto;
/* 2.溢出隐藏方式清除浮动 */
/* overflow: hidden; */
}
/* 3.使用after伪元素清除浮动 */
/* IE6、7 专有 */
/* 其他浏览器可以不设置 */
/* .clearfix {
*zoom: 1;
} */
/* .clearfix:after {
display: block;
clear: both;
content: '';
height: 0;
visibility: hidden;
}
*/
/* 4.双伪元素清除浮动 */
.clearfix::before,
.clearfix::after {
content: '';
display: table;
}
.clearfix::after {
clear: both;
}
.damao {
width: 300px;
height: 300px;
background-color: red;
float: left;
}
.ermao {
width: 250px;
height: 250px;
background-color: deeppink;
float: left;
}
.sanm {
width: 350px;
height: 350px;
background-color: orange;
float: left;
}
.other {
width: 1920px;
height: 200px;
background-color: blue;
}
</style>
</head>
<body>
<div class="box clearfix">
<div class="damao">大毛</div>
<div class="ermao">二毛</div>
<div class="sanm">三毛</div>
<!-- 添加元素方式删除浮动 -->
<!-- <div style="clear:both"></div> -->
</div>
<div class="other"></div>
</body>
</html>
【CSS浮动】
本文档是个人对 Pink 老师课程的总结归纳及补充,转载请注明出处!
一、浮动(float)
1.1 传统网页布局的三种方式
网页布局的本质:用 CSS 来摆放盒子,把盒子摆放到相应位置。
CSS 提供了三种传统布局方式(简单说就是盒子如何进行排列)。
- 普通流(标准流)
- 浮动
- 定位
这里指的只是传统布局,其实还有一些特殊高级的布局方式。
1.2 标准流(普通流/文档流)
所谓的标准流:就是标签按照规定好的默认方式排列。
- 块级元素会独占一行,从上向下顺序排列。
- 行内元素会按照顺序,从左到右顺序排列,碰到父元素边缘则自动换行。
以上都是标准流布局,我们前面学习的就是标准流,标准流是最基本的布局方式。
这三种布局方式都是用来摆放盒子的,盒子摆放到合适位置,布局自然就完成了。
**注意:**实际开发中,一个页面基本都包含了这三种布局方式(后面移动端学习新的布局方式) 。
1.3 为什么需要浮动?
提问:我们用标准流能很方便的实现如下效果吗?
- 如何让多个块级盒子(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>行内块中间有缝隙</title>
<style>
div {
width: 150px;
height: 200px;
background-color: #d87093;
display: inline-block;
}
</style>
</head>
<body>
<div>1</div>
<div>2</div>
<div>3</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IR9mHjXF-1658241798641)(mark-img/20210409231914135.jpg)]
- 如何实现两个盒子的左右对齐?
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sF7Ct6bJ-1658241798641)(mark-img/20210409232236196.jpg)]
总结: 有很多的布局效果,标准流没有办法完成,此时就可以利用浮动完成布局。 因为浮动可以改变元素标签默认的排列方式。
浮动最典型的应用:可以让多个块级元素一行内排列显示。
网页布局第一准则:多个块级元素纵向排列找标准流,多个块级元素横向排列找浮动!
<!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 {
float: left;
width: 150px;
height: 200px;
background-color: #d87093;
}
</style>
</head>
<body>
<div>1</div>
<div>2</div>
<div>3</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wYMfPFgh-1658241798641)(mark-img/20210409232755502.jpg)]
**拓展:**浮动的盒子不会发生外边距合并!
1.4 什么是浮动?
float
属性用于创建浮动框,将其移动到一边,直到左边缘或右边缘触及包含块或另一个浮动框的边缘。
语法:
选择器 { float: 属性值;}
属性 | 描述 |
---|---|
none | 元素不浮动(默认值) |
left | 元素向左浮动 |
right | 元素向右浮动 |
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>什么是浮动</title>
<style>
.left,
.right {
float: left;
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div class="left">左青龙</div>
<div class="right">右白虎</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qs0ztUn3-1658241798642)(mark-img/20210410105021368.jpg)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>什么是浮动</title>
<style>
.left,
.right {
float: left;
width: 200px;
height: 200px;
background-color: pink;
}
/* 层叠性 */
.right {
float: right;
}
</style>
</head>
<body>
<div class="left">左青龙</div>
<div class="right">右白虎</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-h1BM01FO-1658241798642)(mark-img/20210410104859778.jpg)]
1.5 浮动特性(重难点)
加了浮动之后的元素,会具有很多特性,需要我们掌握。
- 浮动元素会脱离标准流(脱标)
- 浮动的元素会一行内显示并且元素顶部对齐
- 浮动的元素会具有行内块元素的特性
下面分别解释:
(1)浮动元素会脱离标准流(脱标)
- 脱离标准普通流的控制(浮) 移动到指定位置(动),(俗称脱标)
- 浮动的盒子不再保留原先的位置
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-891MHjGn-1658241798642)(mark-img/20210410110624702.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>浮动特性1</title>
<style>
/* 设置了浮动(float)的元素会:
1.脱离标准普通流的控制(浮)移动到指定位置(动)。
2.浮动的盒子不再保留原先的位置 */
.box1 {
float: left;
width: 200px;
height: 200px;
background-color: pink;
}
.box2 {
width: 300px;
height: 300px;
background-color: gray;
}
</style>
</head>
<body>
<div class="box1">浮动的盒子</div>
<div class="box2">标准流的盒子</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nFozKjZf-1658241798643)(mark-img/20210410110837237.jpg)]
(2)浮动的元素会一行内显示并且元素顶部对齐
- 如果多个盒子都设置了浮动,则它们会按照属性值一行内显示并且顶端对齐排列。
- 浮动的元素是互相贴靠在一起的(不会有缝隙),如果父级宽度装不下这些浮动的盒子,多出的盒子会另起一行对齐。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>浮动元素特性-浮动元素一行显示</title>
<style>
div {
float: left;
width: 200px;
height: 200px;
background-color: pink;
}
.two {
background-color: skyblue;
height: 249px;
}
.four {
background-color: skyblue;
}
</style>
</head>
<body>
<div>1</div>
<div class="two">2</div>
<div>3</div>
<div class="four">4</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0LeILWWh-1658241798643)(mark-img/20210410111331406.jpg)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lkRZGU0n-1658241798643)(mark-img/20210410111551264.gif)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hqdcGLuE-1658241798644)(mark-img/readme.gif)]
(3)浮动的元素会具有行内块元素的特性
任何元素都可以浮动。不管原先是什么模式的元素,添加浮动之后具有行内块元素相似的特性。
- 块级盒子:没有设置宽度时默认宽度和父级一样宽,但是添加浮动后,它的大小根据内容来决定
- 行内盒子:宽度默认和内容一样宽,直接设置高宽无效,但是添加浮动后,它的大小可以直接设置
- 浮动的盒子中间是没有缝隙的,是紧挨着一起的
- 即:默认宽度由内容决定,同时支持指定高宽,盒子之间无空隙
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>浮动的元素具有行内块元素特点</title>
<style>
/* 任何元素都可以浮动。不管原先是什么模式的元素,添加浮动之后具有行内块元素相似的特性。 */
span,
div {
float: left;
width: 200px;
height: 100px;
background-color: pink;
}
/* 如果行内元素有了浮动,则不需要转换块级\行内块元素就可以直接给高度和宽度 */
p {
float: right;
height: 200px;
background-color: skyblue;
}
</style>
</head>
<body>
<span>span1</span>
<span>span2</span>
<div>div</div>
<p>pppppppppppppp</p>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YLPhfJvj-1658241798644)(mark-img/20210410120453590.jpg)]
注意:之所以顶部没有对齐,原因是 p 标签自带的外边距 > span div 自带的外边距。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>浮动的元素具有行内块元素特点</title>
<style>
* {
margin: 0px;
}
/* 任何元素都可以浮动。不管原先是什么模式的元素,添加浮动之后具有行内块元素相似的特性。 */
span,
div {
float: left;
width: 200px;
height: 100px;
background-color: pink;
}
/* 如果行内元素有了浮动,则不需要转换块级\行内块元素就可以直接给高度和宽度 */
p {
float: right;
height: 200px;
background-color: skyblue;
}
</style>
</head>
<body>
<span>span1</span>
<span>span2</span>
<div>div</div>
<p>pppppppppppppp</p>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VEw2uHTC-1658241798644)(mark-img/20210410120744356.jpg)]
1.6 浮动元素经常和标准流父级搭配使用
为了约束浮动元素位置,我们网页布局一般采取的策略是:
先用标准流的父元素排列上下位置,之后内部子元素采取浮动排列左右位置。符合网页布局第一准侧。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kSUnuEUz-1658241798644)(mark-img/20210410121200199.jpg)]
应用举例:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pRI2D37b-1658241798645)(mark-img/20210410121702801.jpg)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>浮动元素搭配标准流父盒子1</title>
<style>
.box {
width: 1200px;
height: 460px;
background-color: black;
margin: 0 auto;
}
.left {
float: left;
width: 230px;
height: 460px;
background-color: pink;
}
.right {
float: left;
width: 970px;
height: 460px;
background-color: skyblue;
}
</style>
</head>
<body>
<div class="box">
<div class="left">左侧</div>
<div class="right">右侧</div>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uge6nqyX-1658241798645)(mark-img/20210410121929718.jpg)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QUZtWpqO-1658241798645)(mark-img/20210410123510406.jpg)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>浮动元素搭配标准流父盒子2</title>
<style>
* {
margin: 0;
padding: 0;
}
/* 取消 li 前的圆点 */
li {
list-style: none;
}
.box {
width: 1226px;
height: 285px;
background-color: pink;
/* 让大盒子水平居中 */
margin: 0 auto;
}
.box li {
width: 296px;
height: 285px;
background-color: gray;
float: left;
/* 每个小盒子用右边距隔开 */
margin-right: 14px;
}
/* 取消最后一个小盒子的右外边距 */
/* 这里必须写 .box .last 要注意权重的问题 20 */
.box .last {
margin-right: 0;
}
</style>
</head>
<body>
<ul class="box">
<li>1</li>
<li>2</li>
<li>3</li>
<li class="last">4</li>
</ul>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WoV4LkiY-1658241798646)(mark-img/20210410122632628.jpg)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-x1xUXzvO-1658241798647)(mark-img/20210410123520625.jpg)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>浮动布局练习3</title>
<style>
.box {
width: 1226px;
height: 615px;
background-color: pink;
margin: 0 auto;
}
.left {
float: left;
width: 234px;
height: 615px;
background-color: gray;
}
.right {
float: left;
width: 992px;
height: 615px;
background-color: skyblue;
}
.right>div {
float: left;
width: 234px;
height: 300px;
background-color: pink;
margin-left: 14px;
margin-bottom: 14px;
}
</style>
</head>
<body>
<div class="box">
<div class="left">左青龙</div>
<div class="right">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
</div>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9vXUEiDs-1658241798647)(mark-img/20210410122643298.jpg)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DX6S8rQk-1658241798647)(mark-img/image-20220116104204568.png)]
二、常见网页布局
2.1 初识常见网页布局
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cpKoMo20-1658241798648)(mark-img/20210410125437174.jpg)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UiXaH5Dq-1658241798648)(mark-img/20210410125449278.jpg)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>常见网页布局</title>
<style>
* {
margin: 0;
padding: 0;
}
li {
list-style: none;
}
.top {
height: 50px;
background-color: gray;
}
.banner {
width: 980px;
height: 150px;
background-color: gray;
margin: 10px auto;
}
.box {
width: 980px;
margin: 0 auto;
height: 300px;
background-color: pink;
}
.box li {
float: left;
width: 237px;
height: 300px;
background-color: gray;
margin-right: 10px;
}
.box .last {
margin-right: 0;
}
/* 只要是通栏的盒子(和浏览器一样宽)不需要指定宽度 */
.footer {
height: 200px;
background-color: gray;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="top">top</div>
<div class="banner">banner</div>
<div class="box">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li class="last">4</li>
</ul>
</div>
<div class="footer">footer</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OOka4t7A-1658241798649)(mark-img/20210410125651546.jpg)]
2.2 浮动布局注意点
(1)浮动和标准流的父盒子搭配
先用标准流的父元素排列上下位置,之后内部子元素采取浮动排列左右位置。
(2)一个元素浮动了,理论上其余的兄弟元素也要浮动
一个盒子里面有多个子盒子,如果其中一个盒子浮动了,那么其他兄弟也应该浮动,以防止引起问题。
浮动的盒子只会影响浮动盒子后面的标准流,不会影响前面的标准流。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>浮动注意点</title>
<style>
/* 如果一个子元素浮动了,尽量其他盒子也浮动,这样保证这些子元素一行显示 */
.box {
width: 900px;
height: 300px;
background-color: black;
margin: 0 auto;
}
.damao {
float: left;
width: 200px;
height: 200px;
background-color: palevioletred;
}
.ermao {
float: left;
width: 200px;
height: 150px;
background-color: palegreen;
}
.sanmao {
float: left;
width: 300px;
height: 240px;
background-color: skyblue;
}
</style>
</head>
<body>
<div class="box">
<div class="damao">大毛</div> <!-- float: left; -->
<div class="ermao">二毛</div> <!-- float: left; -->
<div class="sanmao">三毛</div> <!-- float: left; -->
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KlcJ60kF-1658241798649)(mark-img/20210410130344660.jpg)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>浮动注意点</title>
<style>
/* 浮动的盒子只会影响浮动盒子后面的标准流,不会影响前面的标准流。 */
/* 大毛为标准流,所以大毛会占据其所在的一行,后面的二毛就算浮动也不会跑到大毛上方!*/
.box {
width: 900px;
height: 300px;
background-color: black;
margin: 0 auto;
}
.damao {
/* float: left; */
width: 200px;
height: 200px;
background-color: palevioletred;
}
.ermao {
float: left;
width: 200px;
height: 150px;
background-color: palegreen;
}
.sanmao {
float: left;
width: 300px;
height: 240px;
background-color: skyblue;
}
</style>
</head>
<body>
<div class="box">
<div class="damao">大毛</div> <!-- 标准流 -->
<div class="ermao">二毛</div> <!-- float: left; -->
<div class="sanmao">三毛</div> <!-- float: left; -->
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tmsS7zrV-1658241798649)(mark-img/20210410130529987.jpg)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>浮动注意点</title>
<style>
/* 浮动的盒子只会影响浮动盒子后面的标准流,不会影响前面的标准流。 */
.box {
width: 900px;
height: 300px;
background-color: black;
margin: 0 auto;
}
.damao {
float: left;
width: 200px;
height: 200px;
background-color: palevioletred;
}
.ermao {
/* float: left; */
width: 200px;
height: 150px;
background-color: palegreen;
}
.sanmao {
float: left;
width: 300px;
height: 240px;
background-color: skyblue;
}
</style>
</head>
<body>
<div class="box">
<div class="damao">大毛</div> <!-- float: left; -->
<div class="ermao">二毛</div> <!-- 标准流 -->
<div class="sanmao">三毛</div> <!-- float: left; -->
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jlJIIL6n-1658241798650)(mark-img/20210410130858443.jpg)]
由于大毛是浮动的,所以原来大毛的位置会空出来,此时二毛就会向上补齐空位,由于二毛高度小于大毛,所以二毛被大毛挡住了,又因为二毛是标准流,所以二毛会占据所在的一行,所以后面浮动的三毛就只能在二毛的底部之下,又由于大毛也是浮动的,所以三毛就会紧贴在大毛右侧。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-E1rggz5Z-1658241798650)(mark-img/20210410131113979.jpg)]
三、清除浮动
3.1 思考题
我们前面浮动元素有一个标准流的父元素,他们有一个共同的特点,都是有高度的。
但是,所有的父盒子都必须有高度吗?
答案:不一定!比如,一个产品列表,随着时期的不同,产品数量也不同,所需的盒子大小也会随之改变,那么直接固定盒子高度的形式显然就是不行的。再比如,文章之类的盒子,不同的文章字数是不相同的,那么显然盒子也不能直接固定高度。
理想中的状态,让子盒子撑开父亲。有多少孩子,我父盒子就有多高。
但是不给父盒子高度会有问题吗?
答案:会!但有方法解决(清除浮动)。
3.2 为什么需要清除浮动?
由于父级盒子很多情况下不方便给高度,但是子盒子浮动又不占有位置,最后父级盒子高度为 0 时,就会影响下面的标准流盒子。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-P63mrcuF-1658241798650)(mark-img/20210410133601570.png)]
-
由于浮动元素不再占用原文档流的位置,所以它会对后面的元素排版产生影响
-
此时一但父盒子下面有其他盒子,那么布局就会发生严重混乱!
3.3 清除浮动本质
- 清除浮动的本质是清除浮动元素造成的影响
- 如果父盒子本身有高度,则不需要清除浮动
- 清除浮动之后,父级就会根据浮动的子盒子自动检测高度。父级有了高度,就不会影响下面的标准流了
3.4 清除浮动
语法:
选择器 { clear: 属性值; }
属性值 | 描述 |
---|---|
left | 不允许左侧有浮动元素(清除左侧浮动的影响) |
right | 不允许右侧有浮动元素(清除右侧浮动的影响) |
both | 同时清除左右两侧浮动的影响 |
我们实际工作中,几乎只用 clear: both;
清除浮动的策略是:闭合浮动。
3.5 清除浮动方法
- 额外标签法也称为隔墙法,是 W3C 推荐的做法。(实际开发不推荐)
- 父级添加 overflow 属性
- 父级添加 after 伪元素
- 父级添加 双伪元素
3.6 清除浮动 —— 额外标签法
额外标签法也称为隔墙法,是 W3C 推荐的做法。
额外标签法会在浮动元素末尾添加一个空的标签。例如 <div style="clear: both"></div>
,或者其他标签(如 <br>
等)。
- 优点: 通俗易懂,书写方便
- 缺点: 添加许多无意义的标签,结构化较差
注意: 要求这个新的空标签必须是块级元素。
总结:
- 清除浮动本质是?
清除浮动的本质是清除浮动元素脱离标准流造成的影响。
- 清除浮动策略是?
闭合浮动。只让浮动在父盒子内部影响,不影响父盒子外面的其他盒子。
- 额外标签法?
隔墙法,就是在最后一个浮动的子元素后面添加一个额外空标签(块级标签),添加清除浮动样式。
实际工作可能会遇到,但是不常用。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>清除浮动之额外标签法</title>
<style>
.box {
width: 800px;
border: 3px solid black;
margin: 0 auto;
}
.damao {
float: left;
width: 300px;
height: 200px;
background-color: salmon;
}
.ermao {
float: left;
width: 200px;
height: 200px;
background-color: skyblue;
}
.footer {
height: 200px;
background-color: gray;
}
.clear {
clear: both;
}
</style>
</head>
<body>
<div class="box">
<div class="damao">大毛</div>
<div class="ermao">二毛</div>
<div class="ermao">二毛</div>
<div class="ermao">二毛</div>
<div class="ermao">二毛</div>
<div class="clear"></div>
<!-- 这个新增的盒子要求必须是块级元素不能是行内元素 -->
<!-- <span class="clear"></span> -->
</div>
<div class="footer"></div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VZnwniSy-1658241798651)(mark-img/20210410140435474.jpg)]
3.7 清除浮动 —— 父级添加 overflow
可以给父级添加 overflow
属性,将其属性值设置为 hidden
、 auto
或 scroll
。
子不教,父之过,注意是给父元素添加代码。
- 优点:代码简洁
- 缺点:无法显示溢出的部分
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>overflow清除浮动</title>
<style>
.box {
/* 清除浮动 */
overflow: hidden;
width: 800px;
border: 1px solid blue;
margin: 0 auto;
}
.damao {
float: left;
width: 300px;
height: 200px;
background-color: purple;
}
.ermao {
float: left;
width: 200px;
height: 200px;
background-color: pink;
}
.footer {
height: 200px;
background-color: black;
}
</style>
</head>
<body>
<div class="box">
<div class="damao">大毛</div>
<div class="ermao">二毛</div>
</div>
<div class="footer"></div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TCdMWrav-1658241798651)(mark-img/20210410141717785.jpg)]
3.8 清除浮动 —— :after 伪元素法
:after
方式是额外标签法的升级版,也是给父元素添加代码。
原理:自动在父盒子里的末尾添加一个 行内盒子,我们将它转换为 块级盒子,就间接实现了额外标签法。
.clearfix:after {
content: "";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.clearfix {
/* IE6、7 专有 */
*zoom: 1;
}
注意:类名不一定非要是 clearfix,但是还是推荐这么写以提高可读性。
- 优点:没有增加标签,结构更简单
- 缺点:需要单独照顾低版本浏览器
- 代表网站: 百度、淘宝网、网易等
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>伪元素清除浮动</title>
<style>
.clearfix:after {
content: "";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.clearfix {
/* IE6、7 专有 */
*zoom: 1;
}
.box {
width: 800px;
border: 1px solid blue;
margin: 0 auto;
}
.damao {
float: left;
width: 300px;
height: 200px;
background-color: purple;
}
.ermao {
float: left;
width: 200px;
height: 200px;
background-color: pink;
}
.footer {
height: 200px;
background-color: black;
}
</style>
</head>
<body>
<div class="box clearfix">
<div class="damao">大毛</div>
<div class="ermao">二毛</div>
</div>
<div class="footer"></div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NtPhu9Dj-1658241798651)(mark-img/20210410141717785-164230099450328.jpg)]
3.9 清除浮动 —— 双伪元素清除浮动
额外标签法的升级版,也是给给父元素添加代码。
原理:自动在父盒子里的两端添加两个行内盒子,并把它们转换为 表格,间接实现了额外标签法。
.clearfix:before,
.clearfix:after {
content: "";
display: table;
}
.clearfix:after {
clear: both;
}
.clearfix {
/* IE6、7 专有 */
*zoom:1;
}
注意:类名不一定非要是 clearfix,但是还是推荐这么写以提高可读性。
-
优点:代码更简洁
-
缺点:需要单独照顾低版本浏览器
-
代表网站:小米、腾讯等
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>双伪元素清除浮动</title>
<style>
.clearfix:before,
.clearfix:after {
content: "";
display: table;
}
.clearfix:after {
clear: both;
}
.clearfix {
*zoom: 1;
}
.box {
width: 800px;
border: 1px solid blue;
margin: 0 auto;
}
.damao {
float: left;
width: 300px;
height: 200px;
background-color: purple;
}
.ermao {
float: left;
width: 200px;
height: 200px;
background-color: pink;
}
.footer {
height: 200px;
background-color: black;
}
</style>
</head>
<body>
<div class="box clearfix">
<div class="damao">大毛</div>
<div class="ermao">二毛</div>
</div>
<div class="footer"></div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bHO9kMcY-1658241798652)(mark-img/20210410141717785-164230099808930.jpg)]
3.10 清除浮动总结
为什么需要清除浮动?
- 父级没高度
- 子盒子浮动了
- 影响下面布局了,我们就应该清除浮动了
清除浮动的方式 | 优点 | 缺点 |
---|---|---|
额外标签法(隔墙法) | 通俗易懂,书写方便 | 添加许多无意义的标签,结构化较差 |
父级 overflow: hidden; | 书写简单 | 溢出隐藏 |
父级 after 伪元素 | 结构语义化正确 | 由于 IE6~7 不支持 :after,兼容性问题 |
父级双伪元素 | 结构语义化正确 | 由于 IE6~7 不支持 :after,兼容性问题 |
after 伪元素、双伪元素 清除浮动的原理将在后面的 CSS3 中解释。
【CSS定位】
本文档是个人对 Pink 老师课程的总结归纳及补充,转载请注明出处!
一、定位
1.1 为什么需要定位?
提问: 以下情况使用标准流或者浮动能实现吗?
-
某个元素可以自由的在一个盒子内移动位置,并且压住其他盒子。
-
当我们滚动窗口的时候,盒子是固定屏幕某个位置的。
以上效果,标准流或浮动都无法快速实现,此时需要定位来实现。
所以:
- 浮动可以让多个块级盒子一行没有缝隙排列显示, 经常用于横向排列盒子。
- 定位则是可以让盒子自由的在某个盒子内移动位置或者固定屏幕中某个位置,并且可以压住其他盒子。
1.2 定位组成
定位:将盒子定在某一个位置,所以定位也是在摆放盒子, 按照定位的方式移动盒子。
定位 = 定位模式 + 边偏移
- 定位模式用于指定一个元素在文档中的定位方式
- 边偏移则决定了该元素的最终位置
(1)定位模式
定位模式决定元素的定位方式,它通过 CSS 的 position
属性来设置,其值可以分为四个。
值 | 语义 |
---|---|
static | 静态定位 |
relative | 相对定位 |
absolute | 绝对定位 |
fixed | 固定定位 |
(2)边偏移
边偏移就是定位的盒子移动的最终位置。有 top
、bottom
、left
和 right
4 个属性。
注意:可以为负值。
边偏移属性 | 实例 | 描述 |
---|---|---|
top | top: 80px | 顶端偏移量,定义元素相对于其父元素上边线的距离 |
bottom | bottom: 80px | 底部偏移量,定义元素相对于其父元素下边线的距离 |
left | left: 80px | 左侧偏移量,定义元素相对于其父元素左边线的距离 |
rigth | right: 80px | 右侧偏移量,定义元素相对于其父元素右边线的距离 |
1.3 静态定位 static(了解)
静态定位是元素的默认定位方式,无定位的意思。
语法:
选择器 { position: static; }
-
静态定位按照标准流特性摆放位置,它没有边偏移
-
静态定位在布局时很少用到
1.4 相对定位 relative(重要)
相对定位是元素在移动位置的时候相对于它原来的位置来说的定位(自恋型)。
语法:
选择器 { position: relative; }
相对定位的特点:(务必记住)
- 它是相对于自己原来的位置来移动的(移动位置的时候参照点是自己原来的位置点)
- 原来在标准流的位置继续占有,后面的盒子仍然以标准流的方式对待它
因此,相对定位并没有脱标。它最典型的应用是给绝对定位当爹的。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>相对定位</title>
<style>
.box1 {
position: relative;
top: 100px;
left: 100px;
width: 200px;
height: 200px;
background-color: pink;
}
.box2 {
width: 200px;
height: 200px;
background-color: deeppink;
}
</style>
</head>
<body>
<div class="box1">
</div>
<div class="box2">
</div>
</body>
</html>
1.5 绝对定位 absolute(重要)
绝对定位是元素在移动位置的时候相对于它祖先元素来说的定位(拼爹型)。
语法:
选择器 { position: absolute; }
绝对定位的特点:(务必记住)
- 如果没有祖先元素或者祖先元素没有定位,则以浏览器为准定位(Document 文档)
- 如果祖先元素有定位(相对、绝对、固定定位),则以最近一级的有定位祖先元素为参考点移动位置
- 绝对定位不再占有原先的位置(脱标),并且脱标的程度大于浮动(会压住浮动)
所以绝对定位是脱离标准流的。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>绝对定位-无父亲或者父亲无定位</title>
<style>
.father {
width: 500px;
height: 500px;
background-color: skyblue;
}
.son {
position: absolute;
/* top: 10px;
left: 10px; */
/* top: 100px;
right: 200px; */
left: 0;
bottom: 0;
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>绝对定位-父级有定位-一级父亲</title>
<style>
.father {
position: relative;
width: 500px;
height: 500px;
background-color: skyblue;
}
.son {
position: absolute;
/* top: 10px;
left: 10px; */
/* top: 100px;
right: 200px; */
left: 0;
bottom: 0;
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>绝对定位-父级有定位-多级父亲</title>
<style>
/* 以最近一级的有定位祖先元素为参考点移动位置 */
/* 即:谁带有定位并且离我最近,我就以谁为准! */
.yeye {
position: relative;
width: 800px;
height: 800px;
background-color: hotpink;
padding: 50px;
}
.father {
width: 500px;
height: 500px;
background-color: skyblue;
}
.son {
position: absolute;
left: 30px;
bottom: 10px;
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div class="yeye">
<div class="father">
<div class="son"></div>
</div>
</div>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>绝对定位-脱标</title>
<style>
.father {
position: relative;
width: 500px;
height: 500px;
background-color: skyblue;
}
.son {
position: absolute;
left: 0;
bottom: 0;
width: 200px;
height: 200px;
background-color: pink;
}
.son2 {
width: 200px;
height: 200px;
background-color: gray;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
<div class="son2"></div>
</div>
</body>
</html>
问题:
- 绝对定位和相对定位到底有什么使用场景呢?
- 为什么说相对定位给绝对定位当爹的呢?
1.6 子绝父相的由来
弄清楚这个口诀,就明白了绝对定位和相对定位的使用场景。
这个 “子绝父相” 太重要了,是我们学习定位的口诀,是定位中最常用的一种方式这句话的意思是:子级是绝对定位的话,父级要用相对定位。
- 子级绝对定位,不会占有位置,可以放到父盒子里面的任何一个地方,不会影响其他的兄弟盒子
- 父盒子需要加定位限制子盒子在父盒子内显示
- 父盒子布局时,需要占有位置,因此父亲只能是相对定位
这就是子绝父相的由来,所以相对定位经常用来作为绝对定位的父级。
总结: 因为父级需要占有位置,因此是相对定位, 子盒子不需要占有位置,则是绝对定位。
当然,子绝父相不是永远不变的,如果父元素不需要占有位置,“子绝父绝” 也会遇到。
思考:为什么非要用定位?浮动不可以吗?
答案:用浮动做某些布局远远没有定位简单和方便!例如,轮播图。
- 左右两边的图片切换按钮,利用浮动也可以做。但是,假如放置图片的盒子是在切换按钮之前添加的,那么根据浮动元素只能影响后面盒子的特性,切换按钮就只可能在图片底部之下,不可能浮于图片之上!
- 就算切换按钮用浮动实现了,但是左下角的轮播序号点图如果也用浮动实现,结果就是轮播序号点图会与切换按钮在一行并排浮动!
可见,浮动单纯用于左右排列盒子是非常适合的,但是用于空间层次上排列盒子就不适合了!应该用定位实现。
重点:竖向上布局找标准流,横向上布局找浮动,空间上布局找定位!
【案例:学成在线 hot new 模块】
<div class="box-bd">
<ul class="clearfix">
<li>
<!--
<em> 不是单纯的倾斜文本,该标签本质上是告诉浏览器把其中的文本表示为强调的内容,
所以,<em> 可以用来包含强调的元素。
-->
<em>
<img src="images/hot.png" alt="">
</em>
<img src="images/pic.png" alt="">
<h4>
Think PHP 5.0 博客系统实战项目演练
</h4>
<div class="info">
<span>高级</span> • 1125人在学习
</div>
</li>
...
</ul>
</div>
.box-bd ul {
width: 1225px;
}
.box-bd ul li {
/* 子绝父相 */
position: relative;
float: left;
width: 228px;
height: 270px;
background-color: #fff;
margin-right: 15px;
margin-bottom: 15px;
}
.box-bd ul li > img {
width: 100%;
}
.box-bd ul li h4 {
margin: 20px 20px 20px 25px;
font-size: 14px;
color: #050505;
font-weight: 400;
}
.box-bd ul li em {
/* 子绝父相 */
position: absolute;
top: 4px;
right: -4px;
}
.box-bd .info {
margin: 0 20px 0 25px;
font-size: 12px;
color: #999;
}
.box-bd .info span {
color: #ff7c2d;
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6u8DSLay-1658241798652)(mark-img/20210411001207402.jpg)]
1.7 固定定位 fixed (重要)
固定定位是元素固定于浏览器可视区的位置。
主要使用场景: 可以在浏览器页面滚动时元素的位置不会改变。
语法:
选择器 { position: fixed; }
固定定位的特点(务必记住):
- 以浏览器的可视窗口为参照点移动元素
- 跟父元素没有任何关系
- 不随滚动条滚动
- 固定定位不再占有原先的位置
- 固定定位也是脱标的,其实固定定位也可以看做是一种特殊的绝对定位。
应用举例:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>固定定位</title>
<style>
.dj {
position: fixed;
top: 100px;
left: 200px;
}
</style>
</head>
<body>
<div class="dj">
<img src="images/pvp.png" alt="">
</div>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
<p>请尽情吩咐妲己,主人</p>
</body>
</html>
1.8 固定定位小技巧:固定在版心右侧位置
小算法:
- 让固定定位的盒子
left: 50%
,走到浏览器可视区(也可以看做版心) 一半的位置 - 让固定定位的盒子
margin-left: 版心宽度的一半距离
,多走版心宽度的一半位置
就可以让固定定位的盒子贴着版心右侧对齐了。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>固定定位小技巧-固定到版心右侧</title>
<style>
.w {
width: 800px;
height: 1400px;
background-color: pink;
margin: 0 auto;
}
.fixed {
position: fixed;
/* 1. 走浏览器宽度的一半 */
left: 50%;
/* 2. 利用 margin 走版心盒子宽度的一半距离(为了美观多加了 5px)*/
margin-left: 405px;
width: 50px;
height: 150px;
background-color: skyblue;
}
</style>
</head>
<body>
<div class="fixed"></div>
<div class="w">版心盒子 800像素</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cseGqqZA-1658241798653)(mark-img/20210411002750577.gif)]
1.9 粘性定位 sticky(了解)
粘性定位可以被认为是相对定位和固定定位的混合。
Sticky 粘性的。
语法:
选择器 { position: sticky; top: 10px; }
粘性定位的特点:
- 以浏览器的可视窗口为参照点移动元素(固定定位特点)
- 粘性定位占有原先的位置(相对定位特点)
- 必须添加 top 、left、right、bottom 其中一个才有效
跟页面滚动搭配使用。 兼容性较差,IE 不支持。
未来开发的趋势,但目前并不常用(目前用 javascript 来实现粘性定位效果)。
应用举例:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>粘性定位</title>
<style>
body {
height: 3000px;
}
.nav {
/* 粘性定位 */
position: sticky;
top: 0;
width: 800px;
height: 50px;
background-color: pink;
margin: 100px auto;
}
</style>
</head>
<body>
<div class="nav">我是导航栏</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zlrJ3JZV-1658241798653)(mark-img/20210411003401335.gif)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7pvWCw6M-1658241798653)(mark-img/a6b709f41c9f42558b8d6870202bc171.gif)]
1.10 定位的总结
定位模式 | 是否脱标 | 移动位置 | 是否常用 |
---|---|---|---|
static 静态定位 | 否 | 不能使用边偏移 | 很少 |
relative 相对定位 | 否(占有位置) | 相对于自身位置移动 | 常用 |
absolute 绝对定位 | 是(不占有位置) | 带有定位的父级 | 常用 |
fixed 固定定位 | 是(不占有位置) | 浏览器可视区 | 常用 |
sticky 粘性定位 | 否(占有位置) | 浏览器可视区 | 当前阶段少 |
- 一定记住,相对定位、固定定位、绝对定位 两个大的特点: 1. 是否占有位置(脱标否) 2. 以谁为基准点移动位置。
- 学习定位重点学会子绝父相。
1.11 定位叠放次序 z-index
在使用定位布局时,可能会出现盒子重叠的情况。此时,可以使用 z-index 来控制盒子的前后次序(z轴)。
语法:
选择器 { z-index: 1; }
- 数值可以是正整数、负整数或 0,默认是 auto,数值越大,盒子越靠上
- 如果属性值相同,则按照书写顺序,后来居上
- 数字后面不能加单位
- 只有定位的盒子才有 z-index 属性
1.12 定位的拓展
(1)绝对定位的盒子居中
加了绝对定位的盒子不能通过 margin: 0 auto
水平居中,但是可以通过以下计算方法实现水平和垂直居中。
left: 50%;
:让盒子的左侧移动到父级元素的水平中心位置。margin-left: -0.5widthpx;
:让盒子向左移动自身宽度的一半。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>绝对定位水平垂直居中</title>
<style>
.box {
position: absolute;
/* 1. left 走 50% 父容器宽度的一半 */
left: 50%;
/* 2. margin 负值 往左边走 自己盒子宽度的一半 */
margin-left: -100px;
/* 垂直居中同理 */
top: 50%;
margin-top: -100px;
width: 200px;
height: 200px;
background-color: pink;
/* margin: auto; */
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QWfAAWcK-1658241798654)(mark-img/20210411004813982.jpg)]
(2)定位特殊特性
绝对定位和固定定位也和浮动类似。
- 行内元素添加绝对或者固定定位,可以直接设置高度和宽度。
- 块级元素添加绝对或者固定定位,如果不给宽度或者高度,默认大小是内容的大小。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>定位的特殊特性</title>
<style>
span {
position: absolute;
top: 100px;
width: 200px;
height: 150px;
background-color: pink;
}
div {
position: absolute;
background-color: skyblue;
}
</style>
</head>
<body>
<span>123</span>
<div>abcd</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ldov7mFy-1658241798654)(mark-img/20210411005338704.jpg)]
(3)脱标的盒子不会触发外边距塌陷
浮动元素、绝对定位(固定定位)元素的都不会触发外边距合并的问题。
(4)绝对定位(固定定位)会完全压住盒子
浮动元素不同,只会压住它下面标准流的盒子,但是不会压住下面标准流盒子里面的文字(图片)。
但是绝对定位(固定定位) 会压住下面标准流所有的内容。
浮动之所以不会压住文字,因为浮动产生的目的最初是为了做文字环绕效果的。 文字会围绕浮动元素。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>浮动产生原来的目的是做文字环绕效果</title>
<style>
img {
float: left;
}
</style>
</head>
<body>
1993年,在古装片《战神传说》中扮演一个武功超群的渔民;同年,主演动作喜剧片《至尊三十六计之偷天换日》,在片中饰演赌术高明的千门高手钱文迪;此外,他还主演了爱情片《天长地久》,在片中塑造了一个风流不羁的江湖浪子形象。
1994年,刘德华投资并主演了剧情片《天与地》,在片中饰演面对恶势力却毫不退缩的禁毒专员张一鹏。1995年,主演赛车励志片《烈火战车》,在片中饰演叛逆、倔强的阿祖,并凭借该片获得第15届香港电影金像奖最佳男主角提名;同年在动作片《大冒险家》中演绎了立仁从小时候父母双亡到长大后进入泰国空军的故事。
1996年,主演黑帮题材的电影《新上海滩》,在片中饰演对冯程程痴情一片的丁力。1997年,担任剧情片《香港制造》的制作人;同年,主演爱情片《天若有情之烽火佳人》,在片中饰演家世显赫的空军少尉刘天伟;12月,与梁家辉联袂主演警匪动作片《黑金》,在片中饰演精明干练、嫉恶如仇的调查局机动组组长方国辉。1998年,主演动作片《龙在江湖》
<img src="images/img.jpg" alt="">
,饰演重义气的黑帮成员韦吉祥;同年,出演喜剧片《赌侠1999》;此外,他还担任剧情片《去年烟花特别多》的制作人。
1993年,在古装片《战神传说》中扮演一个武功超群的渔民;同年,主演动作喜剧片《至尊三十六计之偷天换日》,在片中饰演赌术高明的千门高手钱文迪;此外,他还主演了爱情片《天长地久》,在片中塑造了一个风流不羁的江湖浪子形象。
1994年,刘德华投资并主演了剧情片《天与地》,在片中饰演面对恶势力却毫不退缩的禁毒专员张一鹏。1995年,主演赛车励志片《烈火战车》,在片中饰演叛逆、倔强的阿祖,并凭借该片获得第15届香港电影金像奖最佳男主角提名;同年在动作片《大冒险家》中演绎了立仁从小时候父母双亡到长大后进入泰国空军的故事。
1996年,主演黑帮题材的电影《新上海滩》,在片中饰演对冯程程痴情一片的丁力。1997年,担任剧情片《香港制造》的制作人;同年,主演爱情片《天若有情之烽火佳人》,在片中饰演家世显赫的空军少尉刘天伟;12月,与梁家辉联袂主演警匪动作片《黑金》,在片中饰演精明干练、嫉恶如仇的调查局机动组组长方国辉。1998年,主演动作片《龙在江湖》,饰演重义气的黑帮成员韦吉祥;同年,出演喜剧片《赌侠1999》;此外,他还担任剧情片《去年烟花特别多》的制作人。
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZQrlzU1i-1658241798654)(mark-img/20210411005813237.jpg)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>定位会完全压住标准流盒子内容</title>
<style>
.box {
/* 1.浮动的元素不会压住下面标准流的文字 */
/* float: left; */
/* 2. 绝对定位(固定定位) 会压住下面标准流所有的内容。 */
position: absolute;
width: 150px;
height: 150px;
background-color: pink;
}
</style>
</head>
<body>
<div class="box"></div>
<p>阁下何不同风起,扶摇直上九万里</p>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sSbs9oQB-1658241798655)(mark-img/20210411005914370.jpg)]
二、综合案例
【案例:淘宝焦点图布局】
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WedSa5pQ-1658241798655)(mark-img/20210411010357458.jpg)]
布局分析:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ctO7OPyV-1658241798655)(mark-img/20210411010357453.jpg)]
制作:
- 大盒子我们类名为: tb-promo 淘宝广告
- 里面先放一张图片
- 左右两个按钮用链接就好了,左箭头 prev 右箭头 next
- 底侧小圆点 ul 继续做,类名为 promo-nav
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>淘宝轮播图做法</title>
<style>
* {
margin: 0;
padding: 0;
}
li {
list-style: none;
}
.tb-promo {
position: relative;
width: 520px;
height: 280px;
background-color: pink;
margin: 100px auto;
}
.tb-promo img {
width: 520px;
height: 280px;
}
/* 并集选择器可以集体声明相同的样式 */
.prev,
.next {
position: absolute;
/* 绝对定位的盒子垂直居中 */
top: 50%;
margin-top: -15px;
/* 加了绝对定位的盒子可以直接设置高度和宽度 */
width: 20px;
height: 30px;
background: rgba(0, 0, 0, .3);
text-align: center;
line-height: 30px;
color: #fff;
text-decoration: none;
}
.prev {
left: 0;
/* border-radius: 15px; */
border-top-right-radius: 15px;
border-bottom-right-radius: 15px;
}
.next {
/* 如果一个盒子既有 left 属性也有 right 属性,则默认会执行 left 属性
同理 top bottom 会执行 top */
right: 0;
/* border-radius: 15px; */
border-top-left-radius: 15px;
border-bottom-left-radius: 15px;
}
.promo-nav {
position: absolute;
bottom: 15px;
left: 50%;
margin-left: -35px;
width: 70px;
height: 13px;
/* background-color: pink; */
background: rgba(255, 255, 255, .3);
border-radius: 7px;
}
.promo-nav li {
float: left;
width: 8px;
height: 8px;
background-color: #fff;
border-radius: 50%;
margin: 3px;
}
/* 不要忘记选择器权重的问题 */
.promo-nav .selected {
background-color: #ff5000;
}
</style>
</head>
<body>
<div class="tb-promo">
<img src="images/tb.jpg" alt="">
<!-- 左侧按钮箭头 -->
<a href="#" class="prev"> < </a>
<!-- 右侧按钮箭头 -->
<a href="#" class="next"> > </a>
<!-- 小圆点 -->
<ul class="promo-nav">
<li class="selected"></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qw0g44UG-1658241798656)(mark-img/20210411010727218.jpg)]
三、网页布局总结
通过盒子模型,清楚知道大部分 html 标签是一个盒子。
通过 CSS 浮动、定位可以让每个盒子排列成为网页。
一个完整的网页,是标准流、浮动、定位一起完成布局的,每个都有自己的专门用法。
- 标准流
可以让盒子上下排列或者左右排列,垂直的块级盒子显示就用标准流布局。
- 浮动
可以让多个块级元素一行显示或者左右对齐盒子,多个块级盒子水平显示就用浮动布局。
- 定位
定位最大的特点是有层叠的概念,就是可以让多个盒子前后叠压来显示。如果元素自由在某个盒子内移动就用定位布局。
重点:竖向上布局找标准流,横向上布局找浮动,空间上布局找定位!
四、元素的显示与隐藏
类似网站广告,当我们点击关闭就不见了,但是我们重新刷新页面,会重新出现!
本质:让一个元素在页面中隐藏或者显示出来。
注意:是隐藏,不是删除!
- display 显示隐藏(脱标)
- visibility 显示隐藏(不脱标)
- overflow 溢出显示隐藏
4.1 display 属性
display 属性用于设置一个元素应如何显示。
display: none
:隐藏对象display:block
:除了转换为块级元素之外,同时还有显示元素的意思
display 隐藏元素后,不再占有原来的位置(脱标)。
后面应用及其广泛,搭配 JS 可以做很多的网页特效。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>显示隐藏元素之display</title>
<style>
.peppa {
display: none;
/* display: block; */
width: 200px;
height: 200px;
background-color: pink;
}
.george {
width: 200px;
height: 200px;
background-color: skyblue;
}
</style>
</head>
<body>
<div class="peppa">佩奇</div> <!-- 佩奇被隐藏 -->
<div class="george">乔治</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5XsXUvFb-1658241798656)(mark-img/2021041101335757.gif)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>显示隐藏元素之display</title>
<style>
.peppa {
/* display: none; */
display: block;
width: 200px;
height: 200px;
background-color: pink;
}
.george {
width: 200px;
height: 200px;
background-color: skyblue;
}
</style>
</head>
<body>
<div class="peppa">佩奇</div> <!-- 佩奇被显示 -->
<div class="george">乔治</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZsC9uxnr-1658241798656)(mark-img/20210411013334551.gif)]
4.2 visibility 可见性
visibility 属性用于指定一个元素应可见还是隐藏。
visibility:visible
:元素可视visibility:hidden
:元素隐藏
visibility 隐藏元素后,继续占有原来的位置。
如果隐藏元素想要原来位置, 就用 visibility:hidden。
如果隐藏元素不想要原来位置, 就用 display:none(用处更多,重点)。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>显示隐藏元素之display</title>
<style>
.baba {
visibility: hidden;
width: 200px;
height: 200px;
background-color: pink;
}
.mama {
width: 200px;
height: 200px;
background-color: skyblue;
}
</style>
</head>
<body>
<div class="baba">猪爸爸</div>
<div class="mama">猪妈妈</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LoAnMARp-1658241798657)(mark-img/20210411013759206.gif)]
4.3 overflow 溢出
overflow 属性指定了如果内容溢出一个元素的框(超过其指定高度及宽度)时,会发生什么。
属性值 | 描述 |
---|---|
visible | 不剪切内容也不添加滚动条(默认方式) |
hidden | 不显示超过对象尺寸的内容,超出的部分隐藏掉(并非删除) |
scroll | 不管超出的内容否,总是显示滚动条 |
auto | 超出自动显示滚动条,不超出不显示滚动条 |
一般情况下,我们都不想让溢出的内容显示出来,因为溢出的部分会影响布局。
但是如果有定位的盒子, 请慎用 overflow: hidden 因为它会隐藏多余的部分(例如:学成在线 hot new 模块,右上角有故意超出的部分,此时就不能使用 overflow: hidden)。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>显示隐藏元素之overflow</title>
<style>
.peppa {
/* overflow: visible; */
/* overflow: hidden; */
/* scroll 溢出的部分显示滚动条 不溢出也显示滚动条 */
/* overflow: scroll; */
/* auto 溢出的时候才显示滚动条 不溢出不显示滚动条 */
/* overflow: auto; */
width: 200px;
height: 200px;
border: 3px solid pink;
margin: 100px auto;
}
</style>
</head>
<body>
<div class="peppa">
小猪佩奇》,又译作《粉红猪小妹》(台湾译为粉红猪),原名为《Peppa
Pig》,是由英国人阿斯特利(Astley)、贝克(Baker)、戴维斯(Davis)创作、
导演和制作的一部英国学前电视动画片,也是历年来最具潜力的学前儿童品牌。
故事围绕小猪佩奇与家人的愉快经历,幽默而有趣,
藉此宣扬传统家庭观念与友情,鼓励小朋友们体验生活。
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WTkeKOju-1658241798657)(mark-img/20210411015310864.jpg)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>显示隐藏元素之overflow</title>
<style>
.peppa {
overflow: visible;
/* overflow: hidden; */
/* scroll 溢出的部分显示滚动条 不溢出也显示滚动条 */
/* overflow: scroll; */
/* auto 溢出的时候才显示滚动条 不溢出不显示滚动条 */
/* overflow: auto; */
width: 200px;
height: 200px;
border: 3px solid pink;
margin: 100px auto;
}
</style>
</head>
<body>
<div class="peppa">
小猪佩奇》,又译作《粉红猪小妹》(台湾译为粉红猪),原名为《Peppa
Pig》,是由英国人阿斯特利(Astley)、贝克(Baker)、戴维斯(Davis)创作、
导演和制作的一部英国学前电视动画片,也是历年来最具潜力的学前儿童品牌。
故事围绕小猪佩奇与家人的愉快经历,幽默而有趣,
藉此宣扬传统家庭观念与友情,鼓励小朋友们体验生活。
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JQMh1w4P-1658241798658)(mark-img/20210411015310864-164232733571925.jpg)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>显示隐藏元素之overflow</title>
<style>
.peppa {
/* overflow: visible; */
overflow: hidden;
/* scroll 溢出的部分显示滚动条 不溢出也显示滚动条 */
/* overflow: scroll; */
/* auto 溢出的时候才显示滚动条 不溢出不显示滚动条 */
/* overflow: auto; */
width: 200px;
height: 200px;
border: 3px solid pink;
margin: 100px auto;
}
</style>
</head>
<body>
<div class="peppa">
小猪佩奇》,又译作《粉红猪小妹》(台湾译为粉红猪),原名为《Peppa
Pig》,是由英国人阿斯特利(Astley)、贝克(Baker)、戴维斯(Davis)创作、
导演和制作的一部英国学前电视动画片,也是历年来最具潜力的学前儿童品牌。
故事围绕小猪佩奇与家人的愉快经历,幽默而有趣,
藉此宣扬传统家庭观念与友情,鼓励小朋友们体验生活。
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FJ9gq3He-1658241798658)(mark-img/20210411015442418.jpg)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>显示隐藏元素之overflow</title>
<style>
.peppa {
/* overflow: visible; */
/* overflow: hidden; */
/* scroll 溢出的部分显示滚动条 不溢出也显示滚动条 */
overflow: scroll;
/* auto 溢出的时候才显示滚动条 不溢出不显示滚动条 */
/* overflow: auto; */
width: 200px;
height: 200px;
border: 3px solid pink;
margin: 100px auto;
}
</style>
</head>
<body>
<div class="peppa">
小猪佩奇》,又译作《粉红猪小妹》(台湾译为粉红猪),原名为《Peppa
Pig》,是由英国人阿斯特利(Astley)、贝克(Baker)、戴维斯(Davis)创作、
导演和制作的一部英国学前电视动画片,也是历年来最具潜力的学前儿童品牌。
故事围绕小猪佩奇与家人的愉快经历,幽默而有趣,
藉此宣扬传统家庭观念与友情,鼓励小朋友们体验生活。
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iVNX8Zce-1658241798659)(mark-img/20210411015532804.jpg)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>显示隐藏元素之overflow</title>
<style>
.peppa {
/* overflow: visible; */
/* overflow: hidden; */
/* scroll 溢出的部分显示滚动条 不溢出也显示滚动条 */
overflow: scroll;
/* auto 溢出的时候才显示滚动条 不溢出不显示滚动条 */
/* overflow: auto; */
width: 200px;
height: 200px;
border: 3px solid pink;
margin: 100px auto;
}
</style>
</head>
<body>
<div class="peppa">
小猪佩奇》,又译作《粉红猪小妹》(台湾译为粉红猪),原名为《Peppa
Pig》
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-unc0PPTd-1658241798659)(mark-img/20210411015657674.jpg)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>显示隐藏元素之overflow</title>
<style>
.peppa {
/* overflow: visible; */
/* overflow: hidden; */
/* scroll 溢出的部分显示滚动条 不溢出也显示滚动条 */
/* overflow: scroll; */
/* auto 溢出的时候才显示滚动条 不溢出不显示滚动条 */
overflow: auto;
width: 200px;
height: 200px;
border: 3px solid pink;
margin: 100px auto;
}
</style>
</head>
<body>
<div class="peppa">
小猪佩奇》,又译作《粉红猪小妹》(台湾译为粉红猪),原名为《Peppa
Pig》,是由英国人阿斯特利(Astley)、贝克(Baker)、戴维斯(Davis)创作、
导演和制作的一部英国学前电视动画片,也是历年来最具潜力的学前儿童品牌。
故事围绕小猪佩奇与家人的愉快经历,幽默而有趣,
藉此宣扬传统家庭观念与友情,鼓励小朋友们体验生活。
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Bao3qljq-1658241798660)(mark-img/2021041101581715.jpg)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>显示隐藏元素之overflow</title>
<style>
.peppa {
/* overflow: visible; */
/* overflow: hidden; */
/* scroll 溢出的部分显示滚动条 不溢出也显示滚动条 */
/* overflow: scroll; */
/* auto 溢出的时候才显示滚动条 不溢出不显示滚动条 */
overflow: auto;
width: 200px;
height: 200px;
border: 3px solid pink;
margin: 100px auto;
}
</style>
</head>
<body>
<div class="peppa">
小猪佩奇》,又译作《粉红猪小妹》(台湾译为粉红猪),原名为《Peppa
Pig》
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WgJ9Qd9S-1658241798660)(mark-img/20210411015908937.jpg)]
4.4 总结
- display 显示隐藏元素 但是不保留位置
- visibility 显示隐藏元素 但是保留原来的位置
- overflow 溢出显示隐藏 但是只是对于溢出的部分处理
五、综合案例
【案例:土豆网鼠标经过显示遮罩】
- 练习元素的显示与隐藏
- 练习元素的定位
核心原理:原先半透明的黑色遮罩看不见, 鼠标经过大盒子,就显示出来。
遮罩的盒子不占有位置,就需要用绝对定位 和 display 配合使用。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>仿土豆网显示隐藏遮罩案例</title>
<style>
.tudou {
position: relative;
width: 444px;
height: 320px;
background-color: pink;
margin: 30px auto;
}
.tudou img {
width: 100%;
height: 100%;
}
.mask {
/* 隐藏遮罩层 */
display: none;
/* 添加定位使其能够浮与其他盒子上方 */
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, .4) url(images/arr.png) no-repeat center;
}
/* 当我们鼠标经过了 土豆这个盒子,就让里面遮罩层显示出来 */
.tudou:hover .mask {
/* 而是显示元素 */
display: block;
}
</style>
</head>
<body>
<div class="tudou">
<div class="mask"></div>
<img src="images/tudou.jpg" alt="">
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PDx3kMCd-1658241798661)(mark-img/20210411020723514.gif)]
用 TODO 等做标记:
标记 | 用法 |
---|---|
TODO | 待做 |
FIXME | 待修复 |
HACK | 粗糙的解决方案 |
XXX | 危险!这里有重要的问题 |
【移动Web开发之flex布局】
本文档是个人对 Pink 老师课程的总结归纳及补充,转载请注明出处!
一、flex布局体验
1.1 传统布局与flex布局
【传统布局】
-
兼容性好
-
布局繁琐
-
局限性,不能在移动端很好的布局
【flex布局】
- 操作方便,布局极为简单,移动端应用很广泛
- PC 端浏览器支持情况较差
- IE11 或更低版本不支持或仅部分支持
建议:
- 如果是 PC 端页面布局,我们还是建议使用传统布局
- 如果是移动端或者不考虑兼容性问题的 PC 端页面布局,推荐使用 flex 弹性布局
1.2 初体验
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>01-flex布局体验</title>
<style>
div {
display: flex;
width: 80%;
height: 300px;
background-color: pink;
}
div span {
/* 弹性布局中:行内盒子的宽高可直接设置了,这也是优于百分比布局的地方之一 */
/* 免去了浮动的设置,以及对父盒子清除浮动的麻烦 */
width: 150px;
height: 100px;
background-color: black;
margin-right: 5px;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-U6z1xADP-1658241798661)(mark-img/64fd881a1eef4c89a6fcb00097f3148b.png)]
等间距分布
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>01-flex布局体验</title>
<style>
div {
display: flex;
width: 80%;
height: 300px;
background-color: pink;
justify-content: space-around;
}
div span {
/* 弹性布局中:行内盒子的宽高可直接设置了 */
width: 150px;
height: 100px;
background-color: black;
margin-right: 5px;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PNC8aens-1658241798662)(mark-img/23e4d8a8d2744e8eab0b5ec0b647a853.gif)]
平均分为三等分
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>01-flex布局体验</title>
<style>
div {
display: flex;
width: 80%;
height: 300px;
background-color: pink;
justify-content: space-around;
}
div span {
/* 弹性布局中:行内盒子的宽高可直接设置了 */
/* width: 150px; */
height: 100px;
background-color: black;
margin-right: 5px;
flex: 1;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6PZhNyiQ-1658241798662)(mark-img/91471912c9304a67b5266502b67baec7.gif)]
二、flex布局原理
2.1 布局原理
flex 是 flexible Box 的缩写,意为 “弹性布局”,用来为盒状模型提供最大的灵活性,任何一个容器都可以指定为 flex 布局。
- 当我们为父盒子设为 flex 布局以后,子元素的 float(浮动功能)、clear(清除浮动功能)和 vertical-align(垂直居中功能)属性将失效。
- 伸缩布局 = 弹性布局 = 伸缩盒布局 = 弹性盒布局 = flex 布局
采用 flex 布局的元素,称为 flex 容器(flex container),简称 “容器”。它的所有子元素自动成为容器成员,称为 flex 项目(flex item),简称 “项目”。
- 上面的体验中 div 就是 flex 父容器
- 上面的体验中 span 就是子容器 flex 项目
- 子容器可以横向排列也可以纵向排列
【子容器横向排列时的图示】
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-13TqN80r-1658241798662)(mark-img/7f22d8ac7852446f869984a7a5841eb6.png)]
【总结 flex 布局原理】
就是通过给父盒子添加 flex 属性,来控制子盒子的位置和排列方式。
三、flex布局父项常见属性
3.1 常见父项属性
以下由 6 个属性是对父元素设置的
flex-direction
:设置主轴的方向justify-content
:设置主轴上的子元素排列方式flex-wrap
:设置子元素是否换行align-content
:设置侧轴上的子元素的排列方式(多行)align-items
:设置侧轴上的子元素排列方式(单行)flex-flow
:复合属性,相当于同时设置了 flex-direction 和 flex-wrap
3.2 flex-direction设置主轴的方向
(1)主轴与侧轴
在 flex 布局中,是分为主轴和侧轴两个方向,同样的叫法有:行 和 列、x轴 和 y轴
- 默认主轴方向就是 x轴 方向,水平向右
- 默认侧轴方向就是 y轴 方向,水平向下
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WAKjJIBp-1658241798663)(mark-img/fa87013b3a3b43c48256b2ed15b670e4.png)]
(2)属性值
flex-direction
属性决定主轴的方向(即:项目的排列方向)
注意:主轴和侧轴是会变化的,就看 flex-direction 设置谁为主轴,剩下的就是侧轴。而我们的子元素是跟着主轴来排列的。
属性值 | 说明 |
---|---|
row | 从左到右(默认值) |
row-reverse | 从右到左 |
column | 从上到下 |
column-reverse | 从下到上 |
【案例】
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>02-flex主轴方向</title>
<style>
div {
/* 给父级添加flex属性 */
display: flex;
width: 800px;
height: 300px;
background-color: pink;
/* 默认的主轴是 x 轴,那么 y 轴就是侧轴喽 */
/* 我们的元素是跟着主轴来排列的 */
flex-direction: row;
/* 简单了解翻转即可 */
/* flex-direction: row-reverse; */
/* 我们可以把我们的主轴设置为 y 轴 那么 x 轴就成了侧轴 */
/* flex-direction: column; */
}
div span {
width: 150px;
height: 100px;
background-color: purple;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FTlXzb2D-1658241798663)(mark-img/e203d92f12b64482ba5ea5e99360ede1.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>02-flex主轴方向</title>
<style>
div {
/* 给父级添加flex属性 */
display: flex;
width: 800px;
height: 300px;
background-color: pink;
/* 默认的主轴是 x 轴,那么 y 轴就是侧轴喽 */
/* 我们的元素是跟着主轴来排列的 */
/* flex-direction: row; */
/* 简单了解翻转即可 */
flex-direction: row-reverse;
/* 我们可以把我们的主轴设置为 y 轴 那么 x 轴就成了侧轴 */
/* flex-direction: column; */
}
div span {
width: 150px;
height: 100px;
background-color: purple;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MSrPhKMk-1658241798663)(mark-img/6765176e8adb4b9dac8117df043795fd.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>02-flex主轴方向</title>
<style>
div {
/* 给父级添加flex属性 */
display: flex;
width: 800px;
height: 300px;
background-color: pink;
/* 默认的主轴是 x 轴,那么 y 轴就是侧轴喽 */
/* 我们的元素是跟着主轴来排列的 */
/* flex-direction: row; */
/* 简单了解翻转即可 */
/* flex-direction: row-reverse; */
/* 我们可以把我们的主轴设置为 y 轴 那么 x 轴就成了侧轴 */
flex-direction: column;
}
div span {
width: 150px;
height: 100px;
background-color: purple;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PQWnCODi-1658241798664)(mark-img/4e52189e5aae4108805c8c027df4934c.png)]
3.3 justify-content设置主轴上的子元素排列方式
justify-content
属性定义了项目在主轴上的对齐方式
注意:使用这个属性之前一定要确定好主轴是哪个!
属性值 | 说明 |
---|---|
flex-start | 从头部开始,如果主轴是 x轴,则从左到右(默认值) |
flex-end | 从尾部开始排列 |
center | 在主轴居中对齐(如果主轴是 x轴 则 水平居中) |
space-around | 平分剩余空间 |
space-between | 先两边贴边再平方剩余空间(重要) |
【案例】
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>03-flex设置主轴上的子元素排列方式1</title>
<style>
div {
display: flex;
width: 800px;
height: 300px;
background-color: pink;
/* 默认的主轴是 x轴 row */
flex-direction: row;
/* justify-content: 是设置主轴上子元素的排列方式 */
/* 从头部开始,如果主轴是 x轴,则从左到右(默认值) */
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: gray;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ck7Bsr1q-1658241798664)(mark-img/408e7c1570d44daa9bc4a62372d27baa.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>03-flex设置主轴上的子元素排列方式1</title>
<style>
div {
display: flex;
width: 800px;
height: 300px;
background-color: pink;
/* 默认的主轴是 x轴 row */
flex-direction: row;
/* justify-content: 是设置主轴上子元素的排列方式 */
/* 从头部开始,如果主轴是 x轴,则从左到右(默认值) */
/* 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: gray;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9W86lOAv-1658241798665)(mark-img/201e75d13fc8475b84a23e10b5011609.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>03-flex设置主轴上的子元素排列方式1</title>
<style>
div {
display: flex;
width: 800px;
height: 300px;
background-color: pink;
/* 默认的主轴是 x轴 row */
flex-direction: row;
/* justify-content: 是设置主轴上子元素的排列方式 */
/* 从头部开始,如果主轴是 x轴,则从左到右(默认值) */
/* 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: gray;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tFzSEjxS-1658241798665)(mark-img/79b89bb112374435bad2705726e16aab.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>03-flex设置主轴上的子元素排列方式1</title>
<style>
div {
display: flex;
width: 800px;
height: 300px;
background-color: pink;
/* 默认的主轴是 x轴 row */
flex-direction: row;
/* justify-content: 是设置主轴上子元素的排列方式 */
/* 从头部开始,如果主轴是 x轴,则从左到右(默认值) */
/* 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: gray;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Aj1hxAiW-1658241798666)(mark-img/0035cfe000b24b15b78ee9a9dad02fb5.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uNHSA2Wv-1658241798666)(mark-img/40be74d06a4c43d888edddda10e80d16.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>03-flex设置主轴上的子元素排列方式1</title>
<style>
div {
display: flex;
width: 800px;
height: 300px;
background-color: pink;
/* 默认的主轴是 x轴 row */
flex-direction: row;
/* justify-content: 是设置主轴上子元素的排列方式 */
/* 从头部开始,如果主轴是 x轴,则从左到右(默认值) */
/* 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: gray;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LMDccxr8-1658241798666)(mark-img/8bc9d4554a9546569c350576c429a739.png)]
注意:以上例子并不能根据浏览器窗口大小自动调整子项之间的间距,因为父盒子的宽度是固定死 800px 的,假如我们把父盒子宽度设为 80%,那么就可以有效果了。
div {width: 800px;}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MGF6Rk9s-1658241798666)(mark-img/2345234532.gif)]
div {width: 80%;}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qeNCbI7P-1658241798667)(mark-img/45235324.gif)]
div {width: 80%;}
div span {width: 80%;}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kCO9Pzvd-1658241798667)(mark-img/349857.gif)]
【以上到下为主轴的案例】
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>04-flex设置主轴上的子元素排列方式2</title>
<style>
div {
display: flex;
width: 800px;
height: 400px;
background-color: pink;
/* 我们现在的主轴是 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: 150px;
height: 100px;
background-color: gray;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-z2R46A6x-1658241798668)(mark-img/ceb344ce1a3340e48cf18c89f6b3a563.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>04-flex设置主轴上的子元素排列方式2</title>
<style>
div {
display: flex;
width: 800px;
height: 400px;
background-color: pink;
/* 我们现在的主轴是 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: 150px;
height: 100px;
background-color: gray;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8YLPCdRe-1658241798668)(mark-img/c27f6656f46144f3bac38ff8754b6aa9.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>04-flex设置主轴上的子元素排列方式2</title>
<style>
div {
display: flex;
width: 800px;
height: 400px;
background-color: pink;
/* 我们现在的主轴是 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: 150px;
height: 100px;
background-color: gray;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
盒子自动垂直居中的困扰终于解决啦!!!
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6RcLnngR-1658241798668)(mark-img/e3df7d958bd84be6b4acb4a5d44e2ea0.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>04-flex设置主轴上的子元素排列方式2</title>
<style>
div {
display: flex;
width: 800px;
height: 400px;
background-color: pink;
/* 我们现在的主轴是 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: 150px;
height: 100px;
background-color: gray;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HnUXDzl1-1658241798669)(mark-img/b28d83e992824ef381b7c9ef662633ef.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>04-flex设置主轴上的子元素排列方式2</title>
<style>
div {
display: flex;
width: 800px;
height: 400px;
background-color: pink;
/* 我们现在的主轴是 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: 150px;
height: 100px;
background-color: gray;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-baBQJFWG-1658241798669)(mark-img/ff3f75db39de49edaa70f9e722ce3262.png)]
3.4 flex-wrap设置子元素是否换行
默认情况下,项目都排在一条线(又称 “轴线”)上。flex-wrap 属性定义,flex 布局中默认是不换行的。
属性值 | 说明 |
---|---|
nowrap | 不换行(默认值) |
wrap | 换行 |
【案例】
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>05-flex-wrap子元素是否换行</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: gray;
color: #fff;
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">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>05-flex-wrap子元素是否换行</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: gray;
color: #fff;
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>
3.5 align-items设置侧轴上的子元素排列方式(单行)
该属性是控制子项在侧轴(默认是 y轴)上的排列方式,在子项为单项(单行)的时候使用。
属性值 | 说明 |
---|---|
flex-start | 从上到下 |
flex-end | 从下到上 |
center | 挤在一起居中 |
stretch | 拉伸(默认值)注:前提是子盒子没有指定高度,否则没有效果! |
【案例】
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>06-flex设置侧轴上的子元素排列方式</title>
<style>
div {
display: flex;
width: 800px;
height: 400px;
background-color: pink;
/* 默认的主轴是 x轴 row */
flex-direction: row;
justify-content: center;
/* 设置侧轴:从上到下 */
align-items: flex-start;
}
div span {
width: 150px;
height: 100px;
background-color: gray;
color: #fff;
margin: 10px;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ve8twemx-1658241798670)(mark-img/50d2926f0d9c483794b81138df42d132.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>06-flex设置侧轴上的子元素排列方式</title>
<style>
div {
display: flex;
width: 800px;
height: 400px;
background-color: pink;
/* 默认的主轴是 x轴 row */
flex-direction: row;
justify-content: center;
/* 设置侧轴:从下到上 */
align-items: flex-end;
}
div span {
width: 150px;
height: 100px;
background-color: gray;
color: #fff;
margin: 10px;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sHSybsRi-1658241798671)(mark-img/43173bfa7ed349c89d4003c33192941d.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>06-flex设置侧轴上的子元素排列方式</title>
<style>
div {
display: flex;
width: 800px;
height: 400px;
background-color: pink;
/* 默认的主轴是 x轴 row */
flex-direction: row;
justify-content: center;
/* 设置侧轴:挤在一起居中 */
align-items: center;
}
div span {
width: 150px;
height: 100px;
background-color: gray;
color: #fff;
margin: 10px;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EiMK1u3F-1658241798671)(mark-img/f515660386034bc29da90af33b5947c5.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>06-flex设置侧轴上的子元素排列方式</title>
<style>
div {
display: flex;
width: 800px;
height: 400px;
background-color: pink;
/* 默认的主轴是 x轴 row */
flex-direction: row;
justify-content: center;
/* 设置侧轴:拉伸(默认) */
align-items: stretch;
}
div span {
width: 150px;
/* 拉伸的前提是没有指定高度 */
/* height: 100px; */
background-color: gray;
color: #fff;
margin: 10px;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZpUYbgWU-1658241798671)(mark-img/9d77308724964371a55c2b88e79522f0.png)]
align-items 只能统一对侧轴上的子元素排列方式,假如有多行子元素,要分别对不同的行设置不同的排列方式,那么此种方式就无法做了。
3.6 align-content 设置侧轴上的子元素的排列方式(多行)
设置子项在侧轴上的排列方式并且只能用于子项出现 换行 的情况(多行),在单行下是没有效果的。
属性值 | 说明 |
---|---|
flex-start | 在侧轴的头部开始排列 |
flex-end | 在侧轴的尾部开始排列 |
center | 在侧轴中间显示 |
space-around | 子项在侧轴平方剩余空间 |
space-between | 子项在侧轴先分布在两头,再平分剩余空间 |
stretch | 行拉伸以占据剩余空间(默认值) |
【案例】
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>07-flex设置侧轴上的子元素排列方式(多行)</title>
<style>
div {
display: flex;
width: 800px;
height: 400px;
background-color: pink;
/* 换行 */
flex-wrap: wrap;
/* 因为有了换行,此时我们侧轴上控制子元素的对齐方式我们用 align-content */
align-content: flex-start;
/* align-content: center; */
/* align-content: space-around; */
/* align-content: space-between; */
/* align-content: stretch; */
}
div span {
width: 150px;
height: 100px;
background-color: gray;
color: #fff;
margin: 10px;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WW3WkQU7-1658241798672)(mark-img/aac0655dd5044255a703a1b084f89962.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>07-flex设置侧轴上的子元素排列方式(多行)</title>
<style>
div {
display: flex;
width: 800px;
height: 400px;
background-color: pink;
/* 换行 */
flex-wrap: wrap;
/* 因为有了换行,此时我们侧轴上控制子元素的对齐方式我们用 align-content */
/* align-content: flex-start; */
align-content: center;
/* align-content: space-around; */
/* align-content: space-between; */
/* align-content: stretch; */
}
div span {
width: 150px;
height: 100px;
background-color: gray;
color: #fff;
margin: 10px;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VcXZLuVu-1658241798672)(mark-img/43d771e31287418a839a644229b6e604.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>07-flex设置侧轴上的子元素排列方式(多行)</title>
<style>
div {
display: flex;
width: 800px;
height: 400px;
background-color: pink;
/* 换行 */
flex-wrap: wrap;
/* 因为有了换行,此时我们侧轴上控制子元素的对齐方式我们用 align-content */
/* align-content: flex-start; */
/* align-content: center; */
align-content: space-around;
/* align-content: space-between; */
/* align-content: stretch; */
}
div span {
width: 150px;
height: 100px;
background-color: gray;
color: #fff;
margin: 10px;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Oe9MwYYA-1658241798673)(mark-img/af5d93c39b134dc3958497f57876de05.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>07-flex设置侧轴上的子元素排列方式(多行)</title>
<style>
div {
display: flex;
width: 800px;
height: 400px;
background-color: pink;
/* 换行 */
flex-wrap: wrap;
/* 因为有了换行,此时我们侧轴上控制子元素的对齐方式我们用 align-content */
/* align-content: flex-start; */
/* align-content: center; */
/* align-content: space-around; */
align-content: space-between;
/* align-content: stretch; */
}
div span {
width: 150px;
height: 100px;
background-color: gray;
color: #fff;
margin: 10px;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-S7sP5A87-1658241798673)(mark-img/463ca268100a4df0b092622931cbf05b.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>07-flex设置侧轴上的子元素排列方式(多行)</title>
<style>
div {
display: flex;
width: 800px;
height: 400px;
background-color: pink;
/* 换行 */
flex-wrap: wrap;
/* 因为有了换行,此时我们侧轴上控制子元素的对齐方式我们用 align-content */
/* align-content: flex-start; */
/* align-content: center; */
/* align-content: space-around; */
/* align-content: space-between; */
align-content: stretch;
}
div span {
width: 150px;
height: 100px;
background-color: gray;
color: #fff;
margin: 10px;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qP8P4mX8-1658241798673)(mark-img/b85cdc0e53e649d1bba4979efe7b2b97.png)]
3.7 align-content和align-items区别
- align-items 适用于单行情况下,只有上对齐、下对齐、居中和拉伸
- align-content 适应于换行(多行)的情况下(单行情况下无效),可以设置上对齐、下对齐、居中、拉伸以及平均分配剩余空间等属性值
- 总结就是单行找 align-items 多行找 align-content
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-D8T8O6hn-1658241798674)(mark-img/d84394df9abd463f9b15096f838920ec.png)]
3.8 flex-flow
flex-flow
属性是 flex-direction 和 flex-wrap 属性的复合属性
flex-flow: row wrap;
flex-direction
:设置主轴的方向justify-content
:设置主轴上的子元素排列方式flex-wrap
:设置子元素是否换行align-content
:设置侧轴上的子元素的排列方式(多行)align-items
:设置侧轴上的子元素排列方式(单行)flex-flow
:复合属性,相当于同时设置了 flex-direction 和 flex-wrap
【案例】
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>08-flex-flow复合属性</title>
<style>
div {
display: flex;
width: 600px;
height: 300px;
background-color: pink;
/* flex-direction: column;
flex-wrap: wrap; */
/* 把设置主轴方向和是否换行(换列)简写 */
flex-flow: column wrap;
}
div span {
width: 150px;
height: 100px;
background-color: gray;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CSoyxLsz-1658241798674)(mark-img/209dea57f86a4bb8be262eb10e469012.png)]
四、flex布局子项常见属性
- flex 子项目占的份数
- align-self 控制子项自己在侧轴的排列方式
- order 属性定义子项的排列顺序(前后顺序)
4.1 flex属性
flex 属性定义子项目分配剩余空间,用 flex 来表示占多少份数。
.item {
flex: <number>; /* default 0 */
}
【案例】
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>09-flex子项flex份数</title>
<style>
section {
display: flex;
width: 60%;
height: 150px;
background-color: pink;
margin: 0 auto;
}
section div:nth-child(1) {
width: 100px;
height: 150px;
background-color: rgb(166, 255, 139);
}
section div:nth-child(2) {
flex: 1;
background-color: rgb(0, 0, 0);
}
section div:nth-child(3) {
width: 100px;
height: 150px;
background-color: rgb(124, 185, 255);
}
/* --------------------------------- */
p {
display: flex;
width: 60%;
height: 150px;
background-color: pink;
margin: 100px auto;
}
p span {
flex: 1;
}
p span:nth-child(2) {
flex: 2;
background-color: gray;
}
</style>
</head>
<body>
<section>
<div></div>
<div></div>
<div></div>
</section>
<p>
<span>1</span>
<span>2</span>
<span>3</span>
</p>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-a27bq4gH-1658241798674)(mark-img/8bab533107f2414586bb63a80855afae.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-s4S4evzb-1658241798675)(mark-img/e65b01f3144544a0b6b167c7bf01a11a.gif)]
4.2 align-self控制子项自己在侧轴上的排列方式
align-self
属性允许单个项目有与其他项目不一样的对齐方式,可覆盖 align-items 属性。
默认值 auto,表示继承父元素 align-items 属性,如果没有父元素,则等同于 stretch。
span:nth-child(2) {
/* 设置自己在侧轴上的排列方式 */
align-self: flex-end;
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>align-self控制子项自己在侧轴上的排列方式</title>
<style>
div {
display: flex;
width: 800px;
height: 400px;
background-color: pink;
/* 默认的主轴是 x轴 row */
flex-direction: row;
justify-content: center;
/* 设置侧轴:从下到上 */
align-items: flex-end;
}
div span {
width: 150px;
height: 100px;
background-color: gray;
color: #fff;
margin: 10px;
}
div span:nth-child(2) {
align-self: center;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1U8F1iA6-1658241798675)(mark-img/image-20220119013638909.png)]
4.3 order属性定义项目的排列顺序
数值越小,排列越靠前,默认为 0。
注意:和 z-index 不一样。
.item {
order: <number>;
}
【案例】
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>10-align-self和order</title>
<style>
div {
display: flex;
width: 80%;
height: 300px;
background-color: pink;
/* 让三个子盒子沿着侧轴底侧对齐 */
/* align-items: flex-end; */
/* 我们想只让3号盒子下来底侧 */
}
div span {
width: 150px;
height: 100px;
background-color: gray;
margin-right: 5px;
}
div span:nth-child(2) {
/* 排列顺序 */
/* 默认是0,-1比0小所以在前面 */
order: -1;
}
div span:nth-child(3) {
/* 单独设置 */
align-self: flex-end;
}
</style>
</head>
<body>
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TczG4WL5-1658241798675)(mark-img/b6f1452b4b354803b5a656bc68289881.png)]
五、携程网首页案例制作
【案例:携程网移动端首页】
访问地址:https://m.ctrip.com/
(1)技术选型
方案:我们采取单独制作移动页面方案
技术:布局采取 flex 布局
(2)搭建相关文件夹结构
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pAmUzP9X-1658241798676)(mark-img/c06c94c2caa54ad4ab5d2d9f869eae34.png)]
(3)设置视口标签以及引入初始化样式
<meta name="viewport" content="width=device-width, user-scalable=no,
initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/index.css">
(4)常用初始化样式
/* 放在 index.css 中 */
body {
max-width: 540px;
min-width: 320px;
margin: 0 auto;
font: normal 14px/1.5 Tahoma, "Lucida Grande", Verdana, "Microsoft Yahei", STXihei, hei;
color: #000;
background: #f2f2f2;
overflow-x: hidden;
-webkit-tap-highlight-color: transparent;
}
(5)常见模块命名
(6)常见 flex 布局思路
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SDeq0RJK-1658241798677)(mark-img/4b29644d4f634122a8619a17ba22b9fd.png)]
【完整代码】
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/index.css">
<title>携程在手,说走就走</title>
</head>
<body>
<!-- 顶部搜索 -->
<div class="search-index">
<div class="search">搜索:目的地/酒店/景点/航班号</div>
<a href="#" class="user">我 的</a>
</div>
<!-- 焦点图模块 -->
<div class="focus">
<img src="upload/focus.jpg" alt="">
</div>
<!-- 局部导航栏 -->
<ul class="local-nav">
<li>
<a href="#" title="景点·玩乐">
<span class="local-nav-icon-icon1"></span>
<span>景点·玩乐</span>
</a>
</li>
<li>
<a href="#" title="景点·玩乐">
<span class="local-nav-icon-icon2"></span>
<span>景点·玩乐</span>
</a>
</li>
<li>
<a href="#" title="景点·玩乐">
<span class="local-nav-icon-icon3"></span>
<span>景点·玩乐</span>
</a>
</li>
<li>
<a href="#" title="景点·玩乐">
<span class="local-nav-icon-icon4"></span>
<span>景点·玩乐</span>
</a>
</li>
<li>
<a href="#" title="景点·玩乐">
<span class="local-nav-icon-icon5"></span>
<span>景点·玩乐</span>
</a>
</li>
</ul>
<!-- 主导航栏 -->
<nav>
<div class="nav-common">
<div class="nav-items">
<a href="#">海外酒店</a>
</div>
<div class="nav-items">
<a href="#">海外酒店</a>
<a href="#">特价酒店</a>
</div>
<div class="nav-items">
<a href="#">海外酒店</a>
<a href="#">特价酒店</a>
</div>
</div>
<div class="nav-common">
<div class="nav-items">
<a href="#">海外酒店</a>
</div>
<div class="nav-items">
<a href="#">海外酒店</a>
<a href="#">特价酒店</a>
</div>
<div class="nav-items">
<a href="#">海外酒店</a>
<a href="#">特价酒店</a>
</div>
</div>
<div class="nav-common">
<div class="nav-items">
<a href="#">海外酒店</a>
</div>
<div class="nav-items">
<a href="#">海外酒店</a>
<a href="#">特价酒店</a>
</div>
<div class="nav-items">
<a href="#">海外酒店</a>
<a href="#">特价酒店</a>
</div>
</div>
</nav>
<!-- 侧导航栏 -->
<ul class="subnav-entry">
<li>
<a href="#">
<span class="subnav-entry-icon"></span>
<span>电话费</span>
</a>
</li>
<li>
<a href="#">
<span class="subnav-entry-icon"></span>
<span>电话费</span>
</a>
</li>
<li>
<a href="#">
<span class="subnav-entry-icon"></span>
<span>电话费</span>
</a>
</li>
<li>
<a href="#">
<span class="subnav-entry-icon"></span>
<span>电话费</span>
</a>
</li>
<li>
<a href="#">
<span class="subnav-entry-icon"></span>
<span>电话费</span>
</a>
</li>
<li>
<a href="#">
<span class="subnav-entry-icon"></span>
<span>电话费</span>
</a>
</li>
<li>
<a href="#">
<span class="subnav-entry-icon"></span>
<span>电话费</span>
</a>
</li>
<li>
<a href="#">
<span class="subnav-entry-icon"></span>
<span>电话费</span>
</a>
</li>
<li>
<a href="#">
<span class="subnav-entry-icon"></span>
<span>电话费</span>
</a>
</li>
<li>
<a href="#">
<span class="subnav-entry-icon"></span>
<span>电话费</span>
</a>
</li>
</ul>
<!-- 销售模块 -->
<div class="sales-box">
<div class="sales-hd">
<h2>热门活动</h2>
<a href="#" class="more">获取更多福利</a>
</div>
<div class="sales-bd">
<div class="row">
<a href="#">
<img src="upload/pic1.jpg" alt="">
</a>
<a href="#">
<img src="upload/pic2.jpg" alt="">
</a>
</div>
<div class="row">
<a href="#">
<img src="upload/pic3.jpg" alt="">
</a>
<a href="#">
<img src="upload/pic4.jpg" alt="">
</a>
</div>
<div class="row">
<a href="#">
<img src="upload/pic5.jpg" alt="">
</a>
<a href="#">
<img src="upload/pic6.jpg" alt="">
</a>
</div>
</div>
</div>
</body>
</html>
body {
max-width: 540px;
min-width: 320px;
margin: 0 auto;
font: normal 14px/1.5 Tahoma, "Lucida Grande", Verdana, "Microsoft Yahei",
STXihei, hei;
color: #000;
background: #f2f2f2;
overflow-x: hidden;
-webkit-tap-highlight-color: transparent;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: #222;
}
div {
box-sizing: border-box;
}
/* 搜索模块 */
.search-index {
display: flex;
/* 固定定位跟父级没有关系 它以屏幕为准 */
position: fixed;
top: 0;
left: 50%;
/* 固定的盒子应该有宽度 */
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
width: 100%;
min-width: 320px;
max-width: 540px;
height: 44px;
background-color: #f6f6f6;
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
}
.search {
position: relative;
height: 26px;
line-height: 24px;
border: 1px solid #ccc;
flex: 1;
font-size: 12px;
color: #666;
margin: 7px 10px;
padding-left: 25px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
.search::before {
content: "";
position: absolute;
top: 5px;
left: 5px;
width: 15px;
height: 15px;
background: url(../images/sprite.png) no-repeat -59px -279px;
background-size: 104px auto;
}
.user {
width: 44px;
height: 44px;
font-size: 12px;
text-align: center;
color: #2eaae0;
}
.user::before {
content: "";
display: block;
width: 23px;
height: 23px;
background: url(../images/sprite.png) no-repeat -59px -194px;
background-size: 104px auto;
margin: 4px auto -2px;
}
/* focus */
.focus {
padding-top: 44px;
}
.focus img {
width: 100%;
}
/* local-nav */
.local-nav {
display: flex;
height: 64px;
margin: 3px 4px;
background-color: #fff;
border-radius: 8px;
}
.local-nav li {
flex: 1;
}
.local-nav a {
display: flex;
flex-direction: column;
/* 侧轴居中对齐 因为是单行 */
align-items: center;
font-size: 12px;
}
.local-nav li [class^="local-nav-icon"] {
width: 32px;
height: 32px;
background-color: pink;
margin-top: 8px;
background: url(../images/localnav_bg.png) no-repeat 0 0;
background-size: 32px auto;
}
.local-nav li .local-nav-icon-icon2 {
background-position: 0 -32px;
}
.local-nav li .local-nav-icon-icon3 {
background-position: 0 -64px;
}
.local-nav li .local-nav-icon-icon4 {
background-position: 0 -96px;
}
.local-nav li .local-nav-icon-icon5 {
background-position: 0 -128px;
}
/* nav */
nav {
overflow: hidden;
border-radius: 8px;
margin: 0 4px 3px;
}
.nav-common {
display: flex;
height: 88px;
background-color: pink;
}
.nav-common:nth-child(2) {
margin: 3px 0;
}
.nav-items {
/* 不冲突的 */
flex: 1;
display: flex;
flex-direction: column;
}
.nav-items a {
flex: 1;
text-align: center;
line-height: 44px;
color: #fff;
font-size: 14px;
/* 文字阴影 */
text-shadow: 1px 1px rgba(0, 0, 0, 0.2);
}
.nav-items a:nth-child(1) {
border-bottom: 1px solid #fff;
}
.nav-items:nth-child(1) a {
border: 0;
background: url(../images/hotel.png) no-repeat bottom center;
background-size: 121px auto;
}
/* -n+2就是选择前面两个元素 */
.nav-items:nth-child(-n + 2) {
border-right: 1px solid #fff;
}
.nav-common:nth-child(1) {
background: -webkit-linear-gradient(left, #fa5a55, #fa994d);
}
.nav-common:nth-child(2) {
background: -webkit-linear-gradient(left, #4b90ed, #53bced);
}
.nav-common:nth-child(3) {
background: -webkit-linear-gradient(left, #34c2a9, #6cd559);
}
/* subnav-entry */
.subnav-entry {
display: flex;
border-radius: 8px;
background-color: #fff;
margin: 0 4px;
flex-wrap: wrap;
padding: 5px 0;
}
.subnav-entry li {
/* 里面的子盒子可以写 % 相对于父级来说的 */
flex: 20%;
}
.subnav-entry a {
display: flex;
flex-direction: column;
align-items: center;
}
.subnav-entry-icon {
width: 28px;
height: 28px;
background-color: pink;
margin-top: 4px;
background: url(../images/subnav-bg.png) no-repeat;
background-size: 28px auto;
}
/* sales-box */
.sales-box {
border-top: 1px solid #bbb;
background-color: #fff;
margin: 4px;
}
.sales-hd {
position: relative;
height: 44px;
border-bottom: 1px solid #ccc;
}
.sales-hd h2 {
position: relative;
text-indent: -999px;
overflow: hidden;
}
.sales-hd h2::after {
position: absolute;
top: 5px;
left: 8px;
content: "";
width: 79px;
height: 15px;
background: url(../images/hot.png) no-repeat 0 -20px;
background-size: 79px auto;
}
.more {
position: absolute;
right: 5px;
top: 0px;
background: -webkit-linear-gradient(left, #ff506c, #ff6bc6);
border-radius: 15px;
padding: 3px 20px 3px 10px;
color: #fff;
}
.more::after {
content: "";
position: absolute;
top: 9px;
right: 9px;
width: 7px;
height: 7px;
border-top: 2px solid #fff;
border-right: 2px solid #fff;
transform: rotate(45deg);
}
.row {
display: flex;
}
.row a {
flex: 1;
border-bottom: 1px solid #eee;
}
.row a:nth-child(1) {
border-right: 1px solid #eee;
}
.row a img {
width: 100%;
}
六、携程网首页案例重点强调
6.1 搜索模块
<!-- 顶部搜索 -->
<div class="search-index">
<div class="search">搜索:目的地/酒店/景点/航班号</div>
<a href="#" class="user">我 的</a>
</div>
最上方的搜索模块整体是利用固定定位来实现的,里面的内容布局则是通过弹性布局实现。
(1)整体部分的定位
.search-index {
/* 固定定位跟父级没有关系 它以屏幕为准 */
position: fixed;
top: 0;
/* 重点:利用 translateX 实现移动自身宽度的一半!*/
left: 50%;
/* 固定的盒子应该有宽度 */
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
/* 搜索框宽度与显示宽度一样 */
width: 100%;
min-width: 320px;
max-width: 540px;
height: 44px;
background-color: #f6f6f6;
}
(2)搜索栏与我的部分布局
- 【对 search-index 设置弹性布局】
display: flex;
- 【对 serch 设置占满1份区域】
.search {
height: 26px;
line-height: 24px;
border: 1px solid #ccc;
/* 设置为占据 1份 */
flex: 1;
font-size: 12px;
color: #666;
margin: 7px 10px;
padding-left: 25px;
}
- 【user 单独设置一个宽度,占据自己那部分】
.user {
width: 44px;
height: 44px;
font-size: 12px;
text-align: center;
color: #2eaae0;
}
最终实现的结构:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oxlSCLLY-1658241798678)(mark-img/image-20220128194425850.png)]
6.2 渐变
6.2.1 背景渐变
(1)背景线性渐变
语法:
background: linear-gradient(起始方向, 颜色1, 颜色2, 颜色3, ...);
background: -webkit-linear-gradient(left, red, blue);
background: -webkit-linear-gradient(left top, red, blue, deeppink);
background: -webkit-linear-gradient(45deg, red, blue);
-
背景渐变必须添加浏览器私有前缀
-
起始方向可以是:方位名词 或 度数,如果省略默认就是 top(从上到下)
-
方位名词:
top
、bottom
、left
、right
、两两组合
-
度数:角度是指水平线和渐变线之间的角度,逆时针方向计算。换句话说,0deg 将创建一个从下到上的渐变,90deg 将创建一个从左到右的渐变。
注意很多浏览器(Chrome、Safari、firefox等)的使用了旧的标准,即 0deg 将创建一个从左到右的渐变,90deg 将创建一个从下到上的渐变。换算公式 90 - x = y 其中 x 为标准角度,y为非标准角度。
【案例】
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>背景线性渐变</title>
<style>
div {
width: 600px;
height: 200px;
background-color: red; /* 浏览器不支持的时候显示 */
/* 背景渐变必须添加浏览器私有前缀 */
/* 由于此处是移动端,所以我们写 webkit 就够了 */
background: -webkit-linear-gradient(left, red, blue);
}
</style>
</head>
<body>
<div></div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rsFD2c98-1658241798679)(mark-img/f6d079aad03f4013b60a124619a11523.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>背景线性渐变</title>
<style>
div {
width: 600px;
height: 200px;
background-color: red; /* 浏览器不支持的时候显示 */
/* 背景渐变必须添加浏览器私有前缀 */
/* 默认从上到下 top */
background: -webkit-linear-gradient(red, blue);
}
</style>
</head>
<body>
<div></div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ug1dbeOS-1658241798679)(mark-img/313c9d5d71814cdf876df9107f18faa9.png)]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>背景线性渐变</title>
<style>
div {
width: 600px;
height: 200px;
background-color: red; /* 浏览器不支持的时候显示 */
/* 背景渐变必须添加浏览器私有前缀 */
/* left top 与 top left 都可以 */
background: -webkit-linear-gradient(left top, red, blue);
}
</style>
</head>
<body>
<div></div>
</body>
</html>
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wyfyj6gV-1658241798679)(mark-img/d94d2dd28f4243a0b6fc2f5efe391c1d.png)]
【使用透明度】
CSS3 渐变也支持透明度(transparent),可用于创建减弱变淡的效果。
为了添加透明度,我们使用 rgba()
函数来定义颜色节点。rgba() 函数中的最后一个参数可以是从 0 到 1 的值,它定义了颜色的透明度:0 表示完全透明,1 表示完全不透明。
下面的实例演示了从左边开始的线性渐变。起点是完全透明,慢慢过渡到完全不透明的红色:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>背景线性渐变</title>
<style>
div {
width: 600px;
height: 200px;
/* 背景渐变必须添加浏览器私有前缀 */
/* 前面的 red 为浏览器不支持时显示 */
background: red -webkit-linear-gradient(left, rgba(0,0,255,.2), rgba(255,0,0,1));
}
</style>
</head>
<body>
<div></div>
</body>
</html>
(2)径向渐变
径向渐变由它的中心定义。
为了创建一个径向渐变,你也必须至少定义两种颜色节点。颜色节点即你想要呈现平稳过渡的颜色。同时,你也可以指定渐变的中心、形状(圆形或椭圆形)、大小。默认情况下,渐变的中心是 center(表示在中心点),渐变的形状是 ellipse(表示椭圆形),渐变的大小是 farthest-corner(表示到最远的角落)。
径向渐变不要加浏览器前缀!
径向渐变 - 颜色节点均匀分布(默认情况下)
width: 300px;
height: 300px;
background: radial-gradient(red, green, blue);
径向渐变 - 颜色节点不均匀分布
width: 300px;
height: 300px;
background: radial-gradient(red 5%, green 15%, blue 60%);
设置形状
shape
参数定义了形状。它可以是值 circle
或 ellipse
。其中,circle
表示圆形,ellipse
表示椭圆形。默认值是 ellipse
。
height: 150px;
width: 200px;
background: radial-gradient(red, yellow, green);
background: radial-gradient(circle, red, yellow, green);
不同尺寸大小关键字的使用
size
参数定义了渐变的大小。它可以是以下四个值:
closest-side
farthest-side
closest-corner
farthest-corner
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
#grad1 {
height: 150px;
width: 150px;
background-color: red; /* 浏览器不支持的时候显示 */
background: radial-gradient(closest-side at 60% 55%, red, yellow, black);
}
#grad2 {
height: 150px;
width: 150px;
background-color: red; /* 浏览器不支持的时候显示 */
background: radial-gradient(farthest-side at 60% 55%, red, yellow, black);
}
#grad3 {
height: 150px;
width: 150px;
background-color: red; /* 浏览器不支持的时候显示 */
background: radial-gradient(closest-corner at 60% 55%, red, yellow, black);
}
#grad4 {
height: 150px;
width: 150px;
background-color: red; /* 浏览器不支持的时候显示 */
background: radial-gradient(farthest-corner at 60% 55%, red, yellow, black);
}
</style>
</head>
<body>
<h3>径向渐变 - 不同尺寸大小关键字的使用</h3>
<p><strong>closest-side:</strong></p>
<div id="grad1"></div>
<p><strong>farthest-side:</strong></p>
<div id="grad2"></div>
<p><strong>closest-corner:</strong></p>
<div id="grad3"></div>
<p><strong>farthest-corner(默认):</strong></p>
<div id="grad4"></div>
</body>
</html>
6.2.2 文字渐变
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
p{
font-size: 60px;
background: linear-gradient(0deg,red,blue);
background-clip:text;
-webkit-background-clip: text;
color: transparent;
}
</style>
</head>
<body>
<p>
我是渐变文字
</p>
</body>
</html>
核心内容说明
background 属性:设置背景图片为线性渐变色。
background-clip 属性:规定背景的绘制区域(我们注意到该属性上的-webkit-,说明该属性还存在兼容问题,并不是所有浏览器都支持,在 W3C 是没有 text 这个值的,这里的 text 是背景被裁剪到文字)。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0VTL5G54-1658241798680)(mark-img/image-20220128191422675.png)]
color 属性:设置文字颜色为透明,然后面的背景色显示出来。