12.3css选择器

本文详细讲解了CSS选择器中的交集选择器、并集选择器,以及相邻兄弟选择器、通用兄弟选择器和伪类选择器hover、first和before/after的用法,包括实际示例和应用场景。

一,交集选择器与并集选择器

交集选择器就是在两个标签相交的部分,也就是交集进行修改格式。
对于交集选择器,可以与id和class共同使用。
格式:
标签1标签2
{
       属性;

}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>交集选择器</title>
    <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>
</body>
</html>

与交集选择器不同,并集选择器是对两个标签同时进行修改样式。
并集选择器同样可以与id和class同时使用。
格式:
标签1,标签2
{
     属性;
}

!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>并集选择器</title>
    <style>
        div,p.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>
</body>
</html>

二、相邻兄弟选择器与通用的兄弟选择器
相邻兄弟选择器通过+相连,只能选中紧跟其后的那个标签,不能选择隔开的标签。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>相邻兄弟选择器</title>
    <style>
        div+p{
            color:red;
        }
    </style>
</head>
<body>
<!--选择div相邻的p标签,只会选择他之后且紧挨着的-->
<p>《凤求凰》</p>
<div>
<p>有一美人兮,见之不忘</p>
<p>一日不见兮,思之如狂</p>
<p>凤兮凤兮归故乡</p>
<p>遨游四海求其凰</p>
</div>
<p>司马相如</p>
<p>唐朝</p>
</body>
</html>

通用的兄弟选择器之间用~连接,其选中的是所有由选择器1指定的兄弟标签

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>通用的兄弟选择器</title>
    <style>
        div~p{
            color:red;
        }
    </style>
</head>
<body>
<!--选择div所有的兄弟标签,会选择后面所有的标签-->
<p>《凤求凰》</p>
<div>
    <p>有一美人兮,见之不忘</p>
    <p>一日不见兮,思之如狂</p>
    <p>凤兮凤兮归故乡</p>
    <p>遨游四海求其凰</p>
</div>
<p>司马相如</p>
<p>唐朝</p>

</body>
</html>

三、伪类选择器之hover/first/beforeafter
hover 一般用于链接中,当把鼠标悬浮在带有该选择器的元素或者链接上时触发特定效果
我们可以通过设置 :hover
达到当鼠标移动到第一个链接上的时候第二个链接会变颜色

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>伪类选择器之hover</title>
    <style>
        .box{
            width:100px;
            height:100px;
            border:1px solid #000000;
        }
        .box:hover{
            cursor:pointer;
            background-color: red;
        }
    </style>
</head>
<body>
<!--鼠标变为小手,背景为红色,hover为自动触发效果,需要搭配其他选择器一起使用-->
<div class="box"></div>
</body>
</html>

 first向下逐一降级,进行等级排序

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>伪类选择器之first</title>
    <style>
        ul>li{
            list-style:none;}/*去掉li前面的小黑点*/
        li:first-child{
            color:red;
        }
        li:last-child{
            color:blue;/*选择第一个与最后一个*/
        }
        li:first-child+li{
            color:green;
        }
        li:nth-child(3){
            color:gold;/*可以指定子节点*/
        }

before 向选定的元素前插入内容
after 向选定的元素后插入内容
一般使用是,类或者id::after , 类或者id::before
after是在类的后面处理逻辑
before 是在先的前面处理逻辑
它们 都有一个共同的元素content 可以在里面定义要添加的内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>伪类选择器之beforeandafter</title>
    <style>
        .box{
            width:500px;
            height:500px;
            border:1px solid #000000;
            position:relative;/*相对定位*/

        }
        .box:before{
            content:"";
            width:500px;
            height:500px;
            border-top:1px solid red;/*上边线*/
            border-left:1px solid red;/*左边线*/
            position:absolute;/*绝对定位*/
            left:-1px;
            top:-1px;

        }
        .box:after{
            content:"";
            width:500px;
            height:500px;
            border-right:1px solid red;/*右边线*/
            border-bottom:1px solid red;/*下边线*/
            position:absolute;/*绝对定位*/
            right :-1px;
            bottom:-1px;
        }
    </style>

四、通配符选择器(定位)
配符选择器用于同时选择或选择各种元素。 该选择器将帮助选择相似类型的类名称,名称或属性,并使它们CSS属性可用。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>定位</title>
    <style>
       /* #box{
            width:100%;
            height:20px;
            background-color: gold;
        }*/
        .top{
            width:100%;
            height:50px;
            background-color: red;
            position:relative ;
            top:10px;
           /*display:none;表示隐藏*/
        }
        .center{
            width:400px;
            height:100px;
            background-color: black;
            position: absolute;
            left:25%
            top:25%
            /*transform:translate(-50%,-50%);其作用用于移动位置*/
        }
        .bottom{
            width:100%
            height:50px;
            background-color: green;
            position: fixed;
            left:40%;
            bottom:40%;
        }
        body{
            height:2000px;
        }
        /*通配符选择器,会匹配页面中所有的元素*/
    </style>

二、具体任务(基于上周 4 个 HTML 页面:index.html、case.html、risk.html、profile.html) 任务 1:@import与link区别实践(20 分钟) 任务内容 创建多外部 CSS 文件,分别用link@import引入,观察加载差异,结合学号后三位设置基础样式。 操作步骤 1. 创建 CSS 文件: ◦ 新建base.css:定义平台统一样式(如body的字体"Microsoft YaHei", sans-serif); ◦ 新建color.css:用学号后三位(记为S1S2S3,如学号后三位为 123,则S1=1、S2=2、S3=3)设置颜色: /* color.css 个性化样式 */ body { color: #S1S2S3; /* 如123则为#123,若S1=0则为#0S2S3(例056→#056) */ } h1 { color: #S1+50S2+30S3+20; /* 数值超255则取255,如S1=220→220+50=270→取255 */ } ◦ 新建layout.css:预留盒模型相关样式(后续任务 3 使用)。 引入方式实现:(引入路径:根据同学项目实际情况,用相对路径) ◦ 在index.html中,用link标签引入base.csscolor.css: <link rel="stylesheet" href="base.css"> <link rel="stylesheet" href="color.css"> ◦ 在case.html中,用@import在内部样式表中引入base.csslayout.css: <style> @import url("base.css"); @import url("layout.css"); </style> 观察记录:打开浏览器开发者工具(F12),在「Network」面板查看两种方式的 CSS 加载顺序,记录差异(如link行加载、@import依赖加载)。 个性化要求 所有颜色值必须包含学号后三位衍生值,禁止直接使用固定色值(如#333)。 任务 2:选择器与引用方式优先级验证(25 分钟) 任务内容 覆盖 5 类选择器(通配、标签、类、id、父子)与 3 种引用方式,测试优先级,用学号后三位标记样式唯一性。 操作步骤 1. 定义选择器样式(在base.css中补充,结合学号后三位S1S2S3): /* 1. 通配选择器 */ * { margin: 0; padding: S1px; /* 如S1=5→padding:5px */ } /* 2. 标签选择器 */ p { font-size: 14px; color: #S1S2S3; } /* 3.选择器 */ .text-normal { font-size: 16px; color: #S1+20S2+30S3+10; } /* 4. id选择器 */ #risk-title { font-size: 18px; color: #S1+40S2+50S3+20; } /* 5. 父子选择器 */ div .risk-item { font-size: 15px; color: #S1+60S2+70S3+30; } 1. 叠加引用方式: ◦ 外部样式表:上述base.css(已通过任务 1 引入); ◦ 内部样式表:在risk.html的<style>中重定义.text-normal: <style> .text-normal { color: #S1+80S2+90S3+40; } /* 与外部类选择器对比 */ </style> ◦ 行内样式:在risk.html的某个<p>标签中添加行内样式: <p style="color: #S1+100S2+110S3+50;">测试行内样式优先级</p> 测试记录:在浏览器中打开risk.html,观察不同选择器 + 引用方式的文字颜色 / 字体大小,填写下表(实验记录必含): 选择器类型 引用方式 定义的样式值(含学号后三位) 浏览器实际显示效果 优先级排序(0 最低) 通配选择器 外部 padding:S1px 标签选择器 外部 color:#S1S2S3选择器 外部 / 内部 外部:#S1+20…;内部:#S1+80… id 选择器 外部 color:#S1+40… 父子选择器 外部 color:#S1+60… 类选择器 行内 color:#S1+100… 关键提示 优先级规则参考:行内样式(1000)>id 选择器(100)> 类 / 伪类 / 属性选择器(10)> 标签 / 伪元素选择器(1)> 通配选择器(0);引用方式优先级:行内 > 内部 > 外部(同选择器权重时)。 任务 3:伪类应用(link/visited/hover/active)(20 分钟) 任务内容 在导航链接中应用 4 个链接伪类,验证 “LVHA”(link→visited→hover→active)顺序的重要性,结合学号后三位设置颜色。 操作步骤 1. 在base.css中定义伪类样式(按正确顺序): /* 学号后三位S1S2S3 */ a:link { color: #S1S2S3; text-decoration: none; } /* 未访问 */ a:visited { color: #S1+30S2+40S3+20; } /* 已访问 */ a:hover { color: #S1+60S2+70S3+40; text-decoration: underline; } /* 鼠标悬浮 */ a:active { color: #S1+90S2+100S3+60; } /* 点击瞬间 */
最新发布
10-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Smiling Mr. Rui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值