CSS3-Tips

伪类和伪元素

  • 伪类:
    :link, :visited, :hover, :active(LVHA), :focus, :first-child, :nth-child, :nth-last-child, :nth-of-type, :first-of-type, :last-of-type, :empty, :target, :checked, :enabled, :disabled,

  • 特别:

    • :first-child表示父元素的第一个子元素
    • :first-of-type表示在父容器内(直接父元素)其所有种类元素的第一个元素(所有第一次出现的都会被选中, 比如第一个 li 和第一个 span )
    • :last-of-type同理
    • :last-child表示父元素的最后一个子元素
    • :nth-child(an+b / odd / even),从 1 开始数
    • :nth-last-child()同理
    • :nth-of-type指的是某个选择器选中的那些中的 index,比如 p:nth-of-type(1) 就表示在父元素中第一次出现的p元素,哪怕这个 p 之前有其它标签也会被选中。
    • :nth-last-of-type同理
    • :only-child选中只有一个孩子的父元素
    • :only-of-type作用更 only-child 一样,只是可读性更强
    • :target表示当 window.location.hash == id 时,具备该 id 的元素将被选中
      如:div:target {background-color: red;} 而该 div 的 id 为 section, 那么当 url 中的 hash 为 #section 时,该 div 背景将变红
    • 表单:
      :default选择一组相似元素中的默认元素,如 checkbox 或 radio 中的默认选中项
      :in-range/:out-of-range表示 number 类型的元素在定义的 min 和 max 值之间 / 之外
      :indeterminate表单元素中未确定状态的项,比如在文档加载的时候 document.getElementById(“#id”).indeterminate = true 的那些
      :valid / :invalid 表单通过验证 / 不通过
      :optional / :required表示未required
      :placeholder