Blogger Information
Blog 19
fans 0
comment 0
visits 10872
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
CSS选择器权重的原理与计算方式与结构伪类
牙森江
Original
385 people have browsed it

1.CSS选择器

1.1CSS选择器的四种写法

1.1.1 父子关系


  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>CSS选择器</title>
  8. <style>
  9. /* 父子关系 */
  10. .item> .in
  11. {
  12. color: rgb(231, 22, 224);
  13. border: 1px rgb(0, 0, 0) solid;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <!-- 父子关系:div与p标签 -->
  19. <div class="item">
  20. <p class="in">我不喜欢CSS1</p>
  21. <p class="in">我不喜欢CSS2</p>
  22. <p class="in">我不喜欢CSS3</p>
  23. <ul class="list">
  24. <li class="listitem">我不想学PHP</li>
  25. <li class="listitem">我不想学PHP</li>
  26. <li class="listitem">我不想学PHP</li>
  27. <li class="listitem">我不想学PHP</li>
  28. <li class="listitem">我不想学PHP</li>
  29. </ul>
  30. <p class="in">我不喜欢CSS4</p>
  31. <p class="in">我不喜欢CSS5</p>
  32. <p class="in">我不喜欢CSS6</p>
  33. </div>
  34. </body>
  35. </html>

代码运行效果如下:

" class="reference-link">

1.1.2 后代关系

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>CSS选择器</title>
  8. <style>
  9. /* 后代关系 */
  10. .item .in
  11. {
  12. background-color: red;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <!-- 父子关系:div与p标签 -->
  18. <div class="item">
  19. <p class="in">我不喜欢CSS1</p>
  20. <p class="in">我不喜欢CSS2</p>
  21. <p class="in">我不喜欢CSS3</p>
  22. <ul>
  23. <li class="in">我不想学PHP</li>
  24. <li class="in">我不想学PHP</li>
  25. <li class="in">我不想学PHP</li>
  26. <li class="in">我不想学PHP</li>
  27. <li class="in">我不想学PHP</li>
  28. </ul>
  29. <p class="in">我不喜欢CSS4</p>
  30. <p class="in">我不喜欢CSS5</p>
  31. <p class="in">我不喜欢CSS6</p>
  32. </div>
  33. </body>
  34. </html>

代码运行效果如下:

1.1.3 兄弟关系:同级关系——>同级上下

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>CSS选择器</title>
  8. <style>
  9. /* 链式语法 */
  10. .item>.in.list{
  11. background-color: green;
  12. }
  13. /* 兄弟:+* */
  14. .item>.in.list+*{
  15. background-color: green;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <!-- 父子关系:div与p标签 -->
  21. <div class="item">
  22. <p class="in">我不喜欢CSS1</p>
  23. <p class="in">我不喜欢CSS2</p>
  24. <p class="in">我不喜欢CSS3</p>
  25. <ul>
  26. <li class="in">我不想学PHP</li>
  27. <li class="in">我不想学PHP</li>
  28. <li class="in">我不想学PHP</li>
  29. <li class="in">我不想学PHP</li>
  30. <li class="in">我不想学PHP</li>
  31. </ul>
  32. <p class="in list">我不喜欢CSS4</p>
  33. <p class="in">我不喜欢CSS5</p>
  34. <p class="in">我不喜欢CSS6</p>
  35. </div>
  36. </body>
  37. </html>

代码运行效果如下:

" class="reference-link">

1.1.4 所有:同级关系

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>CSS选择器</title>
  8. <style>
  9. /* 链式语法 */
  10. .item>.in.list{
  11. background-color: green;
  12. }
  13. /* 兄弟:+* */
  14. .item>.in.list+*{
  15. background-color: green;
  16. }
  17. /* 所有: ~ */
  18. .item>.in.list~*{
  19. background-color: green;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <!-- 父子关系:div与p标签 -->
  25. <div class="item">
  26. <p class="in">我不喜欢CSS1</p>
  27. <p class="in">我不喜欢CSS2</p>
  28. <p class="in">我不喜欢CSS3</p>
  29. <ul>
  30. <li class="in">我不想学PHP</li>
  31. <li class="in">我不想学PHP</li>
  32. <li class="in">我不想学PHP</li>
  33. <li class="in">我不想学PHP</li>
  34. <li class="in">我不想学PHP</li>
  35. </ul>
  36. <p class="in list">我不喜欢CSS4</p>
  37. <p class="in">我不喜欢CSS5</p>
  38. <p class="in">我不喜欢CSS6</p>
  39. <p class="in">我不喜欢CSS7</p>
  40. <p class="in">我不喜欢CSS8</p>
  41. <p class="in">我不喜欢CSS9</p>
  42. </div>
  43. </body>
  44. </html>

代码运行效果如下:

" class="reference-link">

1.2 选择器-伪类

1.2.1 用伪类获取第一个和最后一个item

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>伪类</title>
  8. <style>
  9. /* 伪类-匹配第一个class */
  10. /* .list > p.first {
  11. background-color: rgb(233, 0, 0);
  12. } */
  13. /* 伪类获取第一个和最后一个item */
  14. .list > p:first-of-type {
  15. background-color: rgb(233, 0, 0);
  16. }
  17. .list > p:last-of-type {
  18. background-color: rgb(0, 255, 17);
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="list">
  24. <p class="item first">paragraph1</p>
  25. <p class="item">paragraph2</p>
  26. <p class="item">paragraph3</p>
  27. <p class="item">paragraph4</p>
  28. <p class="item">paragraph5</p>
  29. <p class="item">paragraph6</p>
  30. <p class="item">paragraph7</p>
  31. <p class="item">paragraph8</p>
  32. <p class="item">paragraph9</p>
  33. <p class="item">paragraph10</p>
  34. </div>
  35. </body>
  36. </html>

代码运行效果如下:


1.2.1 用伪类获取第一个和最后一个item

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>伪类</title>
  8. <style>
  9. /* 伪类获取前三个item */
  10. /* 公式=an+b */
  11. .list > p:nth-of-type(-n+3) {
  12. background-color: rgb(233, 0, 0);
  13. }
  14. .list > p:nth-last-of-type(-n+3){
  15. background-color: rgb(0, 255, 17);
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div class="list">
  21. <p class="item first">paragraph1</p>
  22. <p class="item">paragraph2</p>
  23. <p class="item">paragraph3</p>
  24. <p class="item">paragraph4</p>
  25. <p class="item">paragraph5</p>
  26. <p class="item">paragraph6</p>
  27. <p class="item">paragraph7</p>
  28. <p class="item">paragraph8</p>
  29. <p class="item">paragraph9</p>
  30. <p class="item">paragraph10</p>
  31. </div>
  32. </body>
  33. </html>

代码运行效果如下:

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