欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!

 一、CSS权重CSS权重指的是样式的优先级,有两条或多条样式作用于一个元素,权重高的那条样式对元素起作用,权重相同的,后写的样式会覆盖前面写的样式。权重的等级可以把样式的应用方式分为几个等级,按照等级来计算权重1、!important,加在样式属性值后,权重值为 100002、内联样式,如:style=””,权重值为10003、ID选择器,如:#content,权重值为1004、类,伪类和属性选择器,如: content、:hover 权重值为105、标签选择器和伪元素选择器,如:div、p、:before 权重值为16、通用选择器(*)、子选择器(>)、相邻选择器(+)、同胞选择器(~)、权重值为0实例一<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>CSS权重</title><styletype="text/css">div{background-color: red!important;}</style></head><body><divstyle="background-color: blue">这是一个div</div></body></html> 两条样式同时作用一个div,上面的样式权重值为10000+1,下面的行间样式的权重值为1000, 所以文字的最终颜色为red 实例二<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>CSS权重</title><styletype="text/css">#content div.main_content h2{background-color: red;}#content .main_content h2{background-color: blue;}</style></head><body><divid="content"><divclass="main_content"><h2>这是一个div</h2></div></div></body></html> 第一条样式的权重计算: 100+1+10+1,结果为112; 第二条样式的权重计算: 100+10+1,结果为111; h2标题的最终颜色为red 二、CSS3新增选择器1、E:nth-child(n):匹配元素类型为E且是父元素的第n个子元素<style type="text/css"> .list div:nth-child(2){ background-color:red; } </style> ...... <div> <h2>1</h2> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> <!-- 第2个子元素div匹配 --> 2、E:nth-last-child(n):匹配元素类型为E且是父元素的倒数第n个子元素(与上一项顺序相反)3、E:first-child:匹配元素类型为E且是父元素的第一个子元素4、E:last-child:匹配元素类型为E且是父元素的最后一个子元素5、E:only-child:匹配元素类型为E且是父元素中唯一的子元素6、E:nth-of-type(n):匹配父元素的第n个类型为E的子元素7、E:nth-last-of-type(n):匹配父元素的倒数第n个类型为E的子元素(与上一项顺序相反)8、E:first-of-type:匹配父元素的第一个类型为E的子元素9、E:last-of-type:匹配父元素的最后一个类型为E的子元素10、E:only-of-type:匹配父元素中唯一子元素是E的子元素11、E:empty 选择一个空的元素12、E:enabled 可用的表单控件13、E:disabled 失效的表单控件14、E:checked 选中的checkbox15、E:not(s) 不包含某元素<style type="text/css"> .list div:not(:nth-child(2)){ background-color:red; } </style> ...... <div> <h2>1</h2> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div> <!-- 第 3、4、5 子元素div匹配 --> 16、E:target 对应锚点的样式<style type="text/css"> h2:target{ color:red; } </style> ...... <a href="#tit01">标题一</a> ...... <h2 id="tit01">标题一</h2> <!-- 点击链接,h2标题变红 --> 17、E > F E元素下面第一层子集18、E ~ F E元素后面的兄弟元素19、E + F 紧挨着的兄弟元素属性选择器:1、E[data-attr] 含有data-attr属性的元素<style type="text/css"> div[data-attr='ok']{ color:red; } </style> ...... <div data-attr="ok">这是一个div元素</div> <!-- 点击链接,h2标题变红 --> 2、E[data-attr='ok'] 含有data-attr属性的元素且它的值为“ok”3、E[data-attr^='ok'] 含有data-attr属性的元素且它的值的开头含有“ok”4、E[data-attr$='ok'] 含有data-attr属性的元素且它的值的结尾含有“ok”5、E[data-attr*='ok'] 含有data-attr属性的元素且它的值中含有“ok”三、CSS3圆角、阴影、rgbaCSS3圆角设置某一个角的圆角,比如设置左上角的圆角:border-top-left-radius:30px 60px;同时分别设置四个角: border-radius:30px 60px 120px 150px;设置四个圆角相同:border-radius:50%;CSS3阴影box-shadow:h-shadow v-shadow blur spread color inset;分别设置阴影:水平偏移 垂直偏移 羽化大小 扩展大小 颜色 是否内阴影<style type="text/css"> .box{ width:200px; height:50px; background-color:gold; /* box-shadow:10px 10px 5px 2px pink inset; */ box-shadow:10px 10px 5px 2px pink; } </style> ...... <div></div> <!-- 给盒子加上了粉红色的阴影 --> rgba(新的颜色值表示法)1、盒子透明度表示法:opacity:0.1;filter:alpha(opacity=10)(兼容IE);2、rgba(0,0,0,0.1) 前三个数值表示颜色,第四个数值表示颜色的透明度四、CSS3 transition动画1、transition-property 设置过渡的属性,比如:width height background-color2、transition-duration 设置过渡的时间,比如:1s 500ms3、transition-timing-function 设置过渡的运动方式

  • linear 匀速

  • ease 开始和结束慢速

  • ease-in 开始是慢速

  • ease-out 结束时慢速

  • ease-in-out 开始和结束时慢速

  • cubic-bezier(n,n,n,n)

  • 比如:cubic-bezier(0.845, -0.375, 0.215, 1.335)

  • 曲线设置网站:https://matthewlein.com/ceaser/

4、transition-delay 设置动画的延迟5、transition: property duration timing-function delay 同时设置四个属性举例:<style type="text/css"> .box{ width:100px; height:100px; background-color:gold; transition:width 300ms ease,height 300ms ease 300ms,background-color 300ms ease 600ms; } .box:hover{ width:300px; height:300px; background-color:red; } </style> ...... <div></div>五、CSS3 transform变换1、translate(x,y) 设置盒子位移2、scale(x,y) 设置盒子缩放3、rotate(deg) 设置盒子旋转4、skew(x-angle,y-angle) 设置盒子斜切5、perspective 设置透视距离6、transform-style flat | preserve-3d 设置盒子是否按3d空间显示7、translateX、translateY、translateZ 设置三维移动8、rotateX、rotateY、rotateZ 设置三维旋转9、scaleX、scaleY、scaleZ 设置三维缩放10、tranform-origin 设置变形的中心点11、backface-visibility 设置盒子背面是否可见举例:(翻面效果)<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>翻面</title> <style type="text/css"> .box{ width:300px; height:272px; margin:50px auto 0; transform-style:preserve-3d; position:relative; } .box .pic{ width:300px; height:272px; position:absolute; background-color:cyan; left:0; top:0; transform:perspective(800px) rotateY(0deg); backface-visibility:hidden; transition:all 500ms ease; } .box .back_info{ width:300px; height:272px; text-align:center; line-height:272px; background-color:gold; position:absolute; left:0; top:0; transform:rotateY(180deg); backface-visibility:hidden; transition:all 500ms ease; } .box:hover .pic{ transform:perspective(800px) rotateY(180deg); } .box:hover .back_info{ transform:perspective(800px) rotateY(0deg); } </style> </head> <body> <div> <div><img src="images/location_bg.jpg"></div> <div>背面文字说明</div> </div> </body> </html>六、CSS3 animation动画1、@keyframes 定义关键帧动画2、animation-name 动画名称3、animation-duration 动画时间4、animation-timing-function 动画曲线

  • linear 匀速

  • ease 开始和结束慢速

  • ease-in 开始是慢速

  • ease-out 结束时慢速

  • ease-in-out 开始和结束时慢速

  • steps 动画步数

5、animation-delay 动画延迟6、animation-iteration-count 动画播放次数 n|infinite7、animation-direction

  • normal 默认动画结束不返回

  • Alternate 动画结束后返回

8、animation-play-state 动画状态

  • paused 停止

  • running 运动

9、animation-fill-mode 动画前后的状态

  • none 不改变默认行为

  • forwards 当动画完成后,保持最后一个属性值(在最后一个关键帧中定义)

  • backwards 在 animation-delay 所指定的一段时间内,在动画显示之前,应用开始属性值(在第一个关键帧中定义)

  • both 向前和向后填充模式都被应用

10、animation:name duration timing-function delay iteration-count direction;同时设置多个属性举例:(人物走路动画)<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>走路动画</title> <style type="text/css"> .box{ width:120px; height:180px; border:1px solid #ccc; margin:50px auto 0; position:relative; overflow:hidden; } .box img{ display:block; width:960px; height:182px; position: absolute; left:0; top:0; animation:walking 1.0s steps(8) infinite; } @keyframes walking{ from{ left:0px; } to{ left:-960px; } } </style> </head> <body> <div><img src="images/walking.png"></div> </body> </html>

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/css3-style/c50725.shtml