Blogger Information
Blog 24
fans 1
comment 0
visits 20861
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
CSS常用选择器和伪类伪元素介绍
知行合一
Original
1249 people have browsed it

除了经常用到的ID选择器、类选择器、元素选择器。CSS还有如简单选择器、上下文选择器,伪类选择器、伪元素选择器等。今天我们用案例来进行演示。

简单选择器

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>简单选择器</title>
  7. <style>
  8. /*简单选择器*/
  9. /*类选择器*/
  10. .container {
  11. height: 300px;
  12. width: 650px;
  13. }
  14. .item {
  15. float: left;
  16. text-align: center;
  17. line-height: 100px;
  18. background-color: cadetblue;
  19. width: 100px;
  20. height: 100px;
  21. margin-right: 20px;
  22. margin-bottom: 20px;
  23. color: white;
  24. font-size: 50px;
  25. }
  26. /*多个类复合应用*/
  27. .item.center {
  28. background-color: tomato;
  29. }
  30. /*ID选择器*/
  31. #first {
  32. background-color: violet;
  33. }
  34. .item#first {
  35. background-color: yellowgreen;
  36. } /*会覆盖上面的单ID选择器,因为优先级高*/
  37. /* * 号属于元素级别, #first默认是 *#first. */
  38. /*id,class可以添加在任何元素上,*/
  39. #first.item {
  40. background-color: blue;
  41. } /*优先级比.item#first高*/
  42. </style>
  43. </head>
  44. <body>
  45. <div class="container">
  46. <div class="item" id="first">1</div>
  47. <div class="item">2</div>
  48. <div class="item">3</div>
  49. <div class="item">4</div>
  50. <div class="item center">5</div>
  51. <div class="item">6</div>
  52. <div class="item">7</div>
  53. <div class="item">8</div>
  54. <div class="item">9</div>
  55. </div>
  56. </body>
  57. </html>

简单选择器

上下文选择器

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>上下文选择器</title>
  7. <style>
  8. /*ID选择器以后常用在表单中和锚点中,不建议CSS中用很多的ID选择器 ID和表单中的label对应*/
  9. .container {
  10. height: 300px;
  11. width: 650px;
  12. }
  13. .item {
  14. float: left;
  15. text-align: center;
  16. line-height: 100px;
  17. background-color: cadetblue;
  18. width: 100px;
  19. height: 100px;
  20. margin-right: 20px;
  21. margin-bottom: 20px;
  22. color: white;
  23. font-size: 50px;
  24. }
  25. /*后代选择器*/
  26. .container div {
  27. border: 3px salmon solid;
  28. }
  29. /*父子选择器*/
  30. body > div {
  31. border: turquoise 5px solid;
  32. } /*只针对他的儿子级设置*/
  33. /*同级相邻选择器*/
  34. .center + .item {
  35. background-color: slateblue;
  36. }
  37. /*同级所有选择器*/
  38. .center ~ .item {
  39. background-color: tan;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <div class="container">
  45. <div class="item">1</div>
  46. <div class="item">2</div>
  47. <div class="item">3</div>
  48. <div class="item">4</div>
  49. <div class="item center">5</div>
  50. <div class="item">6</div>
  51. <div class="item">7</div>
  52. <div class="item">8</div>
  53. <div class="item">9</div>
  54. </div>
  55. </body>
  56. </html>

上下文选择器

伪类选择器

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>伪类选择器</title>
  7. <style>
  8. .container {
  9. height: 300px;
  10. width: 650px;
  11. }
  12. .item {
  13. float: left;
  14. text-align: center;
  15. line-height: 100px;
  16. background-color: cadetblue;
  17. width: 100px;
  18. height: 100px;
  19. margin-right: 20px;
  20. margin-bottom: 20px;
  21. color: white;
  22. font-size: 50px;
  23. }
  24. /*匹配第一个子元素*/
  25. .container .item:first-child {
  26. background-color: thistle;
  27. }
  28. /*匹配最后一个子元素*/
  29. .container > .item:last-child {
  30. background-color: thistle;
  31. }
  32. /*匹配指定的一个子元素,排序从1开始*/
  33. .container .item:nth-child(3) {
  34. background-color: thistle;
  35. }
  36. /*匹配奇数*/
  37. .container .item:nth-child(odd) {
  38. background-color: violet;
  39. }
  40. .container .item:nth-child(2n-1) {
  41. background-color: violet;
  42. }
  43. /*匹配偶数*/
  44. .container .item:nth-child(even) {
  45. background-color: yellow;
  46. }
  47. .container .item:nth-child(2n) {
  48. background-color: yellow;
  49. }
  50. /*指定位置开始,选择剩下所有的元素*/
  51. .container .item:nth-child(n + 4) {
  52. background-color: turquoise;
  53. }
  54. /*只取前三个*/
  55. .container .item:nth-child(-n + 3) {
  56. background-color: black;
  57. }
  58. /*只取最后三个*/
  59. .container .item:nth-last-child(-n + 3) {
  60. background-color: blueviolet;
  61. }
  62. /*取第8个*/
  63. .container .item:nth-last-child(2) {
  64. background-color: red;
  65. }
  66. </style>
  67. </head>
  68. <body>
  69. <div class="container">
  70. <div class="item">1</div>
  71. <div class="item">2</div>
  72. <div class="item">3</div>
  73. <div class="item">4</div>
  74. <div class="item center">5</div>
  75. <div class="item">6</div>
  76. <div class="item">7</div>
  77. <div class="item">8</div>
  78. <div class="item">9</div>
  79. </div>
  80. </body>
  81. </html>

伪类选择器

结构伪类选择器

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>结构伪类选择器:分组不区分元素类型</title>
  7. <style>
  8. .container {
  9. height: 300px;
  10. width: 650px;
  11. }
  12. .item {
  13. float: left;
  14. text-align: center;
  15. line-height: 100px;
  16. background-color: cadetblue;
  17. width: 100px;
  18. height: 100px;
  19. margin-right: 20px;
  20. margin-bottom: 20px;
  21. color: white;
  22. font-size: 50px;
  23. }
  24. /*匹配最后一个元素*/
  25. .container div:last-of-type {
  26. background-color: blue;
  27. }
  28. /*匹配第一个span元素*/
  29. .container span:first-of-type {
  30. background-color: yellow;
  31. }
  32. /*匹配指定的一个div元素*/
  33. .container div:nth-of-type(3) {
  34. background-color: red;
  35. }
  36. /*匹配前两个div元素*/
  37. .container div:nth-of-type(-n + 2) {
  38. background-color: tomato;
  39. }
  40. /*匹配倒数两个div元素*/
  41. .container span:nth-last-of-type(-n + 2) {
  42. background-color: yellowgreen;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div class="container">
  48. <div class="item">1</div>
  49. <div class="item">2</div>
  50. <div class="item">3</div>
  51. <div class="item">4</div>
  52. <span class="item">5</span>
  53. <span class="item">6</span>
  54. <span class="item">7</span>
  55. <span class="item">8</span>
  56. <span class="item">9</span>
  57. </div>
  58. </body>
  59. </html>

结构伪类选择器

伪类与伪元素

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>伪类与伪元素</title>
  7. <style>
  8. #login-form {
  9. display: none;
  10. }
  11. /*target必须和ID配合,实现锚点操作 称为目标选择器*/
  12. /*当前#login-form的表单元素被a的锚点激活的时候执行*/
  13. #login-form:target {
  14. display: block;
  15. }
  16. /* :focus 获取焦点的时候执行 */
  17. input:focus {
  18. background-color: yellow;
  19. }
  20. /* ::selection设置被选中元素的前景色和背景色 */
  21. input::selection {
  22. background-color: red;
  23. color: white;
  24. }
  25. /*:not()选择不满足条件的元素*/
  26. .list > :not(:nth-of-type(3)) {
  27. color: red;
  28. }
  29. /*伪元素前面是双引号,伪类是单引号,如果不兼顾IE8. 伪元素也可单引号*/
  30. /*::before和::after*/
  31. .list::before {
  32. content: "前面添加内容";
  33. font-size: 1.5rem;
  34. color: yellowgreen;
  35. }
  36. .list::after {
  37. content: "后面添加内容";
  38. color: violet;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <!-- <a href="#login-form">点击打开登陆</a> -->
  44. <button onclick="location='#login-form'">点击登陆</button>
  45. <form action="" method="POST" id="login-form">
  46. <label for="name">用户名</label>
  47. <input id="name" type="text" />
  48. <label for="password">密码</label>
  49. <input type="password" id="password" />
  50. </form>
  51. <hr />
  52. <ul class="list">
  53. <li>item1</li>
  54. <li>item2</li>
  55. <li>item3</li>
  56. <li>item4</li>
  57. </ul>
  58. </body>
  59. </html>

" class="reference-link">伪类与伪元素

总结:CSS的选择器很多,选择一个元素可以用多种方法来实现。你懂的越多,代码就会写的越简洁。
建议大家做好笔记,经常测试使用,只有多使用,才能牢记于心。

备注:内容中咋多出了 “ class=”reference-link”> 我写的文章中是没有的。添加最后一个图片出现的!

Correcting teacher:GuanhuiGuanhui

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