博主信息
博文 13
粉丝 1
评论 0
访问量 19332
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
CSS选择器和实例
樊天龙的博客
原创
1146人浏览过

CSS选择器

1.简单选择器

序号 选择器 描述 举例
1. 元素选择器 根据元素名进行匹配 p {...}
2. 类选择器 根据class属性进行匹配 .active {...}
3. ID选择器 根据id属性进行匹配,一般一个页面只有一个名称的id #top
4. 通配符选择器 选择全部元素,不区分类型,一般用于浏览器默认样式重置 * {...}
5. 分组选择器 同时选择多个不同类型的元素 h1,h2,h3 {...}
6. 属性选择器 根据元素的属性进行匹配 input[type="text"] {...}

其中最常用的是:元素选择器,类选择器,ID选择器.

2.上下文选择器

  • HTML文档具有一定的层次结构DOM(文档对象模型),称为DOM结构,或DOM树.
  • 每一个元素, 在文档中, 都有自己的位置,即上下文关系
  • 所以, 完全可以根据元素的上下文关系,来获取到它们

2.1一个元素的五种角色

序号 角色 描述
1. 祖先元素 拥有子元素,孙元素等所有层级的后代元素
2. 父级元素 仅拥有子元素层级的元素
3. 兄弟元素 即层次相同的元素
4. 后代元素 拥有子,孙…等所有更低层级的元素
5. 子元素 即父级元素下的低一个层级的元素

2.2四种上下文选择器

序号 选择器 操作符 描述 举例
1. 后代选择器 空格 选择当前元素的所有后代元素 div p {...}
2. 父子选择器 > 选择当前元素的所有子元素 div > p {...}
3. 毗邻选择器 + 选择当前元素的同父级且相邻的元素 h1 + p {...}
4. 毗邻兄弟选择器 ~ 选择当前元素的同父级后续的所有元素 li.red ~ li {...}

3.伪类选择器

  • 学习之前,先分析上下文选择器的局限性,例如选择同一个父级下的第二个子元素,就没那么简单
  • 而伪类就正好弥补了上下文选择器的短板, 所以伪类,大多数是基于文档中元素结构的
  • : 本意是假的,不存在的意思, 这里是特指, 不需要在元素上添加额外的属性来获取元素

按照应用场景可以对伪类选择器进行如下分类:

场景 描述
结构伪类 根据子元素的位置特征进行选择
表单伪类 根据表单控件状态特征进行选择

3.1结构伪类

3.1.1不分组匹配

序号 选择器 描述 举例
1 :first-child 匹配第一个子元素 div :first-child
2 :last-child 匹配最后一个子元素 div :last-child
3 :only-child 选择元素的唯一子元素 div :only-child
4 :nth-child(n) 匹配任意位置的子元素 div :nth-child(n)
5 :nth-last-child(n) 匹配倒数任意位置的子元素 div :nth-last-child(n)

3.3.2分组匹配

序号 选择器 描述 举例
1 :first-of-type 匹配按类型分组后的第一个子元素 div :first-of-type
2 :last-of-type 匹配按类型分组后的最后一个子元素 div :last-of-type
3 :only-of-type 匹配按类型分组后的唯一子元素 div :only-of-type
4 :nth-of-type() 匹配按类型分组后的任意位置的子元素 div :nth-of-type(n)
5 :nth-last-of-type() 匹配按类型分组后倒数任意位置的子元素 div :nth-last-of-type(n)
  • 允许使用表达式来匹配一组元素,表达式中的”n”是从”0”开始计数,且必须写到前面
  • “-n”表示获取前面一组元素,正数表示从指定位置获取余下元素

3.2其它伪类

序号 选择器 描述
1 :active 向被激活的元素添加样式
2 :focus 向拥有键盘输入焦点的元素添加样式
3 :hover 当鼠标悬浮在元素上方时,向元素添加样式
4 :link 向未被访问的链接添加样式
5 :visited 向已被访问的链接添加样式
5 :root 根元素,通常是html
5 :empty 选择没有任何子元素的元素(含文本节点)
5 :not() 排除与选择器参数匹配的元素

4.实例

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. .container {
  14. width: 400px;
  15. text-align: center;
  16. }
  17. h1 {
  18. margin-bottom: 10px;
  19. color: darkgoldenrod;
  20. }
  21. ul>li {
  22. float: left;
  23. list-style-type: none;
  24. width: 20px;
  25. height: 20px;
  26. line-height: 20px;
  27. margin: 0 10px;
  28. border-radius: 10px;
  29. color: #ffffff;
  30. text-align: center;
  31. }
  32. ul li:first-child {
  33. background-color: red;
  34. }
  35. ul li:last-child {
  36. background-color: aqua;
  37. }
  38. ul li:nth-child(2) {
  39. background-color: blue;
  40. }
  41. ul li:nth-child(3),
  42. li:nth-child(3)+li {
  43. background-color: blueviolet;
  44. }
  45. #bg-purple {
  46. background-color: purple;
  47. }
  48. .bg-wheat {
  49. background-color: wheat;
  50. }
  51. li[title] {
  52. background-color: deeppink;
  53. }
  54. </style>
  55. <title>双色球</title>
  56. </head>
  57. <body>
  58. <div class="container">
  59. <h1>双色球</h1>
  60. <ul>
  61. <li>1</li>
  62. <li>2</li>
  63. <li>3</li>
  64. <li>4</li>
  65. <li id="bg-purple">5</li>
  66. <li class="bg-wheat">6</li>
  67. <li class="bg-wheat">7</li>
  68. <li title="extra">8</li>
  69. <li title="extra">9</li>
  70. <li>10</li>
  71. </ul>
  72. </div>
  73. </body>
  74. </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. <style>
  7. table {
  8. width: 200px;
  9. border-collapse: collapse;
  10. text-align: center;
  11. }
  12. th,
  13. td {
  14. border: 1px solid;
  15. color: #ffffff;
  16. }
  17. th {
  18. background-color: pink;
  19. }
  20. /* 偶数行 */
  21. tr:nth-child(even) {
  22. /* background-color: deepskyblue; */
  23. }
  24. tr:nth-child(2n) {
  25. background-color: deepskyblue;
  26. }
  27. /* 奇数行 */
  28. tr:nth-child(odd) {
  29. /* background-color: lightskyblue; */
  30. }
  31. tr:nth-child(2n+1) {
  32. background-color: lightskyblue;
  33. }
  34. </style>
  35. <title>表格隔行换色</title>
  36. </head>
  37. <body>
  38. <table>
  39. <caption>表格隔行换色</caption>
  40. <tr>
  41. <th>姓名</th>
  42. <th>性别</th>
  43. </tr>
  44. <tr>
  45. <td>1</td>
  46. <td>张三</td>
  47. </tr>
  48. <tr>
  49. <td>2</td>
  50. <td>李四</td>
  51. </tr>
  52. <tr>
  53. <td>3</td>
  54. <td>王五</td>
  55. </tr>
  56. <tr>
  57. <td>4</td>
  58. <td>赵六</td>
  59. </tr>
  60. </table>
  61. </body>
  62. </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. <style>
  7. a {
  8. text-decoration: none;
  9. }
  10. a:link {
  11. color: #000000;
  12. }
  13. a:visited {
  14. color: red;
  15. }
  16. a:hover {
  17. text-decoration: underline;
  18. }
  19. a:active {
  20. background-color: blueviolet;
  21. }
  22. input:focus {
  23. background-color: #ff7300;
  24. }
  25. /* 根元素通常是html */
  26. :root {
  27. background-image: url("images/bg.jpeg");
  28. }
  29. p:empty {
  30. width: 40px;
  31. height: 20px;
  32. background-color: brown;
  33. }
  34. /* 否定伪类:选择器取反,
  35. 除了第一个li元素 */
  36. li:not(.first) {
  37. font-size: 20px;
  38. color: chartreuse;
  39. }
  40. </style>
  41. <title>表单伪类</title>
  42. </head>
  43. <body>
  44. <a href="http://www.php.cn">PHP中文网</a>
  45. <!-- 没有内容的p元素 -->
  46. <p></p>
  47. <form>
  48. <input type="text" autofocus>
  49. </form>
  50. <ul>
  51. <li class="first">1</li>
  52. <li>2</li>
  53. <li>3</li>
  54. </ul>
  55. </body>
  56. </html>

5.效果图

6.总结

  • 选择器是CSS的重点,学会了选择器相当于学会了CSS知识的一半
  • 灵活的使用选择器,对布局和简化代码是友好的
批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:发给大家的课件, 大家一定要保留好, 是一个很好的资料库
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学