CSS选择器

1.交集选择器

  <style>
        div.box {
            color:red;
        }
    </style>
</head>
<body>
<div>没有样式的div</div>
<p>
    <div class="box"></div>
</p>
<div class="box"></div>
<p class="box"></p>

2.并集选择器

<style>
        div,.box {
            color:red;
        }
    </style>
</head>
<body>
<div>没有样式的div</div>
<p>
<div class="box">在p标签中的div</div>
</p>
<div class="box">带有央视的div</div>
<p class="box">这是p标签的内容</p>

3.兄弟选择器

 <style>
        div + p{
            color:red;
        }
    </style>
</head>
<body>
<!--选择div相邻的p标签-->
<!--注意:兄弟相邻选择器,只会选择它之后并且紧挨着的-->
</body>

通用兄弟选择器

  <style>
        div ~ p {
            color:mediumvioletred;
        }
    </style>
</head>
<body>
<!--需求:选择div所有的兄弟选择器-->
<!--选择相邻一个用 + ,选择向坤所有用 ~ -->
<!-- ~ p表示相邻的所有p-->

4.伪类选择器

1)hover

 <style>
        .box {
            width:100px;
            height:100px;
            border:1px solid #000000;
        }
        .box:hover {
            cursor:pointer;/*指针鼠标*/
            background-color:red;
        }
    </style>
</head>
<body>
<!--需求:当鼠标移到div上时,背景颜色变成红色,同时鼠标指针变为小手-->
<!--
伪类选择器都是以:开头的选择器
:hover表示鼠标移到元素上时会自动触发的效果
伪类选择器需要配合其他选择器一起使用!!!
-->
</body>

2)first

 <style>
        .box {
            width:100px;
            height:100px;
            border:1px solid #000000;
        }
        .box:hover {
            cursor:pointer;/*指针鼠标*/
            background-color:red;
        }
    </style>
</head>
<body>
<!--需求:当鼠标移到div上时,背景颜色变成红色,同时鼠标指针变为小手-->
<!--
伪类选择器都是以:开头的选择器
:hover表示鼠标移到元素上时会自动触发的效果
伪类选择器需要配合其他选择器一起使用!!!
-->
</body>

3)before and after

 <style>
        .box {
            width:100px;
            height:100px;
            border:1px solid #000000;/*表示四个边都有,它们的顺序是上右下左*/
            position:relative;
        }
        .box:before {
            content:"";
            width:10px;
            height:10px;
            border-top:1px solid #a70ff3;/*上边线*/
            border-left:1px solid red;/*左边线*/
            position:absolute;/*绝对定位*/
            left:0;
            top:0;
        }
        .box:after {
            content:"";
            width:10px;
            height:10px;
            border-right:1px solid red;/*右边线*/
            border-bottom:1px solid red;/*下边线*/
            position:absolute;
            right:-1px;
            bottom:-1px;
        }
    </style>
</head>
<body>
<!--需求:在左上角和右下角各有一个红色的尖-->
<div class="box">肖战</div>
</body>

4)定位

 <style>
        .box {
            width:100px;
            height:100px;
            border:1px solid #000000;/*表示四个边都有,它们的顺序是上右下左*/
            position:relative;
        }
        .box:before {
            content:"";
            width:10px;
            height:10px;
            border-top:1px solid #a70ff3;/*上边线*/
            border-left:1px solid red;/*左边线*/
            position:absolute;/*绝对定位*/
            left:0;
            top:0;
        }
        .box:after {
            content:"";
            width:10px;
            height:10px;
            border-right:1px solid red;/*右边线*/
            border-bottom:1px solid red;/*下边线*/
            position:absolute;
            right:-1px;
            bottom:-1px;
        }
    </style>
</head>
<body>
<!--需求:在左上角和右下角各有一个红色的尖-->
<div class="box">肖战</div>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值