博客列表 >CSS选择器优先级提权方式与字体、图标应用,盒模型常用属性缩写方案

CSS选择器优先级提权方式与字体、图标应用,盒模型常用属性缩写方案

a.
a.原创
2020年12月16日 23:36:27632浏览

选择器组合实现优先级提权

1.当选择器相同里,与书写顺序有关,后面的样式覆盖前面的
2.当选择器不同时,与优先级相关,级别高的覆盖级别低的

  • 选择器优先级总结:ID > class > tag
  • 组合选择器优先级别计算公式:【id 选择器的数量,class 选择器的数量,tag 选择器数量】
  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. <title>选择器组合优先级提权</title>
  7. <style>
  8. p {
  9. color: red;
  10. }
  11. /*同选择器,后面声明的样式会覆盖前面的样式 */
  12. p {
  13. color: blue;
  14. }
  15. body p {
  16. /*此处的优先级别要大于单p标签选择器,计算公式:【id选择器的数量,class选择器的数量,tag选择器数量】
  17. 此处权重 【0 0 2】
  18. */
  19. color: red;
  20. }
  21. .class {
  22. /*类选择器权重大于tag选择器 */
  23. color: violet;
  24. }
  25. #id {
  26. /*id选择器权重大于类选择器*/
  27. color: tomato;
  28. }
  29. /*优先级总结:ID > class > tag*/
  30. p#id {
  31. /*此处权重【1 0 1】*/
  32. color: yellowgreen;
  33. }
  34. body p#id {
  35. /*此处权重【1 0 2】*/
  36. color: rgb(115, 22, 238);
  37. }
  38. #id.class {
  39. /*此处权重【1 1 0】*/
  40. color: rgb(235, 204, 28);
  41. }
  42. p#id.class {
  43. /*此处权重【1 1 1】*/
  44. color: rgb(0, 255, 128);
  45. }
  46. #id.class.class1 {
  47. /*此处权重【1 2 0】*/
  48. /*class选择器可以叠加权重,ID不能叠加树德*/
  49. color: rgb(255, 166, 0);
  50. }
  51. </style>
  52. </head>
  53. <body>
  54. <p class="class class1" id="id">选择器组合方式不同优先级也不同</p>
  55. </body>
  56. </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. <title>字体与字体图标</title>
  7. <style>
  8. .font {
  9. /*字体设置,如果不知道用什么字体就用通用字体*/
  10. font-family: sans-serif;
  11. /*字体大小*/
  12. font-size: 12px;
  13. /*风格 正常文本用normal 斜体用italic*/
  14. font-style: italic;
  15. /*字体的粗细 */
  16. font-weight: 900;
  17. /*以上都可以简写*/
  18. font: italic sans-serif 12px 900;
  19. }
  20. .iconfont.icon-wancheng {
  21. color: rgb(71, 135, 255);
  22. }
  23. </style>
  24. <!--导入阿里图标库CSS-->
  25. <link
  26. rel="stylesheet"
  27. href="https://at.alicdn.com/t/font_2275051_i6cvmpxveol.css"
  28. />
  29. </head>
  30. <body>
  31. <p class="font">字体与字体图标</p>
  32. <hr />
  33. <p>字体图标,可以用阿里在线图标</p>
  34. <form atcion="#" method="GET">
  35. <label for="dizhi" class="iconfont icon-shouhuodizhi">收货地址:</label>
  36. <input type="text" name="dizhi" id="dizhi" required />
  37. <button class="iconfont icon-wancheng">提交</button>
  38. </form>
  39. </body>
  40. </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. <title>盒模型的常用属性及简写</title>
  7. <style>
  8. .box {
  9. background-color: tomato;
  10. width: 300px;
  11. height: 500px;
  12. }
  13. .box {
  14. /*边框
  15. border-top-style 上
  16. border-right-style 右
  17. border-bottom-style 下
  18. border-left-style 左
  19. */
  20. /*每个边框可以设置三个属性:宽度,样式,颜色 */
  21. border-top-style: solid;
  22. border-top-width: 10px;
  23. border-top-color: rgb(121, 104, 101);
  24. /*简写 */
  25. border-top: solid 5px red;
  26. /*再简写 4边样式宽度颜色相同的情况下*/
  27. border: solid 6px violet;
  28. }
  29. .box {
  30. /*内边距padding: 上右下左*/
  31. background-clip: content-box;
  32. padding: 1px 2px 3px 4px;
  33. /*当左右相等,上下不相等时
  34. */
  35. padding: 12px 3px 4px;
  36. /*当左右相等,上下相等时*/
  37. padding: 18px 9px;
  38. /*当上下左右都相等时*/
  39. padding: 6px;
  40. }
  41. .box {
  42. /*margin-bottom 设置元素的下外边距。
  43. margin-left 设置元素的左外边距。
  44. margin-right 设置元素的右外边距。
  45. margin-top*/
  46. /*外边距:控制多个盒子之间的排列间距
  47. 4值,顺时针,上,右,下,左*/
  48. margin: 5px 6px 8px 4px;
  49. /*3值 左右相等,上下不相等 */
  50. margin: 5px 4px 4px;
  51. /*2值 左右相等,上下相等*/
  52. margin: 5px 10px;
  53. /*单值四个方向全相等*/
  54. margin: 8px;
  55. }
  56. </style>
  57. </head>
  58. <body>
  59. <div class="box">我是一个盒子</div>
  60. </body>
  61. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议