PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

博客列表 > 两种常见伪类和盒模型常用属性

两种常见伪类和盒模型常用属性

P粉854918706
P粉854918706 原创
2022年09月07日 02:16:16 402浏览

两种常见伪类和盒模型常用属性

两种伪类实例:

1.位置伪类:

  1. /* 为所有的列表设置虚线边框,顺带复习了一下选择器权重 */
  2. .list > li:nth-of-type(n) {
  3. border: 1px dashed gray;
  4. }
  5. .item {
  6. border: 1px solid lightblue;
  7. }
  8. /* 头两行设置背景色 */
  9. .list > li:nth-of-type(-n+2){
  10. background-color: aqua;
  11. }
  12. /* 最后两行设置背景色 */
  13. .list > li:nth-last-of-type(-n+2){
  14. background-color:chartreuse;
  15. }
  16. /* 第一行 */
  17. .list > li:first-of-type{
  18. background-color: blue;
  19. font-weight: 800;
  20. color:aliceblue;
  21. }
  22. /* 最后一行 */
  23. .list > li:last-of-type{
  24. background-color: brown;
  25. font-weight: 900;
  26. color:white;
  27. }
  28. /* 设置偶数行,也可以用2n */
  29. .list > li:nth-of-type(even){
  30. border: 1px dashed gray;
  31. }
  32. /* 设置奇数行 也可以用2n+1或2n-1*/
  33. .list > li:nth-of-type(odd){
  34. border: 1px dashed gray;
  35. }
  36. /* 任意设置行 */
  37. .list > li:nth-of-type(n+3){
  38. border: 1px dashed gray;
  39. }
  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/demo2.css">
  9. </head>
  10. <body>
  11. <ul class="list">
  12. <li class="item">item1</li>
  13. <li class="item">item2</li>
  14. <li class="item">item3</li>
  15. <li class="item">item4</li>
  16. <li class="item">item5</li>
  17. <li class="item">item6</li>
  18. <li class="item">item7</li>
  19. <li class="item">item8</li>
  20. </ul>
  21. </body>
  22. </html>

2.状态伪类:

  1. legend{
  2. text-align: center;
  3. border: 1px solid cyan;
  4. }
  5. /* 状态伪类 */
  6. input:focus{
  7. background-color: burlywood;
  8. }
  9. input:checked + label{
  10. color: cyan;
  11. font-weight: bolder;
  12. }
  13. button{
  14. height: 30px;
  15. background-color: coral;
  16. outline: none;
  17. border: none;
  18. border-radius: 2px;
  19. color:aliceblue
  20. }
  21. /* 状态伪类 */
  22. button:hover{
  23. cursor: pointer;
  24. background-color: aqua;
  25. color:coral;
  26. }
  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/demo1.css">
  9. </head>
  10. <body>
  11. <form action="" method="get">
  12. <fieldset>
  13. <legend>用户注册列表</legend>
  14. <br />
  15. <label for="username">用户名:</label>
  16. <input type="text" name="username" id="username">
  17. <br />
  18. <label for="psd">密码:<input type="password" name="password" id="psd"> </label>
  19. <br />
  20. <label for="m">性别:</label>
  21. <input type="radio" name="sex" id="m" value="男" checked><label for="m"></label>
  22. <input type="radio" name="sex" id="f" value="女" ><label for="f"></label>
  23. <input type="radio" name="sex" id="s" value="保密"><label for="s">保密</label>
  24. <br/>
  25. <label for="swing">特长:</label>
  26. <input type="checkbox" name="fa" id="swing" value="游泳" ><label for="swing">游泳</label>
  27. <input type="checkbox" name="fa" id="drive" value="骑马" ><label for="drive">骑马</label>
  28. <input type="checkbox" name="fa" id="shejian" value="射箭" checked><label for="shejian">射箭</label>
  29. <input type="checkbox" name="fa" id="pandeng" value="攀登" ><label for="pandeng">攀登</label>
  30. <br/>
  31. <button>提交</button>
  32. </fieldset>
  33. </form>
  34. </body>
  35. </html>

盒模型及常用属性示例

  1. .box{
  2. width:500px;
  3. height:300px;
  4. border:2px dashed coral;
  5. padding:10px 20px;
  6. margin:15px 10px 20px 10px;
  7. text-align: center;
  8. }
  9. .hello{
  10. width:200px;
  11. height:200px;
  12. float: left;
  13. border:2px dashed cyan;
  14. padding:0;
  15. margin:0;
  16. text-align: left;
  17. }
  18. .hello1{
  19. width:200px;
  20. height:200px;
  21. float: right;
  22. border:2px dashed cyan;
  23. padding:0;
  24. margin:0;
  25. text-align: right;
  26. box-sizing: border-box;
  27. }
  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/demo3.css">
  9. </head>
  10. <body>
  11. <div class="box">
  12. <p>随便写几个字示意示意</p>
  13. <div class="hello">
  14. 这里边再放一个盒子呢?
  15. </div>
  16. <div class="hello1">
  17. 右边来一个?
  18. </div>
  19. </div>
  20. </body>
  21. </html>

盒模型及常用属性

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议