Blogger Information
Blog 18
fans 0
comment 2
visits 10457
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
css伪类选择器
go0
Original
414 people have browsed it

:first-of-type的用法

  • 类型中的第一个,字面意思比较好记,前面要有一个冒号。

    1. <!-- 伪类 -->
    2. <ul class="list">
    3. <span>aaa</span>
    4. <li>item1</li>
    5. <li>item2</li>
    6. <li>item3</li>
    7. <li>item4</li>
    8. <li>item5</li>
    9. <li>item6</li>
    10. <p>asdf</p>
    11. <p>asdf</p>
    12. <p>asdf</p>
    13. <li>item7</li>
    14. <li>item8</li>
    15. </ul>
    16. <style>
    17. .list > li:first-of-type {
    18. background-color: yellow;
    19. }
    20. .list > p:first-of-type {
    21. background-color: red;
    22. }
    23. </style>

  • 如果不限定标签,例如: .list :first-of-type,那么表示的就是所有类型的第一个。

    1. <!-- 伪类 -->
    2. <ul class="list">
    3. <p>aaa</p>
    4. <p>bbb</p>
    5. <p>ccc</p>
    6. <li>item1</li>
    7. <li>item2</li>
    8. <li>item3</li>
    9. <li>item4</li>
    10. <li>item5</li>
    11. <li>item6</li>
    12. <li>item7</li>
    13. <li>item8</li>
    14. </ul>
    15. <style>
    16. .list :first-of-type {
    17. background-color: #888;
    18. }
    19. </style>


:nth-of-type()的用法

  • 这个比:first-of-type()更灵活。如果:first-of-type(-2)能够选中倒数第2个就更人性化了,设计了first-last-of-type(2)表示倒数第2个,有点多余。

    1. <!-- 伪类 -->
    2. <ul class="list">
    3. <span>aaa</span>
    4. <li>item1</li>
    5. <li>item2</li>
    6. <li>item3</li>
    7. <li>item4</li>
    8. <li>item5</li>
    9. <li>item6</li>
    10. <li>item7</li>
    11. <li>item8</li>
    12. </ul>
    13. <style>
    14. .list > li:nth-of-type(3) {
    15. background-color: #888;
    16. }
    17. </style>

——

伪类选择器的参数**:nth-of-type(an+b)

  • a为0表示匹配单个
  • a为1表示匹配一组
  • a为2表示匹配奇偶位置的元素
  1. 举例:nth-of-type(n+3)
  2. n从零开始取值,自动递增,n+3则为345…… ;匹配3-end
  3. -n+3321.匹配前三个
  1. <ul class="list">
  2. <li>item1</li>
  3. <li>item2</li>
  4. <li class="three">item3</li>
  5. <li>item4</li>
  6. <li>item5</li>
  7. <li>item6</li>
  8. <li>item7</li>
  9. <li>item8</li>
  10. </ul>
  11. <style>
  12. .list .three,
  13. .list .three ~ * {
  14. background-color: red;
  15. }
  16. .list > :nth-of-type(n + 3) {
  17. background-color: blue;
  18. }
  19. </style>

  1. 举例:nth-of-type(2n) 表示偶数 语法糖:even
  2. nth-of-type(2n+1) 表示奇数 语法糖:odd
  1. <ul class="list">
  2. <li>item1</li>
  3. <li>item2</li>
  4. <li class="three">item3</li>
  5. <li>item4</li>
  6. <li>item5</li>
  7. <li>item6</li>
  8. <li>item7</li>
  9. <li>item8</li>
  10. </ul>
  11. <style>
  12. .list .three,
  13. .list .three ~ * {
  14. background-color: red;
  15. }
  16. .list > :nth-of-type(2n + 3) {
  17. background-color: blue;
  18. }
  19. .list > :nth-of-type(odd):hover {
  20. background-color: cyan;
  21. }
  22. </style>

伪类小例子,单选按钮选中后label变色

  1. <input type="radio" name="sex" id="m" /><label for="m"></label>
  2. <input type="radio" name="sex" id="fem" /><label for="fem"></label>
  3. <style>
  4. input:checked + label {
  5. color: red;
  6. }
  7. button {
  8. width: 150px;
  9. height: 30px;
  10. border: none;
  11. background-color: palegoldenrod;
  12. color: brown;
  13. }
  14. button:hover {
  15. background-color: coral;
  16. cursor: pointer;
  17. }
  18. </style>

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post