25_背景图片.html 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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>Document</title>
  7. <style>
  8. .box{
  9. /* width: 400px;
  10. height: 400px;
  11. border:5px solid black; */
  12. /* 背景图片 */
  13. /* 背景图片如果无法撑满当前元素 那么他会重复出现直至铺满 */
  14. /* background-image: url("./image/logo.png"); */
  15. /* 背景图片不重复 */
  16. /* background-repeat: no-repeat; */
  17. /* 背景图片尺寸 两个数值 第一个宽 第二个高 */
  18. /* background-size: 400px 400px; */
  19. /* background-size: 100% 100%; */
  20. /* background-image: url("./image/bg.jpg"); */
  21. /* background-size: 100% 100%; */
  22. /* 可以让背景图完整展示 保证比例 */
  23. /* background-size: contain; */
  24. /* 背景图可以铺满整个元素 但是会导致图片被裁减 */
  25. /* background-size: cover; */
  26. }
  27. .box{
  28. width: 400px;
  29. height: 400px;
  30. border:5px solid black;
  31. background-image: url("./image/logo.png");
  32. background-repeat: no-repeat;
  33. /* 背景图片定位 两个值 第一个横向移动 纵向移动*/
  34. /* background-position: 20px 20px; */
  35. /* background-position: right bottom; */
  36. background-position: center center;
  37. }
  38. /* 雪碧图 */
  39. .box2{
  40. width: 40px;
  41. height: 60px;
  42. border:1px solid red;
  43. background-image: url("./image/icon-slides.png");
  44. background-position: -40px 0;
  45. }
  46. </style>
  47. </head>
  48. <body>
  49. <div class="box">
  50. hello world
  51. </div>
  52. <div class="box2">
  53. </div>
  54. </body>
  55. </html>