博客列表 >1. 实例演示: 基本,上下文,伪类(结构,状态)选择器 2. 选做: 状态伪类实现一个下拉菜单功能

1. 实例演示: 基本,上下文,伪类(结构,状态)选择器 2. 选做: 状态伪类实现一个下拉菜单功能

P粉890456325
P粉890456325原创
2023年02月08日 10:11:33262浏览
  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. <link rel="stylesheet" href="css/01css.css">
  9. </head>
  10. <body>
  11. 标签选择器
  12. 属性选择器
  13. <h1>Hello world</h1>
  14. <hr>
  15. <h1 class="site">php</h1>
  16. <h1 id="city">山西</h1>
  17. 根据元素**位置与层级**匹配元素
  18. 父子>后代 兄弟+同级~ 后代空格
  19. <ul class="list">
  20. <li class="item">item1</li>
  21. <li class="item">item2</li>
  22. <li class="item">item3</li>
  23. <li class="item">item4
  24. <ul>
  25. <li class="item">item4-1</li>
  26. <li class="item">item4-2</li>
  27. <li class="item">item4-3</li>
  28. </ul>
  29. </li>
  30. <li class="item start">item5</li>
  31. <li class="item">item6</li>
  32. <li class="item">item7</li>
  33. <li class="item">item8</li>
  34. </ul>
  35. <ul class="list1">
  36. <li class="item first">item1</li>
  37. <li class="item second">item2</li>
  38. <li class="item third">item3</li>
  39. <li class="item fourth">item4</li>
  40. <li class="item fifth">item5</li>
  41. <li class="item sixth">item6</li>
  42. <li class="item seventh">item7</li>
  43. <li class="item eighth">item8</li>
  44. <li class="item ninth">item9</li>
  45. <li class="item tenth">item10</li>
  46. </ul>
  47. <ul class="list2">
  48. <li>item1</li>
  49. <li>item2</li>
  50. <li>item3</li>
  51. <li>item4</li>
  52. <li class="five">item5</li>
  53. <li>item6</li>
  54. <li>item7</li>
  55. <li>item8</li>
  56. <li>item9</li>
  57. <li>item10</li>
  58. </ul>
  59. <ul class="list3">
  60. <li>item1</li>
  61. <li>item2</li>
  62. <li>item3</li>
  63. <li>item4</li>
  64. <li>item5</li>
  65. <li>item6</li>
  66. <li>item7</li>
  67. <li>item8</li>
  68. <li>item9</li>
  69. <li>item10</li>
  70. </ul>
  71. <!-- <form action="">
  72. <fieldset>
  73. <legend>用户注册</legend>
  74. autofocus: 布尔属性,自动获取焦点
  75. <input type="text" id="username" name="username" placeholder="用户名" required autofocus />
  76. <input type="email" id="email" name="email" placeholder="邮箱" required disabled />
  77. <input type="password" id="password" name="password" placeholder="密码" required />
  78. 复选框默认是选中状态: checked
  79. <div>
  80. <input type="checkbox" id="rem" name="remember" checked />
  81. <label for="rem">记住我</label>
  82. </div>
  83. <button>提交</button>
  84. </fieldset>
  85. </form> -->
  86. <div class="box">
  87. <label for="menu">直播课程</label>
  88. <input type="checkbox" name="xxx" id="menu">
  89. <ul>
  90. <li><a href="">全栈开发</a></li>
  91. <li><a href="">安卓开发</a></li>
  92. <li><a href="">苹果开发</a></li>
  93. </ul>
  94. </div>
  95. </body>
  96. </html>

  1. /* 基本选择器 */
  2. /* 01标签选择器 */
  3. h1 {
  4. color: red;
  5. }
  6. /* 02属性选择器
  7. h1[class="site"]{
  8. color: green;
  9. }
  10. h1[id="city"]{
  11. color: violet;
  12. }
  13. id class高频属性
  14. [class="site"]===.site
  15. [id="city"]===#city
  16. */
  17. h1.site{
  18. color: blue;
  19. }
  20. h1#city{
  21. color: green;
  22. }
  23. /* 上下文选择器
  24. 一定要有查询的起点 入口
  25. 根据元素**位置与层级**匹配元素
  26. 父子>后代 兄弟+同级~ 后代空格
  27. class="item start"===.item.start 两个类必须连着
  28. */
  29. .list > .item{
  30. border:thin solid;
  31. }
  32. .list .item{
  33. border:thin solid;
  34. }
  35. /* .list > .item.start + .item{
  36. background-color: yellow;
  37. }
  38. .list > .item.start + *{
  39. background-color: yellow;
  40. } */
  41. .list > .item.start ~ *{
  42. background-color: yellow;
  43. }
  44. /*
  45. 伪类选择器 结构
  46. 伪类选择器, 最常用的"结构伪类",用于查询子元素
  47. 这与上下文选择器非常相似,必须设置一个查询起点,否则从html开始递归查询
  48. 与上下文选择器相比, 结构伪类,要简洁的多,除起点元素外,几乎不需要在子元素上添加多余属性
  49. item是放li的公共样式,后面的是自定义样式
  50. */
  51. /* class的定位方法 */
  52. .list1 > .item.first{
  53. background-color: lightgreen;
  54. }
  55. .list1 > .item.fifth{
  56. background-color: lightgreen;
  57. }
  58. /*
  59. 伪类定位方法
  60. 获取第一个元素或最后一个元素或任意一个元素
  61. :nth-child(n)函数拿到元素
  62. 第一和最后一个是属于高频操作,有语法糖
  63. 拿到前2个-n+2
  64. 拿到第五个开始class
  65. 群组选择器
  66. 第七个开始所以的元素n+7
  67. */
  68. .list2 > :nth-child(1){
  69. background-color: lightgreen;
  70. }
  71. .list2 > :first-child{
  72. background-color: rgb(127, 101, 245);
  73. }
  74. .list2 > :nth-child(10){
  75. background-color: lightgreen;
  76. }
  77. .list2 > :last-child{
  78. background-color: rgb(238, 144, 188);
  79. }
  80. .list2 > :nth-child(4){
  81. background-color: red;
  82. }
  83. .list2 > :nth-child(-n+2){
  84. background-color: lightgreen;
  85. }
  86. .list2 > .five,
  87. .list2 > .five ~ * {
  88. background-color: rgb(255, 0, 217);
  89. }
  90. .list2 > :nth-child(n+7){
  91. background-color: rebeccapurple;
  92. }
  93. /* 伪类选择器:nth-child() */
  94. /*
  95. :nth-of-type(an + b)
  96. 1. a: 系数, [0,1,2,3,...]
  97. 2. n: 参数, [0,1,2,3,...]
  98. 3. b: 偏移量, 从0开始
  99. 规则: 计算出来的索引,必须是有效的(从1开始)
  100. 匹配元素的二种场景1匹配一个2匹配一组
  101. a=0匹配一个
  102. 0乘任何数都是0 0n+1===1
  103. 如果要获取其他的元素 只需要改一个b 偏移量
  104. a=1匹配一组
  105. 获取第三元素起匹配一组
  106. a=-1前三元素起反向匹配一组
  107. 1乘以任何数不变 所以1不必写
  108. a=2 === even匹配偶数位隔行显示
  109. 2n+1 === odd匹配奇数位隔行显示
  110. 匹配最后三个元素===倒数第三个起匹配剩下的全部
  111. 倒数第三元素:nth-last-child(3)
  112. 倒数第三个起全部元素:nth-last-child(-n+3)
  113. */
  114. .list3 > :nth-child(0n + 1){
  115. background-color: lightgreen;
  116. }
  117. .list3 > :nth-child(1){
  118. background-color: lightcoral;
  119. }
  120. .list3 > :nth-child(1n){
  121. background-color: rgb(0, 255, 60);
  122. }
  123. .list3 > :nth-child(1n+3){
  124. background-color: yellow;
  125. }
  126. .list3 > :nth-child(-1n+3){
  127. background-color:red;
  128. }
  129. .list3 > :nth-child(n+5){
  130. background-color: rgb(51, 0, 255);
  131. }
  132. .list3 > :nth-child(-n+3){
  133. background-color:rgb(255, 0, 251);
  134. }
  135. .list3 > :nth-child(2n){
  136. background-color:rgb(8, 4, 8);
  137. }
  138. .list3 > :nth-child(even){
  139. background-color:rgb(150, 88, 150);
  140. }
  141. .list3 > :nth-child(2n+1){
  142. background-color:rgb(225, 254, 3);
  143. }
  144. .list3 > :nth-child(odd){
  145. background-color:rgb(3, 48, 252);
  146. }
  147. .list3 > :nth-child(n+8){
  148. background-color:seagreen;
  149. }
  150. .list3 > :nth-last-child(3){
  151. background-color:rgb(58, 65, 61);
  152. }
  153. .list3 > :nth-last-child(-n+3){
  154. background-color:rgb(254, 5, 5);
  155. }
  156. /* ! 状态伪类 */
  157. fieldset {
  158. display: inline-grid;
  159. gap: 1em;
  160. border-radius: 10px;
  161. padding: 1em 2em;
  162. color: #666;
  163. background: linear-gradient(to left top, lightcyan, white);
  164. }
  165. fieldset legend {
  166. text-align: center;
  167. }
  168. fieldset input {
  169. padding: 5px;
  170. border: none;
  171. border-bottom: 1px solid #666;
  172. outline: none;
  173. background-color: transparent;
  174. }
  175. /*表单操作 状态伪类
  176. 单击输入框后变色
  177. 单击复选框时变色
  178. 按钮基础的样式边框轮廓线去除盒模型背景前景色
  179. 按钮鼠标变手透明度
  180. 禁用元素加背景
  181. 复选框与下拉菜单绑定
  182. 根据复选框的状态确定下拉菜单是否显示
  183. 隐藏复选框
  184. 隐藏下拉菜单
  185. 显示或隐藏下拉菜单
  186. 更新复选框的状态 选中或不选中
  187. */
  188. fieldset :focus{
  189. background-color: aquamarine;
  190. }
  191. input[type='checkbox']:checked+label{
  192. color: red;
  193. }
  194. button{
  195. border: none;
  196. outline: none;
  197. padding: 5px 10px;
  198. background-color: lightcoral;
  199. color: aliceblue;
  200. }
  201. button:hover{
  202. cursor: pointer;
  203. opacity: 0.8;
  204. }
  205. fieldset :disabled{
  206. background-color: yellow;
  207. }
  208. #menu{
  209. display: none;
  210. }
  211. #menu+ul{
  212. display: none;
  213. }
  214. #menu:checked+ul{
  215. display: block;
  216. }
  217. .box {
  218. display: inline-grid;
  219. position: relative;
  220. color: white;
  221. }
  222. .box label {
  223. background-color: lightseagreen;
  224. padding: 5px 10px;
  225. }
  226. .box label:hover {
  227. cursor: pointer;
  228. }
  229. .box li {
  230. list-style: none;
  231. }
  232. .box li a {
  233. text-decoration: none;
  234. color: #444;
  235. font-size: 13px;
  236. }
  237. .box li a:hover {
  238. color: orangered;
  239. font-weight: bolder;
  240. }
  241. .box ul {
  242. margin: 0;
  243. padding: 10px 0;
  244. padding-left: 10px;
  245. border: 1px solid #aaa;
  246. box-shadow: 3px 3px 3px #aaa;
  247. }
  248. `
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议