博客列表 >背景定位技术和阿里图标使用技巧

背景定位技术和阿里图标使用技巧

JKY辉哥
JKY辉哥原创
2020年06月22日 18:02:40732浏览

1.背景控制的常用属性

序号 描述 属性/值
1 背景颜色 background-color:red/yellow/#ccc
2 背景剪切 background-clip:border-box/content-box;
3 背景颜色渐变 background: linear-gradient(coral, yellow);
4 背景图片 background-image:url("图片名.jpg")
5 背景平铺方式 background-repeat:no-repeat/repeat-x/repeat-y
6 背景定位 background-position: 50% 20%/left
7 图片等比例剪切 background-size:contain/cover

示例代码:

  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. width: 300px;
  10. height: 300px;
  11. /* border: 1px solid #ccc; */
  12. border-radius: 150px;
  13. /* 背景色 */
  14. background-color: burlywood;
  15. /* 因为内边距是透明的,只能设置宽度不能设置样式,因此,背景色默认可以从内边距透出来 */
  16. /* padding: 20px; */
  17. /* 控制背景的覆盖范围限制在内容区,裁切 */
  18. /* 背景色扩展到边框 */
  19. /* background-clip: border-box; */
  20. /* 背景色只扩展到内容 */
  21. /* background-clip: content-box; */
  22. /* 渐变 */
  23. /* 从coral渐变到yellow */
  24. /* background: linear-gradient(coral, yellow); */
  25. /* 45度角渐变 */
  26. /* background: linear-gradient(45deg, coral, yellow); */
  27. /* 从左到右 */
  28. /* background: linear-gradient(to right, coral, yellow); */
  29. /* 从右到左 */
  30. /* background: linear-gradient(to left, coral, yellow); */
  31. /* 从右到左多色渐变 */
  32. /* background: linear-gradient(to left, coral, yellow, white, yellow); */
  33. background: linear-gradient(
  34. to left,
  35. rgba(255, 0, 0, 0.5) white,
  36. yellow,
  37. white,
  38. yellow
  39. );
  40. /* 背景图片 */
  41. background-image: url("girl.jpg");
  42. background-repeat: no-repeat;
  43. /* background-repeat: repeat-y; */
  44. /* background-attachment: fixed; */
  45. /* 背景定位:位置 */
  46. background-position: 50px 60px;
  47. /* background-position: center right; */
  48. /* background-position: right center; */
  49. /* 只写一个,第二个默认就是center */
  50. background-position: left;
  51. /* background-position: 50% 20%; */
  52. /* 只写一个,第二个默认就是50% */
  53. /* background-position: 50% 50%; */
  54. /* 图片等比例剪切 */
  55. /* background-size: contain; */
  56. background-size: cover;
  57. /* 简写 */
  58. /* background: violet; */
  59. background: url("girl.jpg") no-repeat center;
  60. position: relative;
  61. top: 20px;
  62. left: 10px;
  63. /* box-shadow: 5px 8px 10px #888; */
  64. }
  65. .box:hover {
  66. /* 外发光效果 */
  67. box-shadow: 0 0 8px #888;
  68. cursor: pointer;
  69. }
  70. </style>
  71. </head>
  72. <body>
  73. <div class="box"></div>
  74. </body>
  75. </html>

运行截图
背景;

2.精灵图的原理与实现

  • 精灵图功能:使用精灵图目标主要是为了减少 HTTP 请求,提升页面图片加载效率
  • 精灵图原理:使用 background-position 属性,设置背景图片,标尺量坐标 0,0,显示不同图片
  • 案例代码如下

  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. .box1 {
  9. width: 500px;
  10. height: 400px;
  11. border: 1px solid #000;
  12. background-image: url("1.png");
  13. background-repeat: no-repeat;
  14. /* background-position: 0px 0px; */
  15. }
  16. .box2 {
  17. width: 110px;
  18. height: 110px;
  19. background-image: url("1.png");
  20. background-repeat: no-repeat;
  21. background-position: -220px -110px;
  22. }
  23. .box3 {
  24. width: 110px;
  25. height: 110px;
  26. background-image: url("1.png");
  27. background-repeat: no-repeat;
  28. background-position: -330px -220px;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div class="box1"></div>
  34. <div class="box2"></div>
  35. <div class="box3"></div>
  36. </body>
  37. </html>

精灵图案例运行截图
精灵图;

3.阿里字体图标的完整引用流程

  • 登录阿里图标库官网,注册账号,创新寻找项目所需图标,下载定义下载图标
  • 具体引用代码如下:

引用图标方式 1:

  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. <link rel="stylesheet" href="font/iconfont.css" />
  7. <title>阿里字体图标的使用方法1</title>
  8. <style>
  9. .dz {
  10. font-size: 63px;
  11. color: red;
  12. /* box-shadow: 4px 4px 3px #888; */
  13. }
  14. .fd {
  15. font-weight: 500;
  16. font-size: 60px;
  17. color: red;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div>
  23. <span class="iconfont dz"></span>
  24. <span class="iconfont fd"></span>
  25. <span class="iconfont fd"></span>
  26. </div>
  27. </body>
  28. </html>

运行截图
方式1
注意:需要引入iconfont.css样式文件,具体使用可以查看下载的图标库示例demo_index.html 模板

引用图标方式 2:
demo4.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. <link rel="stylesheet" href="font-ico.css" />
  7. <title>阿里字体图标的使用方法2</title>
  8. <style>
  9. .lb {
  10. color: brown;
  11. font-size: 36px;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <span class="iconfont lb"></span>
  17. </body>
  18. </html>

font-ico.css 文件代码:

  1. @font-face {
  2. font-family: "iconfont";
  3. src: url("font/iconfont.eot");
  4. src: url("font/iconfont.eot?#iefix") format("font/embedded-opentype"),
  5. url("font/iconfont.woff2") format("font/woff2"),
  6. url("font/iconfont.woff") format("woff"),
  7. url("font/iconfont.ttf") format("font/truetype"),
  8. url("font/iconfont.svg#iconfont") format("font/svg");
  9. }
  10. .iconfont {
  11. font-family: "iconfont" !important;
  12. font-size: 16px;
  13. font-style: normal;
  14. -webkit-font-smoothing: antialiased;
  15. -moz-osx-font-smoothing: grayscale;
  16. }

注意:引用字体路径 font,自行添加

运行截图
方式2;

4.知识总结

    • 背景:背景颜色、背景图片、背景定位、背景裁切、背景颜色渐变、背景图片定位

    • 字体图标:字体图标,一定程度上代替了精灵图的功能,而且功能更加强大(图标在网站上无处不在)

    • 背景图片定位显示图标是字体图标的前身,学会使用精灵图和字体图标将让工作事半功倍

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