【实习第十一天】css背景

1.背景1

<!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>Document</title>
    <style>
        .box1{
            width: 300px;
            height: 300px;
            margin: 0 auto;
            /* 需求1:添加背景色 */
            /* 1、background-color
            添加背景色
             */
            background-color: red;
            /* 需求2:添加背景图片 */
            background-image: url();
            /* background-image:url() */
            /* 2、background-image:url()
            url() 指向图片引入的路径
            如果引入的图片小于盒子,图片会默认平铺
            如果引入的图片大于盒子,图片默认显示的是左上角的位置
            */
            /* 需求3:虽然图小,但图片我只要一张 */
            /* 3、background-repeat:no-repeat
            设置背景图片是否铺满
                可选值:
                    repeat  铺满
                    no-repeat  不平铺,留一张
                    repeat-x  水平方向铺满
                    repeat-y  垂直方向铺满    
            */
            background-repeat: no-repeat;

            /* 需求4:调整背景图片在元素中的位置 */
            /* 4、background-position:;
                参数:(参数之间用空格隔开)
                    参数1  水平方向的位置
                    参数2  垂直方向的位置

                写法1:通过这几个方位词来调整图片的位置
                left  right  top  bottom  center
                    参数2如果不写的话默认是center

                写法2:直接写大小
                注意:
                    参数2如果不写的话,默认也是center
            */
            background-position: right top;
            background-position: center center;
            
        }
    </style>
</head>
<body>
    <div class="box1"></div>
</body>
</html>

2.背景2

<!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>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            

        }

        .box1{
            height: 500px;
            width: 500px;
            background-color: #bfa;
            margin: 0 auto;
            padding: 20px;
            border: 10px double red;
            background-clip: content-box;
            /* 引入背景图 */
            background-image: url();
            /* 设置图片不重复 */
            background-repeat: no-repeat;
            /* 设置图片的位置 */
            background-position: 100px 50px;
            /* 设置图片偏移的原点 */
            background-origin: content-box;
        }

        .box2{
            width: 50%;
            height: 50%;
            background-color: rgba(255,235,205,.6);
            
        }
    </style>
</head>
<body>
    <div class="box1">
        <div class="box2">

        </div>
    </div>
</body>
</html>
<!-- 
    5:background-clips  设置背景色的范围
            可选值:
                border-box  默认值,背景颜色会出现在边框的下边
                padding-content 背景不会出现在边框,只会出现在内容区和内边距
                content-box    背景只出现在内容区 

    6:background-origin
        设置图片的偏移量计算的原点,配合偏移量使用
            可选值:
                border-box  从内部边距开始计算
                content-box 背景图片的偏移量从内容区开始计算
                border-box  从边框开始计算偏移量
 -->

3.背景3

<!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>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }

        .box1{
            width: 500px;
            height: 500px;
            border: 1px red solid;
            margin: 50px auto;
            background-image: url();
            background-repeat: no-repeat;
            background-size: 250px 250px;
        }
    </style>
</head>
<body>
    <div class="box1">
        
    </div>
</body>
</html>
<!-- 
    7:background-size  设置背景图片的大小
        参数:
            第一个值:宽度
            第二个值:高度
                如果只写一个,第二个值默认为auto

            cover   图片的比例不变,将元素铺满
            contain 图片的比例不变,将元素完整显示

 -->

4.背景4

<!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>Document</title>
    <style>
        .box1{
            width: 300px;
            height: 300px;
            background-color: red;
            background-image: url();
            background-repeat: no-repeat;
            background-position: center;
            background-size: auto;
            background-origin: border-box;
            background-clip: padding-box;

            /* 
                background
                - 通过该属性可以同时设置所有背景相关的样式
                - 没有顺序的要求,谁在前谁在后都行
                  也没有数量的要求,不写的样式就使用默认值
                - background-size要写在background-position后面
            */
        }
    </style>
</head>
<body>
    
</body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值