CSS的背景background

1.颜色

  background-color  (默认是transparent ---- 透明的)

<style>
    div {
      width: 200px;
      height: 200px;
      background-color: blue;
    }
</style>
<div>背景为蓝色的div</div>

2.图片

  background-image : none | url 

<style>
    div {
      width: 400px;
      height: 400px;
      background-image: url(../image/头像.jpg);
    }
</style>
<div></div>

 

3.平铺

  background-repeat : repeat | no-repeat | repeat-x | repeat-y

  repeat--重复

<style>
    body {
      background-image: url(../image/小米.png);
      background-repeat: repeat;
    }
</style>

   no-repeat ---不重复

<style>
    body {
      background-image: url(../image/小米.png);
      background-repeat: no-repeat;
    }
</style>

 

   repeat-x ---- 沿着x轴重复

<style>
    body {
      background-image: url(../image/小米.png);
      background-repeat: repeat-x;
    }
</style>

   repeat-y ----沿着y轴重复

<style>
    body {
      background-image: url(../image/小米.png);
      background-repeat: repeat-y;
    }
</style>

 

4.位置

  background-position : x y; (顺序可换)

  • x y 可以是方位名词 :top(顶部)| center(居中)| buttom(底部)| left(左侧)| right(右侧)

<style>
    div {
      width: 200px;
      height: 200px;
      background-color: pink;
      background-image: url(../image/小精灵图.jpg);
      background-repeat: no-repeat;
      background-position: top right;
    }
</style>
<div></div>

 

  • x y 是精确单位 (顺序不能换)

<style>
    div {
      width: 200px;
      height: 200px;
      background-color: pink;
      background-image: url(../image/小精灵图.jpg);
      background-repeat: no-repeat;
      background-position: 100px 50px;
    }
</style>

 

    注:如果只指定一个数值,则一定为x坐标,y默认垂直居中

  • 混合单位(x和y顺序不能换)

 <style>
    div {
      width: 200px;
      height: 200px;
      background-color: pink;
      background-image: url(../image/小精灵图.jpg);
      background-repeat: no-repeat;
      background-position: 100px top;
    }
</style>

 

5.背景图像固定(背景附着)

  background-attachment : scroll | fixed

 当一个有背景图的盒子里面的内容很多会有滚动效果时,背景图默认是scroll会随之滚动,

当我们给其设置为fixed时,再滚动内容时图片不会随之滚动。

  可以选择一张图替换如下地址观察效果

<!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>
    .big {
      width: 200px;
      height: 150px;
      background-image: url(../image/bg.jpg);
      background-repeat: no-repeat;
      background-attachment: fixed;
      overflow: scroll;
    }

    .small {
      width: 50px;
      height: 50px;
      background-color: pink;
    }
  </style>
</head>

<body>
  <div class="big">
    <div class="small">1</div>
    <div class="small">2</div>
    <div class="small">3</div>
    <div class="small">4</div>
  </div>
</body>

</html>

 

6.复合写法

  background : 颜色 地址 平铺 滚动 位置

7.背景色半透明 

  background : rgba(0,0,0,0.5)

<style>
    body {
      background: url(../image/bg.jpg) no-repeat fixed;
    }

    div {
      width: 400px;
      height: 100px;
      background: rgba(250, 242, 242, 0.3);
      /* color: #fff; */
    }
</style>
 <div>这是一个透明的遮罩层</div>

 

8.背景缩放

  background-size : auto(不缩放) | cover (适应盒子的大小)| contain(完全展示)

  •  auto 很好理解,就是图片按照自身的大小来
<style>
    div {
      width: 400px;
      height: 400px;
      border: 3px solid pink;
      background: url(../image/lightrain.jpg) no-repeat;
      background-size: auto;
    }
</style>
<div></div>

  •  cover 就是图片会自动缩放至填满盒子的大小,可能会导致图片部分位置看不到

    将上述代码background-size改成cover

   

  •  contain 会在能够完全展示自身的情况下将图片放到最大

    将上述代码background-size改成contain

    

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值