一.元素的显示和隐藏
1.隐藏后不再保留位置
display:none;
隐藏后想要显示使用display:block;
2.隐藏后保留位置
visibibility:hidden;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.father {
width: 300px;
height: 300px;
margin: 0 auto;
border: 1px solid red;
}
.father div {
width: 100px;
height: 100px;
}
/* display: none;隐藏 display: block;显示*/
.son {
/* 元素的系那是和隐藏 隐藏之后不再保留位置(使用较多)*/
display: none;
background-color: blue;
/* 隐藏之后保留位置 */
/* visibility: hidden; */
}
.father:hover>.son {
display: block;
}
</style>
</head>
<body>
<div class="father">
<div class="son">son1</div>
</div>
</body>
</html>
二.结构性伪类选择器
1.对指定序号的元素设置样式
nth-child(n);参数可以是数字(1、2、3)
2.对基数或偶数
基数:odd
偶数:even
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 1.:nth-child(n) 对指定序号的子元素设置样式(从前往后),参数可以是数字(1、2、3) */
/* li:nth-child(3) {
color: red;
} */
/* 2.odd 基数 even 偶数 */
.nav li:nth-child(odd) {
color: blue;
}
.nav li:nth-child(even) {
color: yellow;
}
/* 3.2n+1 2n 4n 4n+1 n从0开始 */
.main p:nth-child(4) {
color: brown;
}
/* :nth-of-type() 匹配指定序号的同一类型的子元素 */
.main p:nth-of-type(4) {
color: coral;
}
</style>
</head>
<body>
<ul class="nav">
<li>li元素1</li>
<li>li元素2</li>
<li>li元素3</li>
<li>li元素4</li>
<li>li元素5</li>
<li>li元素6</li>
<li>li元素7</li>
<li>li元素8</li>
</ul>
<div class="main">
<span>你好呀</span>
<p>p元素1</p>
<p>p元素2</p>
<p>p元素3</p>
<p>p元素4</p>
<p>p元素5</p>
</div>
</body>
</html>
3.跳跃式
2n+1 2n 4n 4n+1 n从0开始
4.匹配指定序号的同一类型的子元素
:nth-of-type()
三.过渡(transition )
过渡(transition)是CSS3中具有颠覆性的特征之一,我们可以在不使用 Flash 动画或 JavaScript 的情况下,当元素从一种样式变换为另一种样式时为元素添加效果。
属性 | 描述 | CSS |
---|---|---|
transition | 简写属性,用于在一个属性中设置四个过渡属性。 | 3 |
transition-property | 规定应用过渡的 CSS 属性的名称。 | 3 |
transition-duration | 定义过渡效果花费的时间。默认是 0。 | 3 |
transition-timing-function | 规定过渡效果的时间曲线。默认是 "ease"。 | 3 |
transition-delay | 规定过渡效果何时开始。默认是 0。 | 3 |
如果想要所有的属性都变化过渡, 写一个all 就可以
transition-duration 花费时间 单位是 秒 s 比如 0.5s 这个s单位必须写 ms 毫秒
运动曲线 默认是 ease
何时开始 默认是 0s 立马开始
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 200px;
height: 100px;
background-color: pink;
/* 过渡 */
/* 要过度的属性 花费时间 运动曲线(默认 ease缓慢动画) 何时开始*/
/* 运动曲线 linear(匀速) ease(逐渐慢下来) ease-in(加速) */
/* transition: width 1s linear, height 1s; */
transition: all 2s ease-in;
}
.box:hover {
width: 1000px;
height: 500px;
background-color: chartreuse;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
四.2D变形
1.移动(translate(x, y) )
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 200px;
height: 100px;
background-color: pink;
/* 过渡 */
transition: all 2s;
margin: 100px auto;
}
.box:hover {
/* transform: translate(50px, 50px); */
transform: translateY(-10px);
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
2.缩放
注意: scale()的取值默认的值为1,当值设置为0.01到0.99之间的任何值,作用使一个元素缩小;而 任何大于1的值,作用是让元素放大。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 200px;
height: 100px;
background-color: pink;
/* 过渡 */
transition: all 2s;
margin: 500px auto;
}
.box:hover {
/* 缩小[0,1) 1大小不变 变大 大于一 */
transform: scale(0.5, -1);
/* 若果书写一个字值,那么x和y方向都是这个值 */
transform: scale(1.1);
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
3.旋转(rotate)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 200px;
height: 100px;
background-color: pink;
/* 过渡 */
transition: all 1s;
margin: 100px auto;
}
.box:hover {
/* 可以使元素旋转,正直顺时针西旋转 */
transform: rotate(-60deg);
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
五.动画
1.定义动画
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 元素移动 */
/* 1.transform :translatex()
2.margin-left
3.定位 */
@keyframes move {
/* 开始 */
from {
/* transform: translateX(0) */
left: 100px;
}
/* 结束 */
to {
/* transform: translateX(600px) */
left: 800px;
}
}
.car {
/* animation : 动画名称 运动时间 运动曲线 循环次数 */
/* infinite 无限 */
animation: move 1.5s linear infinite;
/* 规定运动之前的状态 */
/* 运动之后停留的位置 */
position: absolute;
left: 0;
top: 0;
}
</style>
</head>
<body>
<div class="car"><img src="img/zx.jpg" alt="" width="100px"></div>
</body>
</html>
2.调用动画
animation:动画名称 动画时间 运动曲线 何时开始 播放次数 是否反方向;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 元素移动 */
/* 1.transform :translatex()
2.margin-left
3.定位 */
@keyframes move {
0% {}
49% {
transform: translateX(1000px);
}
50% {
transform: translateX(1000px) rotateY(180deg);
}
99% {
transform: translateX(0px) rotateY(180deg);
}
100% {
transform: rotateY(0deg);
}
}
.car {
/* animation : 动画名称 运动时间 运动曲线 循环次数 */
/* infinite 无限 */
animation: move 5s linear infinite;
/* 规定运动之前的状态 */
position: absolute;
left: 0;
top: 0;
}
</style>
</head>
<body>
<div class="car"><img src="img/1.png" alt="" width="100px"></div>
</body>
</html>
六.传统布局和flex布局对比
1. 传统布局
-
兼容性好
-
布局繁琐
-
局限性,不能在移动端很好的布局
2.flex
-
操作方便,布局极其简单,移动端使用比较广泛
-
pc端浏览器支持情况比较差
-
IE11或更低版本不支持flex或仅支持部分
3.弹性布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.wrap {
width: 600px;
height: 200px;
border: 1px solid;
margin: 0 auto;
/* 使wrap元素父元素成为弹性盒子 */
/* 如果子元素宽度不够,将自动进行缩小
如果空间有剩余,不会自动变大,按自身大小进行显示 */
display: flex;
}
.wrap span {
width: 500px;
height: 150px;
background-color: pink;
margin-left: 10px;
}
</style>
</head>
<body>
<div class="wrap">
<!-- 对于子元素来说,什么样的子标签都是一样的 -->
<!-- <div class="item">item1</div>
<div class="item">item2</div>
<div class="item">item3</div>
<div class="item">item4</div>
<div class="item">item5</div>
<div class="item">item6</div> -->
<span>span1</span>
<span>span2</span>
<span>span3</span>
<span>span4</span>
<span>span5</span>
</div>
</body>
</html>
4.弹性盒子的方向
flex-direction属性决定主轴的方向(即项目的排列方向)。
它可能取有4个值:
-
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">
<title>Document</title>
<style>
.wrap {
width: 700px;
height: 500px;
border: 1px solid;
margin: 100px auto;
/* 设置父元素为弹性盒子 */
display: flex;
/* 主轴的方向 row水平 column垂直*/
flex-direction: column;
}
.wrap span {
width: 200px;
height: 150px;
background-color: pink;
margin-left: 10px;
}
</style>
</head>
<body>
<div class="wrap">
<span>span1</span>
<span>span2</span>
<span>span3</span>
<span>span4</span>
<span>span5</span>
<span>span5</span>
<span>span5</span>
<span>span5</span>
</div>
</body>
</html>
5.换行(flex-wrap属性 )
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.wrap {
width: 700px;
height: 500px;
border: 1px solid;
margin: 100px auto;
/* 设置父元素为弹性盒子 */
display: flex;
/* 主轴的方向 row水平 column垂直*/
flex-direction: flex;
flex-wrap: wrap;
}
.wrap span {
width: 200px;
height: 150px;
background-color: pink;
margin-left: 10px;
}
</style>
</head>
<body>
<div class="wrap">
<span>span1</span>
<span>span2</span>
<span>span3</span>
<span>span4</span>
<span>span5</span>
<span>span5</span>
<span>span5</span>
<span>span5</span>
<span>span5</span>
<span>span5</span>
</div>
</body>
</html>
6.主轴上的对齐方式
它可能取5个值,具体对齐方式与轴的方向有关。下面假设主轴为从左到右。
-
flex-start(默认值):左对齐
-
flex-end:右对齐
-
center: 居中
-
space-between:两端对齐,项目之间的间隔都相等。
-
space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.wrap {
width: 800px;
height: 200px;
border: 1px solid;
margin: 100px auto;
/* 设置父元素为弹性盒子 */
display: flex;
/* 主轴上的对齐方式 */
/* 默认:左对齐 */
justify-content: flex-start;
/* 右对齐 */
justify-content: flex-end;
/* 居中 */
justify-content: center;
/* 两端对齐 */
justify-content: space-between;
/* 两端相等对其 */
justify-content: space-around;
}
.wrap div {
width: 180px;
height: 200px;
background-color: pink;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div class="wrap">
<div class="item">item1</div>
<div class="item">item2</div>
<div class="item">item3</div>
<div class="item">item4</div>
</div>
</body>
</html>
7.交叉轴上的对齐方式
它可能取5个值。具体的对齐方式与交叉轴的方向有关,下面假设交叉轴从上到下。
-
flex-start:交叉轴的起点对齐。
-
flex-end:交叉轴的终点对齐。
-
center:交叉轴的中点对齐。
-
baseline: 项目的第一行文字的基线对齐。
-
stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度,元素被拉伸以适应容器。。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.wrap {
width: 800px;
height: 300px;
border: 1px solid;
margin: 100px auto;
/* 设置父元素为弹性盒子 */
display: flex;
justify-content: space-between;
/* 交叉轴的对齐方式 */
/* 居中 */
align-items: center;
}
.wrap div {
width: 180px;
height: 200px;
background-color: pink;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div class="wrap">
<div class="item">item1</div>
<div class="item">item2</div>
<div class="item">item3</div>
<div class="item">item4</div>
</div>
</body>
</html>
8.嵌套元素水平居中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.wrap {
width: 800px;
height: 300px;
border: 1px solid;
margin: 100px auto;
/* 设置父元素为弹性盒子 */
display: flex;
justify-content: center;
/* 交叉轴的对齐方式 */
/* 居中 */
align-items: center;
}
.wrap div {
width: 180px;
height: 200px;
background-color: pink;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div class="wrap">
<div class="item">item1</div>
</div>
</body>
</html>