【前端学习笔记】背景属性

背景属性

个数属性描述说明
1background-color背景颜色background-color : red;
2background-image背景图片background-image : url();
3background-repeat背景图片的平铺background-repeat: no-repeat/ repeat/ repeat-x/ repeat-y ;
4background-position背景图片的定位background-position: 水平位置 垂直位置 可以给负值
5background-attachment背景图片的固定background- scroll(滚动)/ fixed (固定,固定在浏览器窗口里面,固定之后就相对于浏览器窗口了);

可以简写成background

background-image: 默认平铺效果

代码示例:

<!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: 400px;
            height: 400px;
            background-color: yellow;
            background-image: url(../img/feiman.jpg);
        }
    </style>
</head>
<body>
    <div class="box1"></div>
</body>
</html>

background-repeat

repeat: 默认平铺
repeat-x :x轴平铺
repeat-y :y轴平铺
no-repeat:不平铺

background-position

数字表示

background-position: 20px 20px;

百分比表示:

background-position: 10% 10%;

以下第一排与第二排两两组合

left center right
top center bottom

background-attachment

用该属性可以实现视差效果。
示例代码:

<!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;
        }
        .box{
            width: 500px;
            height: 1000px;
            background-color: yellow;
            margin: 0,auto;
        }
        body{
            background-image: url(../img/zhihu.png);
            background-attachment:fixed;
        }
    </style>
</head>
<body>
    <div class="box">

    </div>
</body>
</html>

背景的复合属性

复合写法:

  1. 用空格隔开
  2. 顺序可以换
  3. 可以只取一个值,放在后面能覆盖前面的值

background-size属性只能单独用

示例代码:

<!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>
        div{
            width: 300px;
            height: 300px;
            /* background-color: yellow;
            background-image: url(../img/zhihu.png);
            background-repeat: no-repeat;
            background-position: center; */
            background: yellow url(../img/zhihu.png) no-repeat center;
        }

        .box2{
            width: 300px;
            height: 300px;
            background-color: yellow;
            background: url(../img/zhihu.png) no-repeat;
        }
    </style>
</head>
<body>
    <div class="box1"></div>

    <div class="box2"></div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值