再回首html与css(3)背景样式以及小技巧(含图解)

 背景样式 背景图片只会在当前的元素中显示
   background-color 背景颜色
   background-image: url("images/02.jpg"); 背景图片url("图片路径")
   background-repeat:    平铺四个值
                  repeat(默认值) 平铺
                  no-repeat 不平铺
                  repeat-x;  只有x轴方向平铺
                  repeat-y;  只有y轴方向平铺
   background-position: 值有三种写法(三种方法可以搭配使用 但是不推荐)
            1. 200px 200px; 背景图片定位 第一个值是x轴 第二个值是y轴
            2.  left right top bottom center 可以两两搭配使用 如果只给了一个值 另一个值就是默认center
            3. 百分比 百分比是根据盒子的宽高-背景图片的宽高来计算的
   background-attachment: 有两个值
            1. scroll背景图片随着页滚动而滚动
            2. fixed   这个时候背景图片的position是相对于body去定位的
   background-size: 有四种写法
               400px 400px     第一个值代表x轴  第二个值代表y轴
               300px  只给一个值代表x轴 第二个值auto 自适应 图片按比例缩小
               cover  按比例将盒子铺满
               contain  铺满一个方向就停止
               20% 20%  第一个x轴 第二个是y轴  是按照盒子宽高计算百分比
   复合样式:
   background: deeppink url("images/02.jpg") no-repeat fixed 0 0/cover;
   background: color image repeat attachment position/size;
         背景颜色 背景图片 是否平铺 是否固定 背景定位 背景尺寸
   其他值都可以省略 但是0 0/cover一定要搭配使用

background-attachment:fixed可以用来制作小特效

使背景图片固定,转动盒子


效果图如下 div的背景图片只会在div的盒子内部显示,超出部分将隐藏



background-position:后面的值为负数



   body{
            background: #252526;
        }
        div.wrap{
            width: 40px;
            margin: 100px;
        }
        .wrap ul li{
             width: 40px;
             height: 40px;
             background: red url("images/1.png") no-repeat;
             margin-bottom: 5px;
         }
        .wrap ul li:nth-child(1){
             background-position: -80px -280px;
        }
        .wrap ul li:nth-child(2){
            background-position: 0 -200px;
        }
        .wrap ul li:nth-child(3){
            background-position: 0 -240px;
        }
        .wrap ul li:nth-child(4){
            background-position: 0 -320px;
        }
        .wrap ul li:nth-child(5){
            background-position: 0 0;
        }
        </style>
</head>
<body>
    <div class="wrap">
        <ul>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值