P55-前端基础CSS-背景图片
        【摘要】 
                    
                        
                    
                    P55-前端基础CSS-背景图片 
1.概述 
 
 页面除了设置背景颜色外,还可以设置背景图片。通过对背景图片样式设置美化我们的页面。 
 
2.背景使用入门 
 
 background-image ...
    
    
    
    P55-前端基础CSS-背景图片
1.概述
页面除了设置背景颜色外,还可以设置背景图片。通过对背景图片样式设置美化我们的页面。
2.背景使用入门
background-image 设置背景图片
- 可以同时设置背景图片和背景颜色,这样背景颜色将会成为图片的背景色
 - 如果背景的图片小于元素,则背景图片会自动在元素中平铺将元素铺满
 - 如果背景的图片大于元素,将会一个部分背景无法完全显示
 - 如果背景图片和元素一样大,则会直接正常显示
 background-repeat 用来设置背景的重复方式
可选值:
- repeat 默认值 , 背景会沿着x轴 y轴双方向重复
 - repeat-x 沿着x轴方向重复
 - repeat-y 沿着y轴方向重复
 - no-repeat 背景图片不重复
 background-position 用来设置背景图片的位置
设置方式:
- 通过 top left right bottom center 几个表示方位的词来设置背景图片的位置
 - 使用方位词时必须要同时指定两个值,如果只写一个则第二个默认就是center
 - 通过偏移量来指定背景图片的位置:
 
- 水平方向的偏移量 垂直方向变量
 
2.1.使用背景图片
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>背景</title>
  <style>
    .box1 {
      width: 200px;
      height: 200px;
      /* 
        background-color 设置背景颜色
       */
      background-color: #1ff;
      /* 
        background-image 设置背景图片 
            - 可以同时设置背景图片和背景颜色,这样背景颜色将会成为图片的背景色
            - 如果背景的图片小于元素,则背景图片会自动在元素中平铺将元素铺满
            - 如果背景的图片大于元素,将会一个部分背景无法完全显示
            - 如果背景图片和元素一样大,则会直接正常显示
                    
        */
      background-image: url("./img/1.png");
      /* 
        background-repeat 用来设置背景的重复方式
            可选值:
                repeat 默认值 , 背景会沿着x轴 y轴双方向重复
                repeat-x 沿着x轴方向重复
                repeat-y 沿着y轴方向重复
                no-repeat 背景图片不重复
        */
        background-repeat: no-repeat;
        /*
                background-position 用来设置背景图片的位置
                    设置方式:
                        通过 top left right bottom center 几个表示方位的词来设置背景图片的位置
                            使用方位词时必须要同时指定两个值,如果只写一个则第二个默认就是center
                        通过偏移量来指定背景图片的位置:
                            水平方向的偏移量 垂直方向变量
            */
            /* 只写一个则第二个默认就是center */
            /* background-position: center; */
            /* 设置右上角位置 */
            /* background-position: top right; */
            /* 通过偏移量来指定背景图片的位置 */
            background-position: 50px 50px;
    }
  </style>
</head>
<body>
  <div class="box1"></div>
</body>
</html>
  
 - 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 - 19
 - 20
 - 21
 - 22
 - 23
 - 24
 - 25
 - 26
 - 27
 - 28
 - 29
 - 30
 - 31
 - 32
 - 33
 - 34
 - 35
 - 36
 - 37
 - 38
 - 39
 - 40
 - 41
 - 42
 - 43
 - 44
 - 45
 - 46
 - 47
 - 48
 - 49
 - 50
 - 51
 - 52
 - 53
 - 54
 - 55
 - 56
 - 57
 - 58
 - 59
 - 60
 - 61
 - 62
 - 63
 - 64
 
2.2.使用背景效果展示

3.设置背景图范围
设置背景的范围
background-clip
可选值:
- border-box 默认值,背景会出现在边框的下边
 - padding-box 背景不会出现在边框,只出现在内容区和内边距
 - content-box 背景只会出现在内容区
 background-origin 背景图片的偏移量计算的原点
- padding-box 默认值,background-position从内边距处开始计算
 - content-box 背景图片的偏移量从内容区处计算
 - border-box 背景图片的变量从边框处开始计算
 
3.1.background-clip: border-box效果

3.2.background-clip: padding-box效果

3.3.background-clip: content-box

3.4. background-origin: padding-box效果

3.5.background-origin: content-box效果

3.6.background-origin: border-box效果

3.7.设置背景图片大小

3.7.背景图片范围完整代码
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>背景</title>
    <style>
      .box1 {
        width: 500px;
        height: 500px;
        /* overflow: auto; */
        background-image: url("./img/1.png");
        background-color: blueviolet;
        background-repeat: no-repeat;
        border: 20px red double;
        background-position: 0 0;
        padding: 10px;
         /*
                 设置背景的范围 
                    background-clip 
                        可选值:
                            border-box 默认值,背景会出现在边框的下边
                            padding-box 背景不会出现在边框,只出现在内容区和内边距
                            content-box 背景只会出现在内容区
                    background-origin 背景图片的偏移量计算的原点
                            padding-box 默认值,background-position从内边距处开始计算
                            content-box 背景图片的偏移量从内容区处计算
                            border-box 背景图片的变量从边框处开始计算
            */
           
            /* background-clip: border-box 默认值,背景会出现在边框的下边 */
            /* background-clip: border-box; */
            /* background-clip: padding-box 背景不会出现在边框,只出现在内容区和内边距 */
            /* background-clip: padding-box; */
            /* background-clip: content-box 背景只会出现在内容区 */
            /* background-clip: content-box; */
            /* background-origin:  padding-box 默认值,background-position从内边距处开始计算 */
            /* background-origin: padding-box; */
            /* background-origin:  content-box 背景图片的偏移量从内容区处计算 */
            background-origin: content-box;
            /* background-origin:  border-box 背景图片的变量从边框处开始计算 */
            background-origin: border-box;
            /* 
                background-size 设置背景图片的大小
                    第一个值表示宽度 
                    第二个值表示高度
                    - 如果只写一个,则第二个值默认是 auto
                    cover 图片的比例不变,将元素铺满
                    contain 图片比例不变,将图片在元素中完整显示
            */
            background-size: contain;
      }
    </style>
  </head>
  <body>
    <div class="box1"></div>
  </body>
</html>
  
 - 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 - 19
 - 20
 - 21
 - 22
 - 23
 - 24
 - 25
 - 26
 - 27
 - 28
 - 29
 - 30
 - 31
 - 32
 - 33
 - 34
 - 35
 - 36
 - 37
 - 38
 - 39
 - 40
 - 41
 - 42
 - 43
 - 44
 - 45
 - 46
 - 47
 - 48
 - 49
 - 50
 - 51
 - 52
 - 53
 - 54
 - 55
 - 56
 - 57
 - 58
 - 59
 - 60
 - 61
 - 62
 - 63
 - 64
 - 65
 - 66
 - 67
 
4.背景图片跟随元素移动
4.1.设置背景图片是否跟随元素滚动

4.2.设置背景图片是否滚动效果

5.backgound 背景相关的简写属性
5.1.backgound 背景相关的简写属性案例
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>背景</title>
    <style>
      .box1 {
        width: 500px;
        height: 500px;
        /* overflow: auto; */
        background-image: url("./img/1.png");
        background-color: blueviolet;
        background-repeat: no-repeat;
        border: 20px red double;
        background-position: 0 0;
        padding: 10px;
         /*
                 设置背景的范围 
                    background-clip 
                        可选值:
                            border-box 默认值,背景会出现在边框的下边
                            padding-box 背景不会出现在边框,只出现在内容区和内边距
                            content-box 背景只会出现在内容区
                    background-origin 背景图片的偏移量计算的原点
                            padding-box 默认值,background-position从内边距处开始计算
                            content-box 背景图片的偏移量从内容区处计算
                            border-box 背景图片的变量从边框处开始计算
            */
           
            /* background-clip: border-box 默认值,背景会出现在边框的下边 */
            /* background-clip: border-box; */
            /* background-clip: padding-box 背景不会出现在边框,只出现在内容区和内边距 */
            /* background-clip: padding-box; */
            /* background-clip: content-box 背景只会出现在内容区 */
            /* background-clip: content-box; */
            /* background-origin:  padding-box 默认值,background-position从内边距处开始计算 */
            /* background-origin: padding-box; */
            /* background-origin:  content-box 背景图片的偏移量从内容区处计算 */
            background-origin: content-box;
            /* background-origin:  border-box 背景图片的变量从边框处开始计算 */
            /* background-origin: border-box; */
            /* 
                background-size 设置背景图片的大小
                    第一个值表示宽度 
                    第二个值表示高度
                    - 如果只写一个,则第二个值默认是 auto
                    cover 图片的比例不变,将元素铺满
                    contain 图片比例不变,将图片在元素中完整显示
            */
            background-size: contain;
        
      }
      .box2{
            width: 300px;
            height: 1000px;
            background-image: url('./img/1.png');
            background-repeat: no-repeat;
            background-position: 100px 100px;
            /* 
            background-attachment
                - 背景图片是否跟随元素移动
                - 可选值:
                    scroll 默认值 背景图片会跟随元素移动
                    fixed 背景会固定在页面中,不会随元素移动
             */
            background-attachment: fixed;
        }
        .box3 {
          /* 
                background-color
                background-image
                background-repeat
                background-position
                background-size
                background-origin
                background-clip
                background-attachment
                - backgound 背景相关的简写属性,所有背景相关的样式都可以通过该样式来设置
                    并且该样式没有顺序要求,也没有哪个属性是必须写的
                    注意:
                        background-size必须写在background-position的后边,并且使用/隔开
                            background-position/background-size
                        background-origin background-clip 两个样式 ,orgin要在clip的前边
            
             */
          border: 10px red double;
          padding: 50px;
          width: 500px;
          height: 500px;
          background: url('./img/2.jpg') #bfa center/contain border-box content-box no-repeat;
        }
    </style>
  </head>
  <body>
    <div class="box1">
      <div class="box2"></div>
    </div>
    <div class="box3"></div>
    
  </body>
</html>
  
 - 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 - 19
 - 20
 - 21
 - 22
 - 23
 - 24
 - 25
 - 26
 - 27
 - 28
 - 29
 - 30
 - 31
 - 32
 - 33
 - 34
 - 35
 - 36
 - 37
 - 38
 - 39
 - 40
 - 41
 - 42
 - 43
 - 44
 - 45
 - 46
 - 47
 - 48
 - 49
 - 50
 - 51
 - 52
 - 53
 - 54
 - 55
 - 56
 - 57
 - 58
 - 59
 - 60
 - 61
 - 62
 - 63
 - 64
 - 65
 - 66
 - 67
 - 68
 - 69
 - 70
 - 71
 - 72
 - 73
 - 74
 - 75
 - 76
 - 77
 - 78
 - 79
 - 80
 - 81
 - 82
 - 83
 - 84
 - 85
 - 86
 - 87
 - 88
 - 89
 - 90
 - 91
 - 92
 - 93
 - 94
 - 95
 - 96
 - 97
 - 98
 - 99
 - 100
 - 101
 - 102
 - 103
 - 104
 - 105
 - 106
 - 107
 - 108
 - 109
 - 110
 - 111
 - 112
 - 113
 - 114
 - 115
 - 116
 - 117
 - 118
 - 119
 - 120
 - 121
 
5.2.background属性简写效果

文章来源: brucelong.blog.csdn.net,作者:Bruce小鬼,版权归原作者所有,如需转载,请联系作者。
原文链接:brucelong.blog.csdn.net/article/details/113090675
        【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
            cloudbbs@huaweicloud.com
        
        
        
        
        
        
        - 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)