05-css选择器进阶及三大特性

1、选择器进阶

选择器说明使用场景举例
复合选择器根据HTML的嵌套关系,选择父元素 后代中 满足条件元素
1. 后代选择器:空格
1. 语法: 选择器1 选择器2{}
2. 后代选择器中可以有儿子,孙子,孙孙子
2. 子代选择器
1. 语法:选择器1>选择器2{}
区别:后代标签中只要满足选择器的2的标签都会设置效果,子代选择器只会设置的儿子标签.box a { color:red}
box的后代标签中 只要满足a标签,不管后代 后后代都会设置成red颜色
并集选择器同时选中多组标签,设置相同样式
1. 语法: 选择器1,选择器2
可以选择个标签设置相同演示
交集选择器选中页面中 同时满足 多个选择器的标签
1. 选择器1选择器2{css}
既有原则:
1. 找到页面中既能被1选中又能被选择器2选中的标签,设置样式
hover伪类选择器作用:选中鼠标悬停在元素上的状态设置样式
1. 语法 选择器:hover{css}
设置鼠标悬停样式

1、后代选择器

后代选择器 只要满足选择器2都会被设置css, 所以后代和后后代都会设置成红色

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    /* 
    1. 后代选择器 只要满足选择器2都会被设置css, 所以后代和后后代都会设置成红色
    */
    div a{
        color: red;
    }
</style>
<body>

    <div>
        <a href="">这是a标签</a>
        <li>
            <ol><a href="">第二个a标签</a></ol>
        </li>
    </div>
</body>
</html>

展示
image.png

2、子代选择器

子代选择器 只要满足选择器2只选择儿子层 不会向下寻找

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    /* 
    1. 子代选择器 只要满足选择器2只选择儿子层 不会向下寻找
    */
    div>a{
        color: red;
    }
</style>
<body>

    <div>
        <a href="">这是a标签</a>
        <li>
            <ol><a href="">第二个a标签</a></ol>
        </li>
    </div>
</body>
</html>

展示
image.png

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>
</head>
<style>
    /* 
    1. 可以给多个标签设置相同的样式
    */
    div,a{
        color: red;
    }
</style>
<body>

    <div>
        <a href="">这是a标签</a>
        <li>
            <ol><a href="">第二个a标签</a></ol>
        </li>
    </div>
</body>
</html>

展示
image.png

4、交集选择器

找同时满足多个选择器的元素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>

    a.A{
        color: red;
    }
</style>
<body>

    <div class="box">
        <a href="" class="A">这是a标签</a>
        <li>
            <ol><a href="">第二个a标签</a></ol>
        </li>
    </div>
</body>
</html>

展示
image.png

5、hover伪类选择器

选中鼠标悬停在元素上的状态

<!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>

    a:hover{
        color: red;
    }
</style>
</head>
<body>

    <div class="box">
        <a href="" class="A">这是a标签</a>
        <li>
            <ol><a href="">第二个a标签</a></ol>
        </li>
    </div>
</body>
</html>

展示
image.png

2、背景相关属性

属性说明备注
background-color设置背景颜色
background-image设置背景图片
background-repeat背景平铺
取值
1. repeat:水平和垂直方向都平铺
2. no-repeat 不平铺
3. repeat-x 沿着水平方向 x轴平铺
4. repeat-y 沿着水平方向y轴平铺
background-positionimage.png注意点:
方向名词取值和坐标取值时可以混用的,第一个取值表示水平方向,第二个取值表示垂直
背景属性连写举例
background:color image repeat position

3、元素的显示模式

属性说明备注
块元素div、p、h系列、ul、li、dl、dt、dd、form、header、nav、footer
行内元素a、span 、b、u、i、s、strong、ins、em、del
行内块元素input、textarea、button、select
元素显示模式转换image.png

4、水平居中方法居中

image.png

5、CSS特性

1、继承性

特性:子元素有默认继承父元素样式的特点(子承父业) 常见继承属性有

  1. coror
  2. font-style font-weight font-size font-family
  3. text-indent text-align
  4. 可以通过调试工具来判断是否继承
  5. 好处时可以在一定的程度上减少代码

2、层叠行

给同一个标签设置相同的样式 → 此时样式会层叠覆盖 → 最终写在最后的样式会生效

6、练习题

image.png

<!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>
        *{
            margin: 0;
            padding: 0;
        } *
        ul{
            list-style: none;
        }

         li{
            display: inline-block;
            width: 80px;
            height: 50px;
            background-color: #ff0000;
            line-height: 50px;

        }
        li:hover{
            background-color: #ffa500;
        }
        li a{
            color: black;


            text-decoration: none;
        }

    </style>
</head>
<body>
    <ul>
        <li><a href="">导航栏1</a></li>
        <li><a href="">导航栏2</a></li>
        <li><a href="">导航栏3</a></li>
        <li><a href="">导航栏4</a></li>

    </ul>
</body>
</html>

image.png

<!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>

        ul{
            list-style: none;
        }

         li{
            display: inline-block;
            width: 120px;
            height: 58px;
            background-color: #ff0000;
            line-height: 50px;

          
        }
        li a{
            color: black;
            text-decoration: none;
            display: inline-block;
            width: 120px;
            height: 58px;

            }
            .one{
                background-image: url(./images/bg1.png) ;
            }
            .two{
                background-image: url(./images/bg2.png) ;
            }
            .three{
                background-image: url(./images/bg3.png) ;
            }
            .four{
                background-image: url(./images/bg4.png) ;
            }
            .one:hover {
            background-image: url(./images/bg5.png);
        }

        .two:hover {
            background-image: url(./images/bg6.png);
        }

        .three:hover {
            background-image: url(./images/bg7.png);
        }

        .four:hover {
            background-image: url(./images/bg8.png);
        }

    </style>
</head>
<body>
    <ul>
        <li><a href="" class="one">导航栏1</a></li>
        <li><a href="" class="two">导航栏2</a></li>
        <li><a href="" class="three">导航栏3</a></li>
        <li><a href="" class="four">导航栏4</a></li>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值