一、浮动float
1、浮动有什么用?
- 将多个块级元素在一行内水平排列
- 创建一个浮动框,将其移到左/右侧,排列到前一个的块或浮动框后
2、float代码演示
float具有三种属性:left || right || none
css
.a,.b,.c,.d,.e{
width: 200px;
height: 200px;
background-color: pink;
margin: 10px;
}
.a,.b,.c{
float: left;
}
.d,.e{
float: right;
}
html
<body>
<div class="a"></div>
<div class="b"></div>
<div class="c"></div>
<div class="d"></div>
<div class="e"></div>
</body>
3、浮动的特性
- 脱标:脱离标准流(可以理解为“浮在空中”)
- 不会保留原先位置;标准流会占用原先位置(此时会出现层叠);只会压住后面的标准流
- 浮动元素会统一上沿对齐
- 一行装不下时,自动换行
- 任何模式元素在添加浮动后,都会具有行内块元素相似的特性
4、浮动搭配标准流使用
css
.a,.b,.c{
width: 250px;
height: 95px;
background-color: pink;
margin: 30px;
}
.d,.e,.f,.g{
width: 75px;
height: 95px;
background-color: hotpink;
margin: 30px;
}
.h{
width: 600px;
height: 95px;
background-color: orange;
margin: 30px;
}
.a,.b,.c{
float: left;
}
.d,.e{
float: left;
}
.f,.g{
float: right;
}
.e{
float: left;
}
.x,.y,.z{
width: 1100px;
height: 160px;
border: black solid;
margin: 20px;
}
html
<body>
<div class="x">
<div class="a">a</div>
<div class="b">b</div>
<div class="c">c</div>
</div>
<div class="y">
<div class="d">d</div>
<div class="e">e</div>
<div class="f">f</div>
<div class="g">g</div>
</div>
<div class="z">
<div class="h">h</div>
</div>
</body>
5、清除浮动clear: both;
(1)清除浮动有什么用
- 父盒子不需要确定高度(盒内元素会增加),但是浮动元素不占位置会导致父盒子高度为0,进而影响下方的标准流盒子
- 可以理解为“有家不能回”,因此需要用到以下方法避免
(2)【推荐】额外标签法(隔墙法)
css
.con{
width: 1190px;px;
margin: 30px auto;
border: black solid;
}
.a,.b,.c,.d,.e{
width: 215px;
height: 215px;
background-color: pink;
border-radius: 5px;
margin: 11px;
float: left;
}
.sta{
width: 1190px;
height: 325px;
margin: 30px auto;
border: black solid;
}
html
<body>
<div class="con">
<div class="a"></div>
<div class="b"></div>
<div class="c"></div>
<div class="d"></div>
<div class="e"></div>
<div style="clear: both;"></div>
</div>
<div class="sta"></div>
</body>
在子盒子后添加了一个空盒子(必须为块级元素!),并且 clear: both; 即可撑开盒子
(3)父盒子添加overflow: hidden;
css
.con{
width: 1190px;px;
margin: 30px auto;
border: black solid;
overflow: hidden;
}
.a,.b,.c,.d,.e{
width: 215px;
height: 215px;
background-color: pink;
border-radius: 5px;
margin: 11px;
float: left;
}
.sta{
width: 1190px;
height: 325px;
margin: 30px auto;
border: black solid;
}
html
<body>
<div class="con">
<div class="a"></div>
<div class="b"></div>
<div class="c"></div>
<div class="d"></div>
<div class="e"></div>
</div>
<div class="sta"></div>
</body>
(4)after伪元素清除浮动
css
.clearfix:after{
content: "";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.clearfix{
*zoom: 1;
}
/* 对需要清除浮动的父盒子进行上述操作即可 */
.con{
width: 1190px;px;
margin: 30px auto;
border: black solid;
overflow: hidden;
}
.a,.b,.c,.d,.e{
width: 215px;
height: 215px;
background-color: pink;
border-radius: 5px;
margin: 11px;
float: left;
}
.sta{
width: 1190px;
height: 325px;
margin: 30px auto;
border: black solid;
}
html
<body>
<div class="con clearfix">
<div class="a"></div>
<div class="b"></div>
<div class="c"></div>
<div class="d"></div>
<div class="e"></div>
</div>
<div class="sta"></div>
</body>
(5) 双伪元素清除浮动
css
.clearfix:before,.clearfix:after{
content: "";
display: table;
}
.clearfix:after{
clear: both;
}
.clearfix{
*zoom: 1;
}
/* 对需要清除浮动的父盒子进行上述操作即可 */
二、过渡
- 想要改变多个属性时加在“ ,”后面即可
- 改变所有属性时,选属性(transition中)时选“all”即可
- 谁做过渡给谁加
css
body{
background-color: #eae8eb;
}
.a{
width: 235px;
height: 300px;
background-color: #ffff;
margin: 50px auto;
/* transition: 过渡属性 过渡时间 速度曲线 开始时间(可省略), 改变多个属性加“ ,”; */
transition: box-shadow .5s ease;
}
.a:hover{
box-shadow: 16px 16px 10px -4px rgba(0, 0, 0, .3);
}
html
<body>
<div class="a"></div>
</body>
等我把怎么上传视频弄明白,再把代码示例展示出来~~
三、定位position
1、定位的作用
- 定位可以使盒子固定在盒子或页面中的任意位置
- 定位可以使盒子盖住别的盒子
- 标准流和浮动做不到的事情
2、定位的组成
-
定位 = 定位模式 + 边偏移
-
定位模式
-
边偏移
3、静态定位 static
- 默认值,无定位
- 没有脱离文档流
- 需要用margin来控制位置
4、相对定位 relative
- 相对于原来位置移动
- 不脱标,保留文档流中的原有位置(“人走了钱还在”)
- 常用于给绝对定位提供父盒子
- 用 left / top 给其设置偏移量
代码见下一部分(绝对定位),图中即“人走了钱还在”
5、绝对定位 absolute
- 没有父盒子 或 父盒子无定位 时,绝对定位以浏览器为准
- 如果父盒子有定位(静态/相对/绝对),则以最近一级有定位的父盒子为参考点移动
- 不再占有原来的位置(脱标)
- 口诀:子绝父相(轮播图翻页键原理)
- 【最常用的盒子水平居中方式】子绝父相,子盒子设置left: 50%; margin-left: 负盒子宽度一半;
css
body{
background-color: #f5f5f7;
}
.yeye{
width: 400px;
height: 500px;
border-radius: 18px;
padding: 30px;
background-color: #ffff;
box-shadow: 2px 4px 12px rgb(0 0 0 / 8%);
position: relative;
top: 30px;
left: 430px;
}
.baba{
width: 330px;
height: 105px;
border-radius: 18px;
background-color: palegoldenrod;
}
.son{
width: 315px;
height: 240px;
border-radius: 18px;
background-color: bisque;
position: absolute;
top: 200px;
left: 80px;
}
.sta{
width: 315px;
height: 240px;
border-radius: 18px;
background-color: #ffff;
box-shadow: 2px 4px 12px rgb(0 0 0 / 8%);
}
html
<body>
<div class="yeye">
<div class="baba">
<div class="son">
</div>
</div>
</div>
<div class="sta"></div>
</body>
依照代码可以看出:没有定位的baba盒子并没有作为son盒子的参考点,而是将上一级有定位的yeye盒子作为参考
6、固定定位 fixed
- 以浏览器的可视窗口(顾名思义,可以看到的部分)为参照点,跟父盒子无关
- 不随页面滚动而滚动
- 不再占有原来的位置(脱标),可以看做一种特殊的绝对定位
css
body{
background-color: #f5f5f7;
}
.banxin{
margin: 20px auto;
width: 900px;
height: 3000px;
border-radius: 18px;
padding: 30px;
background-color: #ffff;
box-shadow: 2px 4px 12px rgb(0 0 0 / 8%);
position: relative;
}
.dingbu{
width: 700px;
height: 20px;
text-align: center;
position: absolute;
left: 50%;
margin-left: -350px;
top: 10px;
}
.dibu{
width: 700px;
height: 20px;
text-align: center;
position: absolute;
left: 50%;
margin-left: -350px;
bottom: 10px;
}
.fanhui{
text-align: center;
width: 80px;
height: 70px;
position: fixed;
bottom: 150px;
right: 30px;
border-radius: 18px;
background-color: pink;
box-shadow: 2px 4px 12px rgb(0 0 0 / 8%);
}
html
<body>
<div class="banxin">
<div class="dingbu">顶部</div>
<div class="dibu">底部</div>
</div>
<div class="fanhui"><p>返回<br>顶部</p></div>
</body>
7、粘性定位 sticky(先跟随滚动,后固定在某个位置)
8、定位叠放次序 z-index
.box{ z-index: 1; }
- 默认值为auto,属性值数值越大,盒子叠放层越靠上
- 属性值相同,按书写顺序后来居上
- 数字后无单位
- 具有定位的盒子才有该属性
9、定位的特性
- 添加定位后,inline元素可直接添加宽高,block元素默认宽高为内容宽高(与浮动类似)
- 绝对定位 / 固定定位 会完全压住下方盒子,但浮动盒子不会压住标准流盒子中的文字(文字环绕效果)
四、动画 animation
1、动画的基本使用
- 先定义动画
- 再使用动画
2、定义动画
(1)动画序列@keyframes
- 0%(动画开始)和 100%(动画结束)等同于:“ from{ } ” 和 “ to{ } ”
- 以一种样式改变为另一种样式;任意多样式、任意多次数
css
@keyframes move{
/* 开始状态 */
0%{
transform: translateX(0px);
}
/* 结束状态 */
100%{
transform: translateX(100px);
}
}
(2)使用动画
- 调用的动画名必须与定义的一致
css
.box{
/* 调用动画 */
animation-name: move;
/* 动画持续时间 */
animation-duration: 1.5s;
}
2、动画相关属性
3、动画的简写【常用】
- animation: 调用的动画名称 - 持续时间 - 运动曲线 - 何时开始 - 播放次数 - 是否反向播放 - 起始或结束状态 ;
- (上面加粗的是必要属性)
- 播放次数的 infinite 属性为 无限循环
animation: name duration timing-function delay iteration-count direction fill-mode;
/* animation: 调用的动画名称 持续时间 运动曲线 何时开始 播放次数 是否反向播放 起始或结束状态; */
css
@keyframes move{
0%{
transform: translate(0,0);
}
25%{
transform: translate(300px,0), rotate(45deg);
}
50%{
transform: translate(300px,300px), rotate(45deg);
}
75%{
transform: translate(0,300px), rotate(45deg);
}
100%{
transform: translate(0,0), rotate(45deg);
}
}
.box{
animation: move 2.5s infinite;
}
此为代码样例,动画视频暂时还展示不出来我再学学~