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. `

更多相关文章

  1. 【CSS入门】CSS基本语法和选择器优先级学习总结简介
  2. CSS3选择器 :nth-child()的用法
  3. 导航遍历并激活当前样式的方法
  4. 【css/scss】修改input,textarea中的placeholder样式
  5. web前端:常用的js语法
  6. 4.基础数据类型
  7. HTML表格与表单的标签与属性
  8. CSS 浮动的作用以及高度塌陷的解决方案BFC
  9. java集合【8】——— List源码详细分析

随机推荐

  1. 如何写出高质量的PHP代码
  2. 如何使用PHP_CodeSniffer检查代码规范(详
  3. PHP实现搜索联想功能(基于字典树算法)
  4. php比Node.js好用的五大理由
  5. php如何使用PHPAnalysis提取关键字中文分
  6. 简单的php多线程解决方法
  7. 教你用PHP实现微信小程序人脸识别刷脸登
  8. 谈一谈php面向对象的理解
  9. php实现文件上传到服务器(含代码)
  10. 十大最主流的PHP框架