CSS选择器及优先级 总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/b954960630/article/details/79560590

一、优先级

  • 不同级别
    1. 在属性后面使用 !important 会覆盖页面内任何位置定义的元素样式。
    2. 作为style属性写在元素内的样式
    3. id选择器
    4. 类选择器
    5. 元素选择器
    6. 通配符选择器
    7. 浏览器自定义或继承

    总结排序:!important > 行内样式 > ID选择器 > 类选择器 > 元素 > 通配符 > 继承 > 浏览器默认属性

  • 同一级别
  • (1) 同一级别中后写的会覆盖先写的样式

    (2) 同一级别css引入方式不同,优先级不同
    总结排序:内联(行内)样式 > 内部样式表 > 外部样式表 > 导入样式(@import)。

    对于选择器优先级,还可以通过计算权重值来比较。(请参见:狠狠戳我

    例1、
    test.css文件

    #my{background:blue;}

    html文件

    <link rel="stylesheet" type="text/css" href="./css/test.css">
    <style>
    /*这里是内部样式*/
    #my{background:red;}
    </style>
    
    <span id="my">333</span>

    执行结果:span最后为红色。
    说明css引入方式优先级:内部样式表 > 外部样式表

    例2、

     <link rel="stylesheet" type="text/css" href="./css/test.css">
     <style>#my{background:red;}</style>
     <span id="my" style="background: yellow">333</span>

    执行结果:span最后为黄色。
    说明css引入方式优先级:内联(行内)样式 > 内部样式表 > 外部样式表

    二、选择器

    1、普通选择器
    选择器 eg 描述
    通用选择器 * * 选择所有元素。
    类选择器 .class .message 选择 class="intro" 的所有元素。
    id选择器 #id #head 选择 id="firstname" 的所有元素。
    元素选择器 el p 选择所有 <p> 元素。
    选择器分组 el,el div,p 选择所有 <div> 元素和所有 <p> 元素。
    后代选择器 el el div p 选择 <div> 元素内部的所有 <p> 元素。
    子元素选择器 el > el div>p 选择 <div> 的第一子代的 所有<p> 元素。
    相邻兄弟选择器 el + el div+p 选择与<div>同级且紧接在其后的第一个 <p> 元素
    2、属性选择器
    类型 eg 描述
    [attribute] [target] 选择带有 target 属性所有元素
    [attribute=value] [target=_blank] 选择 target="_blank" 的所有元素
    [attribute~=value] [title~=flower] 选择 title 属性包含单词 "flower" 的所有元素
    [attribute¦=value] [lang¦=en] 选择 lang 属性值以 "en" 开头的所有元素。
    [attribute^=”value”] [abc^=”def”] 选择 abc 属性值以 “def” 开头的所有元素
    [attribute$=”value”] [abc$=”def”] 选择 abc 属性值以 “def” 结尾的所有元素
    [attribute*=”value”] [abc*=”def”] 选择 abc 属性值中包含子串 “def” 的所有元素
    3、伪类
    类型 eg 描述
    :link a:link 选择所有未被访问的链接
    :visited a:visited 选择所有已被访问的链接
    :active a:active 选择正在被点击的活动链接
    :hover a:hover 选择鼠标指针位于其上的链接
    :focus input:focus 选择获得焦点的 input 元素
    :lang(language) p:lang(it) 选择带有以 "it" 开头的 lang 属性值的每个 <p> 元素

    注:link、visited、active、hover的顺序,为LoVe HAte

    4、伪元素
    类型 eg 描述
    :before p:before 在每个 <p> 元素的内容之前插入内容
    :after p:after 在每个 <p> 元素的内容之后插入内容
    :first-letter p:first-letter 选择每个 <p> 元素的首字母
    :first-line p:first-line 选择每个 <p> 元素的首行
    :first-child p:first-child 选择属于父元素的第一个子元素的每个 <p> 元素
    el1~el2 p~ul 选择前面有 <p> 元素的每个 <ul> 元素。
    :first-of-type p:first-of-type 选择属于其父元素的首个 <p> 元素的每个 <p> 元素。
    :last-of-type p:last-of-type 选择属于其父元素的最后 <p> 元素的每个 <p> 元素。
    :only-of-type p:only-of-type 选择属于其父元素唯一的 <p> 元素的每个 <p> 元素。
    :only-child p:only-child 选择属于其父元素的唯一子元素的每个 <p> 元素。
    :nth-child(n) p:nth-child(2) 选择属于其父元素的第二个子元素的每个 <p> 元素。
    :nth-last-child(n) p:nth-last-child(2) 同上,从最后一个子元素开始计数。
    :nth-of-type(n) p:nth-of-type(2) 选择属于其父元素第二个 <p> 元素的每个 <p> 元素。
    :nth-last-of-type(n) p:nth-last-of-type(2) 同上,但是从最后一个子元素开始计数。
    :last-child p:last-child 选择属于其父元素最后一个子元素每个 <p> 元素。
    :root :root 选择文档的根元素。
    :empty p:empty 选择没有子元素的每个 <p> 元素(包括文本节点)。
    :target #news:target 选择当前活动的 #news 元素。
    :enabled input:enabled 选择每个启用的 <input> 元素。
    :disabled input:disabled 选择每个禁用的 <input> 元素
    :checked input:checked 选择每个被选中的 <input> 元素。
    :not(selector) :not(p) 选择非 <p> 元素的每个元素。
    ::selection ::selection 选择被用户选取的元素部分。

    三、一些注意

    1、id选择器

    www3school中所说:“一个id选择器 只能在一个文档中使用一次“,
    但在css实践中你会发现 两个同样的id都会生效。

    <style>
    span{margin: 5px;}  
    #my{background:red;}
    </style>
    
      <span id="my">666</span>
      <span id="my">333</span>

    执行结果为:
    这里写图片描述

    问:那为什么官方文档只让使用一次呢?

    答:若使用两次,第一影响就是不能通过W3的校验。
    在页面显示上,目前的浏览器css还都允许你犯这个错误,用多个相同ID“一般情况下”也能正常显示。
    但是当你需要用JavaScript通过id来控制这个div,那就会出现错误。因为js里获取DOM是通过getElementById,而如果页面出现同一个id几次,这样就获取不到了。所以id要有唯一性。

    2、多类选择器

    类选择器很简单,那多类选择器呢?好好看下面的例子及注释。
    www3school
    例1:2个类

    <style type="text/css">
    .important {font-weight:bold;}
    .warning {font-style:italic;}
    
    /*.important和.warning之间不能有空格*/
    /*.warning.important {background:silver;}颠倒顺序执行结果不变*/
    .important.warning {background:silver;}
    </style>
    
    <p class="important">This paragraph is very important.</p>
    <p class="warning">This is a warning.</p>
    <p class="important warning">This paragraph is a very important warning.</p>
    

    执行结果:
    这里写图片描述

    例2:3个类
    注:如果一个多类选择器包含类名列表中没有的一个类名,匹配就会失败。

    <style>
    .important {font-weight:bold;}
    .warning {font-style:italic;}
    /*.important和.warning之间不能有空格*/
    /*.warning.important {background:silver;}颠倒顺序执行结果不变*/
    .important.warning {background:silver;}
    </style>
    
    <p class="important">This paragraph is very important.</p>
    <p class="warning">This is a warning.</p>
    <p class="important urgent warning">This paragraph is a very important warning.</p>

    执行结果:同上
    这里写图片描述

    猜你喜欢

    转载自blog.csdn.net/b954960630/article/details/79560590