一.选择器的优先级 id,class,tag

  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>选择器的优先级 id,class,tag</title>
  8. <style>
  9. /* 1,1,3 */
  10. html body h1#id1.class1 {
  11. color: purple;
  12. }
  13. /* id选择器,优先级别大于class */
  14. /* 1,0,3 */
  15. html body h1#id1 {
  16. color: blue;
  17. }
  18. /* 1,0,2 */
  19. body h1#id1 {
  20. color: blueviolet;
  21. }
  22. /* 选择器本身优先级大于书写顺序 */
  23. /* 类样式 */
  24. /* 0,1,2 */
  25. body h1.class1 {
  26. color: lightsalmon;
  27. }
  28. /* 0,1,1 */
  29. h1.class1 {
  30. color: lightcoral;
  31. }
  32. /* 标签 */
  33. /* 0,0,3 */
  34. html body h1 {
  35. color: yellow;
  36. }
  37. /* 优先级相同时,书写顺序决定优先级 */
  38. html body h1 {
  39. color: lawngreen;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <h1 class="class1" id="id1">hello world</h1>
  45. </body>
  46. </html>

由此可见选择器的优先级,id>class>tag

二.样式模块化

  • 效果图

  • html 页面代码

  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. <link rel="stylesheet" href="css/style.css" />
  9. </head>
  10. <body>
  11. <header>页眉</header>
  12. <main>主体</main>
  13. <footer>页脚</footer>
  14. </body>
  15. </html>
  • css (style.css) 组件代码
  1. header {
  2. min-height: 3em;
  3. background-color: aqua;
  4. }
  5. main {
  6. min-height: 20em;
  7. background-color: lightcyan;
  8. }
  9. footer {
  10. min-height: 5em;
  11. background-color: blueviolet;
  12. color: #fff;
  13. }

三.伪类选择器的使用方式

  • 效果图

  • html 代码

  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 li:first-of-type {
  11. background-color: aqua;
  12. }
  13. /* 选择第三个元素 */
  14. .list li:nth-of-type(3) {
  15. background-color: lightgreen;
  16. }
  17. /* 选择最后一个元素 */
  18. .list li:last-of-type {
  19. background-color: blueviolet;
  20. }
  21. /* 选择倒数第五个元素 */
  22. .list li:nth-last-of-type(2) {
  23. background-color: lightcoral;
  24. }
  25. /* 使用伪类获取到p元素 */
  26. .list :only-of-type {
  27. background-color: yellow;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <ul class="list">
  33. <li>item1</li>
  34. <li>item2</li>
  35. <li>item3</li>
  36. <li>item4</li>
  37. <li>item5</li>
  38. <p>item6</p>
  39. <li>item7</li>
  40. <li>item8</li>
  41. <li>item9</li>
  42. <li>item10</li>
  43. </ul>
  44. </body>
  45. </html>

更多相关文章

  1. 1. 实例演示box-sizing属性; 2. 实例演示常用的元素居中方式
  2. CSS:box-sizing理解与相对定位与绝对定位理解
  3. css基础:box-sizing功能与定位功能的应用
  4. em,rem的原理与应用场景,分析 rem / em /vh/ vw的区别与联系
  5. CSS盒模型以及CSS定位
  6. 第三课 选择器、模块化组件、伪类选择器
  7. java集合【9】——— ArrayList源码分析
  8. CSS样式表优先级及模块化原理及实现
  9. 请用Python手写实现插入排序

随机推荐

  1. 如何使用php通过AJAX从数据库中删除记录,
  2. 尝试将纬度和经度发布到数据库时,Android
  3. C标准库中的函数定义在哪里?
  4. Mysql 使用 select into outfile
  5. 检索每n行的最高值
  6. 如何在C中删除多个闪存地址?
  7. MySQL数据库引擎ISAM MyISAM HEAP InnoDB
  8. mysql 数据库中表不同 但是列名相同,怎么
  9. SQL从结果数据库中选择subCode及其得分,并
  10. 什么时候最推荐使用mysql_real_escape_st