百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

CSS的选择器

myzbx 2025-01-17 12:23 32 浏览

基本选择器

类选择器 .

class属性的值与类选择器一一对应

ID选择器 #

id选择器的名字是唯一的,虽然两个一样的不报错,但不能那么写

通配符选择器 *

通配符选择器: 定位当前HTML页面中所有元素

* 应用: 用来设置当前HTML页面的默认样式

* 问题: 该选择器的性能不好

属性选择器 []

可以为拥有指定属性的 HTML 元素设置样式,而不仅限于 class 和 id 属性。


层级选择器

HTML元素之间的关系分为三种:

  • 父级与子级的关系:如下所示,<ul>元素是<li>元素的父级元素,那么<li>元素就是<ul>元素的子级元素。 <ul id="u1">
    <li id="l1"></li>
    </ul>
  • 兄弟之间的关系:如下所示,<ul>是两个<li>的父级,即两个<li>是<ul>的子级,那么两个<li>之间就是兄弟元素。 <ul id="u1">
    <li id="l1"></li>
    <li id="l2"></li>
    </ul>
  • 祖先与后代之间的关系:如下所示,<div>是<ul>和<li>元素的祖先元素,那么<ul>和<li>就是<div>的后代元素 <div>
    <ul id="u1">
    <li id="l1"></li>
    <li id="l2"></li>
    </ul>
    </div>

后代选择器

后代选择器包含该元素中所有包裹的元素 如下图所示

 
   <style>
     .C {
       width: 500px;
       height: 300px;
     }
     .C1 {
       width: 300px;
       height: 200px;
     }
     .C2 {
       width: 200px;
       height: 100px;
     }
     /* 
       定位的是 .ancestor 的后代为 div 的元素
       * 后代选择器包含该元素中所有包裹的元素
      */
     .C div {
       background-color: lightcoral;
     }
   </style>
 </head>
 
 <body>
   <div class="C">
     18级启嘉班
     <div class="C1">
       19级启嘉班
       <div class="C2">20级启嘉班</div>
     </div>
   </div>
 </body>

效果: C1和C2都是C的后代,所以C1和C2都有颜色



子级选择器

子级选择器 定位的是为子级的元素 如下所示:

 ​
   <style>
     .C {               /*C的颜色覆盖区域*/
       width: 500px;
       height: 30px;
     }
 ​
     .C1 {              /*C1的颜色覆盖区域*/
       width: 300px;
       height: 20px;
     }
 ​
     .C2 {               /*C2的颜色覆盖区域*/
       width: 200px;
       height: 20px;
     }
 ​
     /*
       定位的是 .C 的子级为 div 的元素
     */
     .ancestor>div {
       background-color: lightcoral;
     }
   </style>
 </head>
 ​
 <body>
   <div class="C">
     18级启嘉班
     <div class="C1">
       19级启嘉班
       <div class="C2">20级启嘉班</div>
     </div>
   </div>
 </body>

效果: C的子级是C1所以,C1的地方有颜色



相邻兄弟选择器

相邻兄弟选择器(只能找后面的div,不能找前面的)如下所示

 ​
   <style>
     .C {
       width: 500px;
       height: 20px;
     }
 ​
     .C1 {
       width: 300px;
       height: 20px;
     }
 ​
     .C2 {
       width: 200px;
       height: 20px;
     }
 ​
     /*
       定位的是 .C2 的子级为 div 的元素
     */
     .C2+div {
       background-color: lightcoral;
     }
   </style>
 </head>
 ​
 <body>
   <div class="C">
     18级启嘉班
     <div class="C1">
       19级启嘉班
       <div class="C2">20级启嘉班</div>
       <div class="C2">21级启嘉班</div>
       <div class="C2">22级启嘉班</div>
     </div>
   </div>
 </body>

效果如下: 相邻兄弟选择器只能找后面的,C2相邻的是C3,所以C3有颜色

普通兄弟选择器

普通兄弟选择器 (找到后面所有的兄弟)

   <style>
     .C {
       width: 500px;
       height: 20px;
     }
 ​
     .C1 {
       width: 300px;
       height: 20px;
     }
 ​
     .C2 {
       width: 200px;
       height: 20px;
     }
 ​
     
      /* 定位的是 .C3 的后面兄弟为 div 的元素 */
     .C3~div {
       background-color: lightcoral;
     }
   </style>
 </head>
 ​
 <body>
   <div class="C">
     18级启嘉班
     <div class="C1">
       19级启嘉班
       <div class="C2">20级启嘉班</div>
       <div class="C3">21级启嘉班</div>
       <div class="C4">22级启嘉班</div>
       <div class="C5">23级启嘉班</div>
       <div class="C6">24级启嘉班</div>
     </div>
   </div>
 </body>

效果:

并集选择器

给所有的选择器选中的标签设置属性。以","隔开,如下所示

 ​
   <style>
     /* 为 <h1> ~ <h4> 元素的文本内容设置相同颜色 */
     /* h1 {
       color: lightcoral;
     }
 ​
     h2 {
       color: lightcoral;
     }
 ​
     h3 {
       color: lightcoral;
     }
 ​
     h4 {
       color: lightcoral;
     }
     */
       
     /* 通过并集选择器进行改写 */
     h1,h2, h3, h4{
                 color: lightcoral;
         }
   </style>
 ​
 <body>
   <h1>标题一</h1>
   <h2>标题二</h2>
   <h3>标题三</h3>
   <h4>标题四</h4>
 </body>

效果:h1~h4颜色相同,可以用并集选择器集合起来


交集选择器

 <!DOCTYPE html>
 <html lang="en">
 ​
 <head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>交集选择器</title>
   <style>
     p {
       color: lightcoral;
     }
 ​
     .cls {
       color: lightskyblue;
     }
 ​
     /* 交集选择器 */
     p.cls {
       color: magenta;
     }
   </style>
 </head>
 ​
 <body>
   <p>18级启嘉班</p>
   <p class="cls">19级启嘉班</p>
   <p>20级启嘉班</p>
   <div class="cls">启嘉网</div>
 </body>
 ​
 </html>

效果:交集改变了19级启嘉班的颜色


下一节更新CSS的其他的选择器

相关推荐

首次被击毁!低调但先进的S-350,为何活得比韩国仿版差这么多?

【军武次位面】作者:乐乐2月18日,乌克兰军方网站发布了其前线炮兵侦察旅,在顿涅茨克地区攻击俄军S-350防空系统的现场视频。这也是这款地位独特的先进防空系统,第一次确认在战场上被摧毁——考虑到近三年...

Windows 10 LTSC 2021 vs 2019:哪个更适合你?

本内容来源于@什么值得买APP,观点仅代表作者本人|作者:闪电神龙微软近日发布了2024年11月份ISO镜像,包括Windows1124H2、Windows1022H2以及Server2025...

叛变投敌?俄军最先进隐身无人机S-70,在乌东上空被苏-57击落!

【军武次位面】作者:天狼2024年10月5日,乌克兰东部战区传来一条令人震惊的消息:一架俄罗斯最先进的隐身无人机S-70“猎人-B”在乌东上空被击落,令人意外的是,击落它的竟然是俄罗斯自己的战斗机。这...

自动驾驶车祸致1死1伤!特斯拉被判赔偿2.43亿美元

当地时间8月1日,美国佛罗里达州一个陪审团裁定,美国电动汽车制造商特斯拉应为2019年一辆配备自动驾驶系统的ModelS所致的致命车祸承担部分责任,并判令该公司向一名遇难女性的家属及一名伤者支付约2...

HP488DZ 无绳电锤钻(18V)牧田DTD156SFJ

HP488DZ无绳电锤钻(18V)牧田DTD156SFJHP488DZ无绳电锤钻(18V)HP488DZ特征HP488D是一款基于HP457D开发的无绳电锤,采用18V锂离子电池供电。其...

FJK-SJRFPZS防爆阀位行程开关级

解答常见误区在工业自动化和安全控制领域,FJK-SJRFPZS防爆阀位行程开关等级是一个关乎设备安全与运行效率的重要参数。许多用户在选择和应用这类开关时,可能对其等级分类存在一些误解。本文将通过通俗易...

China&#39;s PLA aerobatic team to perform in Thailand for 50th anniversary of bilateral diplomatic ties

TIANJIN,March2(Xinhua)--TheBayiAerobaticTeamoftheChinesePeople'sLiberationArmy(PLA)A...

JD.com Enters Travel and Hospitality With Supply Chain-Focused Strategy

TMTPOST--JD.comhasofficiallythrownitshatintoChina’sfiercelycompetitivetravelandhospita...

JD.com Drives Robotics Funding Frenzy With Investments in LimX Dynamics, Spirit AI, and EngineAI

TMTPOST--JD.comisdoublingdownonembodiedintelligence,catalyzinganewwaveoffundinginChi...

JD.com opens first JD Mall in Beijing, steps up offline retail push

bySongJiananJD.comhaslauncheditsfirstJDMALLinBeijing,expandingitsofflineretailfootpr...

JD.com&#39;s food delivery fleet tops 120,000 full-time riders

JD.com'sfull-timefooddeliveryfleethassurpassed120,000ridersandisexpectedtoexceed150,00...

China willing to share military equipment achievements with friendly countries: defense ministry

BEIJING,July8(Xinhua)--Chinahasalwaystakenaprudent,responsibleapproachtomilitaryexpor...

FJK-SJRFPZS防爆阀位行程开关等级

解答常见误区在工业自动化和安全控制领域,FJK-SJRFPZS防爆阀位行程开关等级是一个关乎设备安全与运行效率的重要参数。许多用户在选择和应用这类开关时,可能对其等级分类存在一些误解。本文将通过通俗易...

JD&#39;s 618 Festival Smashes Records as AI Powers Next-Gen Retail Engine

AsianFin–JD.com’s2025“618ShoppingFestival”wrappedupwithrecord-breakingmomentum,drivenby...

JD’s Food Delivery Blitz Shakes Meituan as Founder Wang Xing Vows to Win at All Costs

Credit:CFPAsianFin--JD.comInc.isturninguptheheatinChina'sfooddeliverywars,andfounder...