• HTML5和CSS3五CSS3二


    代码下载地址

    属性选择器

    根据属性值来查找元素的方式:

    • E[attr]:查找有attr属性的E标签
    • E[attr=value]:查找有attr属性为value的E标签,严格匹配
    • E[attr*=value]:查找有attr属性值包含value的E标签
    • E[attr^=value]:查找有attr属性值以value开头的E标签
    • E[attr$=value]:查找有attr属性值以value结尾的E标签
            li[style] {
                text-decoration: underline;
            }
            li[class=red] {
                font-size: 30px;
            }
            li[class*=red] {
                background-color: cyan;
            }
            li[class^=red] {
                border: 1px solid black;
            }
            li[class$=red] {
                padding-top: 10px;
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    伪类选择器

    以某元素相对于其父元素或兄弟元素的位置来获取元素的结构伪类。

    相对于父元素结构伪类

    • E:first-child——E元素的父元素中的第一个子元素(并且这个元素必须为E元素,意思就是只看第一个子元素是不是E元素)
    • E:last-child——E元素的父元素中的最后一个子元素(同上)
    • E:nth-child(从1开始的索引|关键字|表达式)——E元素的父元素中的子元素中查找(同上)
    • E:empty——E元素的父元素中的空值E元素(同上)
    • E:first-of-type——E元素的父元素中的第一个子E元素(在指定类型中查找)
    • E:last-of-type——E元素的父元素中的最后一个子E元素(同上)
    • E:nth-last-of-type(从1开始的索引|关键字|表达式)——E元素的父元素中子E元素中查找(同上)
            li:first-child {
                color: red;
            }
            li:last-child {
                color: skyblue;
            }
            li:first-of-type {
                color: red;
            }
            li:last-of-type {
                background-color: pink;
            }
            li:nth-child(3) {
                text-decoration: underline;
            }
            /*even:偶数,odd奇数*/
            /*li:nth-child(even) {
                background-color: green;
            }
            li:nth-child(odd) {
                background-color: orange;
            }*/
            li:nth-last-of-type(even) {
                background-color: green;
            }
            li:nth-last-of-type(odd) {
                background-color: orange;
            }
            /*取值范围是0~子元素长度,小于1的值无效,如下表示前5个和后5个:*/
            li:nth-of-type(-n+5) {
                font-size: 30px;
            }
            li:nth-last-of-type(-n+5) {
                text-decoration: underline;
            }
            li:empty {
                background-color: yellow;
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    相对于兄弟元素结构伪类

    • .class ~ E——class类型元素的所有兄弟E元素
    • .class + E——class类型元素的下一个兄弟E元素
            .one ~ p {
                color: cyan;
            }
            .two + p {
                background-color: red;
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    target样式伪类选择器

    E:target——为锚点目标元素添加样式,当目标元素触发为当前锚链接时调用此伪类样式:

            h3:target {
                color: red;
            }
    
    • 1
    • 2
    • 3

    伪元素

    形如E::before、E::after的伪元素:

    • 伪元素是一个行内元素,要设置宽高则需要转化为块:display:block,float:*,position:,一般都是使用position。
    • 伪元素必须设置content,即使不设置内容,也需要设置content。
    • E:before、E:after在旧版本里是伪类,在新版本里是伪元素,新版下E:before、E:after会自动识别为E::before、E::after按伪元素来对待,这样做的目的是做兼容处理。
    • E::before定义在元素的内容之前插入content属性定义的内容与样式。
    • E::after定义在元素的内容之后插入content属性定义的内容与样式。

    注意:

    • IE6、IE7、IE8(怪异模式Quriks mode)不支持此伪元素。
    • CSS2中E:before、E:after是属于伪类,不存在伪元素概念。
    • CSS2中提出伪元素概念并将E:before、E:after归属于伪元素,伪类就不存在E:before、E:after。
    • dom元素中是不存在伪元素的,所以无法通过js操作伪元素。
            div.blue:nth-of-type(2):before {
                content: "";
                width: 20px;
                height: 20px;
                position: absolute;
                border-radius: 10px;
                background-color: #cae8ca;
                left: -10px;
                top: -10px;
            }
            div.blue:nth-of-type(2):after {
                content: "";
                width: 20px;
                height: 20px;
                position: absolute;
                border-radius: 10px;
                background-color: #cae8ca;
                left: -10px;
                bottom: -10px;
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    其他伪元素

    • E:first-letter——文本的第一个字母或字(不是词组)
    • E:first-line——文本第一行(如果设置了first-letter,无法同时设置它的样式)
    • E:selection——选中的文本(如果设置了first-letter,无法同时设置它的样式;并且只能设置显示样式,不能设置内容大小)
            /*获取第一个字符,实现首字下沉效果*/
            p.title::first-letter {
                font-size: 40px;
                /*文本环绕*/
                float: left;
            }
            p.content::first-letter {
                color: red;
            }
            /*获取第一行内容,如果设置了first-letter*/
            p.content::first-line {
                text-decoration: underline;
            }
            p.content::selection {
                background-color: #cccccc;
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    颜色与文本阴影

    • 预设颜色值如red、green、blue……
    • RGB/RGBA颜色值如#000000、#FFFFFF99……
    • rgb/rgba颜色函数,参数分别表示红、绿、蓝、不透明度这4个通道的值,红、绿、蓝通道取值为0255,不透明度通道为01。
    • hsl/hsla颜色函数,参数分别表示色相(0360)、饱和度(0100%)、明度(0100%均衡值为50%)、不透明度(01)。
            li.color1 {
                color: red;
            }
            li.color2 {
                color: #ff0000;
                background-color: #00ffff6e;
            }
            li.color3 {
                color: rgb(255, 0, 0);
                background-color: rgba(0, 255, 255, 0.5);
            }
            li.color4 {
                color: hsl(0, 100%, 50%);
                background-color: hsla(180, 100%, 50%, 0.5);
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    透明度说明:

    • opacity针对整个盒子透明,子会继承父的透明度
    • 预设transparent颜色值不可调节,始终完全透明
    • rgba/hsla颜色函数,相对于来说不具有继承性

    文本阴影如下表示:
    text-shadow: x轴偏移 y轴偏移 模糊半径 颜色, x轴偏移 y轴偏移 模糊半径 颜色……
    或者如下表示:
    text-shadow:颜色 x轴偏移 y轴偏移 模糊半径 , 颜色 x轴偏移 y轴偏移 模糊半径……

            div.shadow1 {
                color: white;
                text-shadow: -1px -1px 2px red;
            }
            div.shadow2 {
                color: white;
                text-shadow: 0px 0px 10px white;
            }
            div.shadow3 {
                color: black;
                text-shadow: 0px 0px 5px white, 0px 0px 10px red;
            }
            div.shadow4 {
                color: black;
                text-shadow: 0px 1px 1px white;
            }
            div.shadow5 {
                color: white;
                text-shadow: -1px -1px 0px #eeeeee, -2px -2px 0px #cccccc, -3px -3px 0px #bbbbbb, -4px -4px 0px #aaaaaa;
            }
            div.shadow6 {
                color: white;
                text-shadow: 0px -1px 0px #eeeeee, 0px -2px 0px #cccccc, 0px -3px 0px #bbbbbb, 0px -4px 0px #aaaaaa;
            }
            div.shadow7 {
                color: transparent;
                text-shadow: 0px 0px 5px hsl(20, 100%, 50%);
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    盒模型

    默认情况下,css设置盒子的宽高仅仅是内容的宽高,真正的宽高会加上一些其他内容:

    • 宽度=padding+border+width
    • 高度=padding+border+height

    css3中box-sizing来指定盒模型content-box、border-box改变计算盒子大小的方式:

    • content-box的实际宽高需加上padding和border
    • border-box的实际宽高就包括padding和border

    盒模型对比:

            div.div1 {
                width: 100px;
                height: 100px;
                background-color: red;
                float: left;
                padding-right: 10px;
                border-right: 10px solid pink;
            }
            div.div2 {
                width: 100px;
                height: 100px;
                background-color: green;
                float: left;
            }
            div.div3 {
                width: 100px;
                height: 100px;
                background-color: blue;
                float: left;
                padding-right: 10px;
                border-right: 10px solid yellow;
                box-sizing: border-box;
            }
            div.div4 {
                width: 100px;
                height: 100px;
                background-color: cyan;
                float: left;
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    盒模型案例,鼠标放上去加一个边框:

            div.item {
                width: 30%;
                height: 100px;
                background-color: red;
                display: inline-block;
                overflow: hidden;
                margin-left: 2.5%;
                box-sizing: border-box;
            }
    
            /*鼠标往上移的时候触发*/
            div.item:hover {
                border: 5px cyan solid;
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    边框圆角

    border-radius定义样式相同的角,也能为每个角分别定义:

    • border-radius:*px创建4个半径大小一样的圆角
    • border-radius:*px *px *px *px分别为左上、右上、右下、左下顺时针方向创建4个圆角
    • border-radius:*px *px第一个值为左上、右下对角线方向创建2个圆角,第二个值为右上、左下对角线方向创建2个圆角
    • border-radius:*px *px *px分别为左上、右上、右下顺时针方向创建3个圆角
    • border-radius:*px/*px创建4不同方向半径值的大小一样的圆角
    • border-radius:*px *px *px *px/*px *px *px *px分别创建4不同方向半径值的大小不一样的圆角,分别表示水平方向的左上、右上、右下、左下圆角半径值和垂直方向的左上、右上、右下、左下圆角半径值
            div.div1 {
                border-radius: 10px;
            }
            div.div2 {
                border-radius: 10px 20px 30px 40px;
            }
            div.div3 {
                border-radius: 10px 20px;
            }
            div.div4 {
                border-radius: 10px 20px 30px;
            }
            div.div5 {
                border-radius: 10px/20px;
            }
            div.div6 {
                border-radius: 10px 20px 30px 40px/50px 60px 70px 80px;
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    除了上述设置4个角外,还可以为每个角单独创建圆角:

    • border-top-left-radius/border-top-right-radius/border-bottom-left-radius/border-bottom-right-radius:*px为每个角单独创建圆角
    • border-top-left-radius/border-top-right-radius/border-bottom-left-radius/border-bottom-right-radius:*px *px为每个角单独创建不同方向半径值的圆角
            div.div7 {
                border-top-left-radius: 10px;
            }
            div.div8 {
                border-top-right-radius: 10px;
            }
            div.div9 {
                border-bottom-left-radius: 10px;
            }
            div.div10 {
                border-bottom-right-radius: 10px;
            }
            div.div11 {
                border-bottom-right-radius: 10px 20px;
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    总结案例

    绘制安卓机器人,主要是对伪元素、边框圆角的运用:

    
    
        
        Title
    
        
    
    
    
    

    机器人案例

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
  • 相关阅读:
    中移OneOS开发板学习入门
    【数据结构】字符串匹配(kmp算法)
    web前端面试高频考点——Vue原理(理解MVVM模型、深度/监听data变化、监听数组变化、深入了解虚拟DOM)
    Java包装类
    Git --》Git与GitHub操作
    概率论的学习整理3: 概率的相关概念
    C# 多线程一: Thread 的简单理解与运用
    门店如何设置多个联系电话和营业时间
    边缘计算物联网网关在机械加工行业的应用及作用分享
    计算机毕业论文微信小程序毕业设计开题报告SSM美容预约+后台管理系统|前后分离VUE[包运行成功]
  • 原文地址:https://blog.csdn.net/jiuchabaikaishui/article/details/126857094