CSS_图片篇&背景

╰+攻爆jí腚メ 2023-10-06 16:56 89阅读 0赞

一、图片大小

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <link rel="stylesheet" href="../../reset.css">
  7. <style>
  8. body {
  9. background-color: antiquewhite;
  10. }
  11. .img-list {
  12. width: 190px;
  13. height: 470px;
  14. overflow: hidden;
  15. margin: 50px auto;
  16. background-color: #fff;
  17. }
  18. /* 设置图片的大小 */
  19. .img-list img {
  20. width: 100%;
  21. }
  22. .img-list li {
  23. margin-bottom: 9px;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <ul class="img-list">
  29. <li>
  30. <a href="javascript:;"><img src="./img/1.webp" alt=""></a>
  31. </li>
  32. <li>
  33. <a href="javascript:;"><img src="./img/2.webp" alt=""></a>
  34. </li>
  35. <li>
  36. <a href="javascript:;"><img src="./img/3.webp" alt=""></a>
  37. </li>
  38. </ul>
  39. </body>
  40. </html>

二、图片缝隙解决方法

  1. img{
  2. vertical-align: bottom;
  3. }

在这里插入图片描述

背景图片

  1. background-size
  2. 作用:设置背景图片的大小
  3. 第一个值表示宽度
  4. 第二个值表示高度
  5. 如果只写一个,则第二个值默认是auto
  6. cover图片的比例不变,将元素铺满
  7. contain图片比例不变,将图片在元素中完整显示

背景


















































背景说明 代码
background-color 设置背景颜色 background-color: #bfa;
background-image 设置背景图片 background-image: url(“./img/1.png”);
background-repeat 用来设置背景的重复方式 可选值:repeat-x 沿着x轴方向重复,repeat-y沿着y轴方向重复 , no-repeat 背景图片不重复, repeat 默认值 , 背景会沿着x轴 y轴双方向重复
background-position 用来设置背景图片的位置 1、background-position: center; 通过 top left right bottom center 几个表示方位的词来设置背景图片的位置,使用方位词时必须要同时指定两个值,如果只写一个则第二个默认就是center;2、background-position: -50px 300px; 通过偏移量来指定背景图片的位置:水平方向的偏移量 垂直方向变量
  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. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style>
  9. .box1{
  10. width: 500px;
  11. height: 500px;
  12. overflow: auto;
  13. background-color: #bfa;
  14. background-image: url("./img/2.jpg");
  15. background-repeat: no-repeat;
  16. background-position: 0 0;
  17. padding: 10px;
  18. /*
  19. 设置背景的范围
  20. background-clip
  21. 可选值:
  22. border-box 默认值,背景会出现在边框的下边
  23. padding-box 背景不会出现在边框,只出现在内容区和内边距
  24. content-box 背景只会出现在内容区
  25. background-origin 背景图片的偏移量计算的原点
  26. padding-box 默认值,background-position从内边距处开始计算
  27. content-box 背景图片的偏移量从内容区处计算
  28. border-box 背景图片的变量从边框处开始计算
  29. */
  30. /* background-origin: border-box;
  31. background-clip: content-box; */
  32. /*
  33. background-size 设置背景图片的大小
  34. 第一个值表示宽度
  35. 第二个值表示高度
  36. - 如果只写一个,则第二个值默认是 auto
  37. cover 图片的比例不变,将元素铺满
  38. contain 图片比例不变,将图片在元素中完整显示
  39. */
  40. background-size: contain;
  41. /*
  42. background-color
  43. background-image
  44. background-repeat
  45. background-position
  46. background-size
  47. background-origin
  48. background-clip
  49. background-attachment
  50. - backgound 背景相关的简写属性,所有背景相关的样式都可以通过该样式来设置
  51. 并且该样式没有顺序要求,也没有哪个属性是必须写的
  52. 注意:
  53. background-size必须写在background-position的后边,并且使用/隔开
  54. background-position/background-size
  55. background-origin background-clip 两个样式 ,orgin要在clip的前边
  56. */
  57. }
  58. .box2{
  59. width: 300px;
  60. height: 1000px;
  61. background-image: url('./img/1.png');
  62. background-repeat: no-repeat;
  63. background-position: 100px 100px;
  64. /*
  65. background-attachment
  66. - 背景图片是否跟随元素移动
  67. - 可选值:
  68. scroll 默认值 背景图片会跟随元素移动
  69. fixed 背景会固定在页面中,不会随元素移动
  70. */
  71. background-attachment: fixed;
  72. }
  73. .box3{
  74. border: 10px red double;
  75. padding: 50px;
  76. width: 500px;
  77. height: 500px;
  78. background: url('./img/2.jpg') #bfa center center/contain border-box content-box no-repeat ;
  79. }
  80. </style>
  81. </head>
  82. <body>
  83. <div class="box3">
  84. </div>
  85. <!-- <div class="box1">
  86. <div class="box2">
  87. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Totam aut, odio iusto accusantium ipsum aliquid omnis facere sapiente, nobis vel dicta alias ducimus. Repellat similique unde eius tempore, quia quo.
  88. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Accusantium, accusamus quibusdam. Adipisci in dolorem qui accusantium accusamus voluptatibus magnam nesciunt minus enim quaerat! Quidem, rem. Ipsum amet praesentium enim aliquid!
  89. Lorem ipsum dolor sit amet consectetur adipisicing elit. Aperiam provident repellendus ipsum dolorum optio quo, iure eveniet beatae cupiditate rerum minus corporis illum aliquam illo ut quidem aliquid expedita deserunt.
  90. </div>
  91. </div> -->
  92. </body>
  93. </html>

发表评论

表情:
评论列表 (有 0 条评论,89人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CSS背景图片无法显示

    问题描述: 我在编写网页时,设置了CSS样式来指定背景图片,但是在浏览器中预览时,背景图片却无法显示出来。请问我应该如何解决这个问题呢? 解决方案: 1. 检查图片路