背景图片设置

目录

一、背景贴图方式

二、重复效果(background-repeat)

三、图片位置

1、像素方式

2、九宫格方式

四、图片大小(“重要”background-size)

五、图片偏移位置原点(background-origin)

六、背景图裁剪显示方式(background-clip)

七、代码


一、背景贴图方式

通过url贴链接,最好加上双引号

background-image: url("../src/milk.jpg");

二、重复效果(background-repeat)

(1)no-repeat

(2)repeat-x,沿x轴方向重复

(3)repeat-y,沿y轴方向重复

(4)repeat默认值,铺满整个区域

三、图片位置

1、像素方式

两个参数,以像素作为x,y轴的标尺

2、九宫格方式

(1)两个参数,可选值有center,left,right等,top、right就表示右上角

(2)一个参数,可选值与上面相同

四、图片大小(“重要”background-size)

(1)以像素或者比例的方式设置

(2)第一个值表示宽度,第二个值表示高度

(3)如果只写一个,则第二个值默认是auto

(4)可以通过background-size:100% atuo的方式来填充内容,这样会按照原图片比例缩放

(5)可以直接用contain的方式自动匹配大小,且不会超出边距

注意:一般设置背景图后,要设置图片大小,因为背景图片的大小可能会比自己设置的背景大小更大,如果图片周围存在透明效果,而图片内容在偏中间的位置,就可能导致图片不显示

五、图片偏移位置原点(background-origin)

(1)默认值是padding-box

(2)conten-box表示背景图片偏移量从内容区开始计算

(3)border-box表示背景图片偏移量从边框区开始计算

六、背景图裁剪显示方式(background-clip)

(1)border-box默认值,背景会出现在边框下方

(2)padding-box背景不会出现在边框,出现在内容区和内边距

(3)content-box背景只出现在内容区域

七、代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>背景</title>
    <style>
        .box1{
            /* 通过url贴链接,最好加上双引号*/
            background-image: url("../src/milk.jpg");
            width: 400px;
            height: 200px;
            background-color: #bfa;
            background-repeat:no-repeat;
            background-position: 50px 50px;
            background-size: 100% auto;
            border: red 10px double;
            background-clip: content-box;
            

            /* 
            repeat:
            1.no-repeat
            2.repeat-x
            3.repeat-y
            4.repeat

            background-position:
            1.像素(x,y)
            2.center,left等,九宫格

            background-size(设置背景图片的大小):
            1.第一个值表示宽度
            2.第二个值表示高度
            3.如果只写一个,则第二个值默认是auto
            4.可以通过
            background-size:100% atuo的方式来填充
            
            background-origin(设置图片偏移的坐标原点):
            1.默认值是padding-box
            2.conten-box表示背景图片偏移量从内容区开始计算
            3.border-box表示背景图片偏移量从边框区开始计算

            background-clip(设置背景图裁剪显示方式):
            1.border-box默认值,背景会出现在边框下方
            2.padding-box背景不会出现在边框,出现在内容区和内边距
            3.content-box背景只出现在内容区域
            */
            overflow: scroll;
        }
        .box2{
            background-image: url("../src/tea.jpg");
            background-size: 100% auto;
            background-repeat: no-repeat;
            height: 1000px;
        }
    </style>
</head>
<body>
    <div class="box1">
        <div class="box2">
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Quam, earum iste odio harum corporis asperiores ducimus deleniti culpa voluptatum minima ea possimus esse maxime autem consequuntur aut quos quis nobis.
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Pariatur, animi temporibus facilis ad adipisci eius obcaecati molestias at aperiam eos, suscipit nostrum sint molestiae, ex quos magnam reprehenderit officia veniam.
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Blanditiis atque aspernatur aut illo natus consequuntur voluptatibus quod aperiam vitae pariatur culpa sed repudiandae repellat earum iste, esse quia ipsum facere!
        </div>
    </div>
</body>
</html>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值