CSS选择器

2018-03-29  本文已影响0人  hum_tan

CSS选择器规定了CSS规则会作用到哪些元素上。

基本选择器

元素 {样式声明}
.类名 {样式声明}
#ID {样式声明}
* {样式声明}
img[src$=".svg" i] { /**忽略大小写**/
  color: pink;
}

组合选择器

前方元素 , 目标元素 {样式声明}
前方元素 + 目标元素 {样式声明}
元素 ~ 元素 {样式声明}
元素1 > 元素2 {样式声明}
元素1 元素2 {样式声明}

伪类选择器

为了正确地渲染链接元素的样式需遵循LVFHA的先后顺序,即::link — :visited — :focus — :hover — :active

  1. X不能包含另外一个否定选择器
  2. X会参与权重计算
  1. :nth-child(0n+3) / :nth-child(3) 匹配第三个元素
  2. 1n+0 或简单的 n 匹配每个元素
  3. :nth-child(2n+1) / :nth-child(odd) 匹配奇数行
  4. :nth-child(2n) / :nth-child(even) 匹配偶数行
  5. :nth-child(-n+3) 匹配前三个子元素
  1. E:nth-of-type(2) 匹配第二个标签为E的元素
  2. .class:nth-of-type(2) 匹配相同标签的第二个且该(第二个)标签的类名匹配.class
<style>
      .item:nth-of-type(3){
        color:red;
      }
</style>
<div>
      <h1>标题</h1>
      <p class="item">这是锻若</p>
      <p>这是锻若</p>
      <span>这是span</span>
      <span class="item">这是span</span>
      <span class="item">这是span</span> <!-- 红色 -->
      <p class="item">这是锻若</p> <!-- 红色 -->
      <p class="item">这是锻若</p>
      <p class="item">这是锻若</p>
      <h1 class="item">标题</h1>
      <h1>标题</h1> <!-- 不是红色 -->
  </div>

伪元素选择器

注意 块对象指display: block, inline-block...

上一篇 下一篇

猜你喜欢

热点阅读