Blogger Information
Blog 6
fans 0
comment 0
visits 3140
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
css第一课
深海
Original
416 people have browsed it

css

第一课

样式

1.内链样式

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>Document</title>
  8. <!-- 内部样式,仅作用于当前的html文档 -->
  9. <style>
  10. h1 {
  11. color: green;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <h1>css内链样式 </h1>
  17. </body>
  18. </html>

2.行内样式

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>Document</title>
  8. </head>
  9. <!-- 行内样式,仅适用于当前元素,优先级要高于style标签设置的内部样式 -->
  10. <h1 style="color: red">css行内样式 </h1>
  11. </html>

3.引用样式

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. <link rel="stylesheet" href="style.css" />
  8. <!-- 引用了文件style.css样式,仅适用于当前元素,优先级要高于style标签设置的内部样式 -->
  9. <title>Document</title>
  10. </head>
  11. <body>
  12. <h1>css引用样式</h1>
  13. </body>
  14. </html>

1.ID、class选择器本质上是属性选择器

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. li {
  10. background-color: #000;
  11. }
  12. li.on {
  13. background-color: #fff;
  14. }
  15. li#foo {
  16. background-color: #fe3213;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <ul>
  22. <li id="foo">第一行</li>
  23. <li class="on">第二行</li>
  24. </ul>
  25. </body>
  26. </html>

2.上下文选择器

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. /* 根据元素的位置来选择 */
  10. /* 后代选择器 */
  11. /* 0,0,2 */
  12. ul li {
  13. background-color: #000000;
  14. }
  15. /* 只选中子元素,忽略孙元素 */
  16. /* 0,0,3 */
  17. body ul li {
  18. background-color: #f10404;
  19. }
  20. /* 同级相邻 可以是+号 也可以是空格*/
  21. .start + li {
  22. background-color: #ffff00;
  23. }
  24. .start li {
  25. background-color: #ffff00;
  26. }
  27. /* 同级所有兄弟 */
  28. .start ~ li {
  29. background-color: #3e0cf3;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <ul>
  35. <li>item1</li>
  36. <li class="start">item2</li>
  37. <li>item3</li>
  38. <li>
  39. item4
  40. <ul>
  41. <li>item4-1</li>
  42. <li>item4-2</li>
  43. <li>item4-3</li>
  44. </ul>
  45. </li>
  46. <li>item5</li>
  47. </ul>
  48. </body>
  49. </html>

3.伪类 (好好消化)

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. /* 结构伪类: 选择子元素, 要有一个父元素做为查询起点 */
  10. /* .list > :nth-child(3) {
  11. background-color: violet;
  12. } */
  13. /* 匹配任何位置的元素
  14. n = (0,1,2,3,4....) */
  15. /* .list > li:nth-child(0n + 3) {
  16. background-color: violet;
  17. } */
  18. /* 分组伪类结构选择器,推荐使用 */
  19. .list > li:nth-of-type(3) {
  20. background-color: cyan;
  21. }
  22. /* 选择中第一个p */
  23. .list > p:nth-of-type(1) {
  24. background-color: lightgreen;
  25. }
  26. .list > li:nth-of-type(1) {
  27. background-color: lightgreen;
  28. }
  29. /* 最后一个li */
  30. .list > li:nth-of-type(7) {
  31. background-color: green;
  32. }
  33. /* 最后一个p */
  34. .list > p:nth-of-type(3) {
  35. background-color: lightblue;
  36. }
  37. .list p:last-of-type {
  38. background-color: blue;
  39. }
  40. .list p:first-of-type {
  41. background-color: red;
  42. }
  43. /* 选择倒数第三个li */
  44. .list > li:nth-last-of-type(3) {
  45. background-color: yellow;
  46. }
  47. ul li:only-of-type {
  48. background-color: yellow;
  49. }
  50. /* 选择任何一个: :nth-of-type(n)
  51. 选择第一个: :first-of-type
  52. 选择最后一个: :last-of-type
  53. 选择倒数某一个: :nth-last-of-type()
  54. 唯一子元素的元素: :only-of-type 这里只的item11 */
  55. </style>
  56. </head>
  57. <body>
  58. <ul class="list">
  59. <li>item1</li>
  60. <li>item2</li>
  61. <li>item3</li>
  62. <li>item4</li>
  63. <li>item5</li>
  64. <li>item6</li>
  65. <p>item7</p>
  66. <p>item8</p>
  67. <li>item9</li>
  68. <p>item10</p>
  69. </ul>
  70. <ul>
  71. <li>item11</li>
  72. </ul>
  73. </body>
  74. </html>
  75. `

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