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;
        }

伪类选择器

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

相对于父元素结构伪类

  • 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;
        }

相对于兄弟元素结构伪类

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

target样式伪类选择器

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

        h3:target {
            color: red;
        }

伪元素

形如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;
        }

其他伪元素

  • 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;
        }

颜色与文本阴影

  • 预设颜色值如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);
        }

透明度说明:

  • 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%);
        }

盒模型

默认情况下,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;
        }

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

        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;
        }

边框圆角

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;
        }

除了上述设置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;
        }

总结案例

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

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        h2 {
            text-align: center;
        }
        div.content {
            width: 450px;
            height: 450px;
            border: 1px solid darkgray;
            margin: auto;
            background-color: blanchedalmond;
        }
        div.robot {
            background-color: green;
        }
        div.header {
            width: 200px;
            height: 100px;
            position: relative;
            margin-top: 15px;
            left: 125px;
            border-radius: 100px 100px 0px 0px;
        }
        div.header::before, div.header::after {
            content: "";
            position: absolute;
            width: 20px;
            height: 20px;
            background-color: white;
            border-radius: 10px;
            top: 50px;
        }
        div.header::before {
            left: 60px;
        }
        div.header::after {
            right: 60px;
        }
        div.body {
            width: 200px;
            height: 200px;
            position: relative;
            margin-top: 10px;
            left: 125px;
            border-radius: 0px 0px 20px 20px;
        }
        div.body::before, div.body::after {
            content: "";
            position: absolute;
            width: 24px;
            height: 120px;
            background-color: green;
            border-radius: 8px;
            top: 20px;
        }
        div.body::before {
            left: -35px;
        }
        div.body::after {
            right: -35px;
        }
        div.footer {
            width: 200px;
            height: 80px;
            position: relative;
            margin: auto;
            background-color: transparent;
        }
        div.footer::before, div.footer::after {
            content: "";
            position: absolute;
            width: 24px;
            height: 80px;
            background-color: green;
            border-radius: 0px 0px 8px 8px;
        }
        div.footer::before {
            left: 40px;
        }
        div.footer::after {
            right: 40px;
        }
    </style>
</head>
<body>

<h2>机器人案例</h2>
<div class="content">
    <div class="robot header"></div>
    <div class="robot body"></div>
    <div class="robot footer"></div>
</div>

</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值