主要内容:web
浏览器支持程度差,须要添加私有前缀chrome
类选择器、属性选择器、伪类选择器,权重为 10
代码演示浏览器
button { cursor: pointer; } button[disabled] { cursor: default }
input[type=search] { color: skyblue; } span[class^=black] { color: lightgreen; } span[class$=black] { color: lightsalmon; } span[class*=black] { color: lightseagreen; }
代码演示dom
ul li:first-child { background-color: lightseagreen; } ul li:last-child { background-color: lightcoral; } ul li:nth-child(3) { background-color: aqua; }
even
偶数、odd
奇数
代码演示动画
<style> /* 偶数 */ ul li:nth-child(even) { background-color: aquamarine; } /* 奇数 */ ul li:nth-child(odd) { background-color: blueviolet; } /*n 是公式,从 0 开始计算 */ ul li:nth-child(n) { background-color: lightcoral; } /* 偶数 */ ul li:nth-child(2n) { background-color: lightskyblue; } /* 奇数 */ ul li:nth-child(2n + 1) { background-color: lightsalmon; } /* 选择第 0 5 10 15, 应该怎么选 */ ul li:nth-child(5n) { background-color: orangered; } /* n + 5 就是从第5个开始日后选择 */ ul li:nth-child(n + 5) { background-color: peru; } /* -n + 5 前五个 */ ul li:nth-child(-n + 5) { background-color: tan; } </style>
nth-child
和 nt-of-type
的区别nth-child
选择父元素里面的第几个子元素,不论是第几个类型nt-of-type
选择指定类型的元素代码演示spa
<style> div :nth-child(1) { background-color: lightblue; } div :nth-child(2) { background-color: lightpink; } div span:nth-of-type(2) { background-color: lightseagreen; } div span:nth-of-type(3) { background-color: #fff; } </style>
content
属性行内元素
。看不见刚才建立的元素
,因此咱们称为伪元素权重为 1
代码演示firefox
<style> div { width: 100px; height: 100px; border: 1px solid lightcoral; } div::after, div::before { width: 20px; height: 50px; text-align: center; display: inline-block; } div::after { content: '德'; background-color: lightskyblue; } div::before { content: '道'; background-color: mediumaquamarine; } </style>
父元素添加 overfloat:hidden
3d
使用::after伪元素清除浮动code
.box::after{ content:""; display:block; clear:both; visibility:hidden; } <div class=box> <div class\="big"\>big</div\> <div class\="small"\>small</div\> <!--<div class="clear">额外标签法</div>--> </div>
.box{ *zoom:1; }
.box:after,.box:before{ content: ""; display: table; } .box:after{ clear: both; } .box{ *zoom: 1; }
转换(transform)是CSS3中具备颠覆性的特征之一,能够实现元素的位移、旋转、变形、缩放。orm
2D移动是2D转换里面的一种功能,能够改变元素在页面中的位置,相似定位。
transform: translate(x,y); transform: translateX(n); transform: translateY(n);
不会影响到其余元素的位置
行内标签
没有效果div { background-color: lightseagreen; width: 200px; height: 100px; /* 平移 */ /* 水平垂直移动 100px */ /* transform: translate(100px, 100px); */ /* 水平移动 100px */ /* transform: translate(100px, 0) */ /* 垂直移动 100px */ /* transform: translate(0, 100px) */ /* 水平移动 100px */ /* transform: translateX(100px); */ /* 垂直移动 100px */ transform: translateY(100px) }
2D旋转指的是让元素在2维平面内顺时针旋转或者逆时针旋转。
transform:rotate(angle)
rotate
里面跟度数,单位是 deg
img:hover { transform: rotate(360deg) }
缩放,顾名思义,能够放大和缩小。 只要给元素添加上了这个属性就能控制它放大仍是缩小。
transform:scale(x,y);
* 动画是 `CSS3` 中最具颠覆性的特征之一,可经过设置多个节点来精确的控制一个或者一组动画,从而实现复杂的动画效果
* 先定义动画 * 在调用定义好的动画
@keyframes 动画名称 { 0% { width: 100px; } 100% { width: 200px } }
div { /\* 调用动画 \*/ animation-name: 动画名称; /\* 持续时间 \*/ animation-duration: 持续时间; }
* 0% 是动画的开始,100 % 是动画的完成,这样的规则就是动画序列 * 在 @keyframs 中规定某项 CSS 样式,就由建立当前样式逐渐改成新样式的动画效果 * 动画是使元素从一个样式逐渐变化为另外一个样式的效果,能够改变任意多的样式任意多的次数 * 用百分比来规定变化发生的时间,或用 `from` 和 `to`,等同于 0% 和 100%
<style\> div { width: 100px; height: 100px; background-color: aquamarine; animation-name: move; animation-duration: 0.5s; } @keyframes move{ 0% { transform: translate(0px) } 100% { transform: translate(500px, 0) } } </style\>
div { width: 100px; height: 100px; background-color: aquamarine; /\* 动画名称 \*/ animation-name: move; /\* 动画花费时长 \*/ animation-duration: 2s; /\* 动画速度曲线 \*/ animation-timing-function: ease-in-out; /\* 动画等待多长时间执行 \*/ animation-delay: 2s; /\* 规定动画播放次数 infinite: 无限循环 \*/ animation-iteration-count: infinite; /\* 是否逆行播放 \*/ animation-direction: alternate; /\* 动画结束以后的状态 \*/ animation-fill-mode: forwards; } div:hover { /\* 规定动画是否暂停或者播放 \*/ animation-play-state: paused; }
/\* animation: 动画名称 持续时间 运动曲线 什么时候开始 播放次数 是否反方向 起始与结束状态 \*/ animation: name duration timing-function delay iteration-count direction fill-mode
animation-paly-state
animation-paly-state: paused
; 常常和鼠标通过等其余配合使用animation-direction: alternate
animation-fill-mode: forwards
animation: move 2s linear 1s infinite alternate forwards;
animation-timing-function
: 规定动画的速度曲线,默认是ease
3D
转换特色3D
转换3D
转换知识要点3D
位移:translate3d(x, y, z)
3D
旋转:rotate3d(x, y, z)
perspctive
3D
呈现 transfrom-style
3D
移动 translate3d
3D
移动就是在 2D
移动的基础上多加了一个能够移动的方向,就是 z 轴方向transform: translateX(100px)
:仅仅是在 x 轴上移动transform: translateY(100px)
:仅仅是在 y 轴上移transform: translateZ(100px)
:仅仅是在 z 轴上移动transform: translate3d(x, y, z)
:其中x、y、z 分别指要移动的轴的方向的距离
transform: translate3d(x, y, z)
transform: translate3d(100px, 100px, 100px) /\* 注意:x, y, z 对应的值不能省略,不须要填写用 0 进行填充 \*/ transform: translate3d(100px, 100px, 0)
perspective
3D
效果须要透视(理解成 3D
物体投影的 2D
平面上)注意下方图片
body { perspective: 1000px; }
translateZ
translateZ
与 perspecitve
的区别perspecitve
给父级进行设置,translateZ
给 子元素进行设置不一样的大小3D
旋转rotateX
3D 旋转指可让元素在三维平面内沿着 x 轴、y 轴、z 轴 或者自定义轴进行旋转
transform: rotateX(45deg)
-- 沿着 x 轴正方向旋转 45 度transform: rotateY(45deg)
-- 沿着 y 轴正方向旋转 45 度transform: rotateZ(45deg)
-- 沿着 z 轴正方向旋转 45 度transform: rotate3d(x, y, z, 45deg)
-- 沿着自定义轴旋转 45 deg 为角度div { perspective: 300px; } img { display: block; margin: 100px auto; transition: all 1s; } img:hover { transform: rotateX(\-45deg) }
3D
旋转 rotateY
div { perspective: 500px; } img { display: block; margin: 100px auto; transition: all 1s; } img:hover { transform: rotateY(180deg) }
左手准则
3D
旋转 rotateZ
div { perspective: 500px; } img { display: block; margin: 100px auto; transition: all 1s; } img:hover { transform: rotateZ(180deg) }
rotate3d
transform: rotate3d(x, y, z, deg)
-- 沿着自定义轴旋转 deg 为角度x, y, z 表示旋转轴的矢量,是标识你是否但愿沿着该轴进行旋转,最后一个标识旋转的角度
transform: rotate3d(1, 1, 0, 180deg)
-- 沿着对角线旋转 45degtransform: rotate3d(1, 0, 0, 180deg)
-- 沿着 x 轴旋转 45degdiv { perspective: 500px; } img { display: block; margin: 100px auto; transition: all 1s; } img:hover { transform: rotate3d(1, 1, 0, 180deg) }
3D
呈现 transform-style
transform-style
transform-style: flat
表明子元素不开启 3D
立体空间,默认的transform-style: preserve-3d
子元素开启立体空间浏览器私有前缀是为了兼容老版本的写法,比较新版本的浏览器无须添加。
私有前缀
\-moz-border-radius: 10px; \-webkit-border-radius: 10px; \-o-border-radius: 10px; border-radius: 10px;