CSS背景

CSS 背景属性用于定义HTML元素的背景。

CSS 属性定义背景效果:
  • background-color
  • background-image
  • background-repeat
  • background-attachment
  • background-position

代码简写时也按照此顺序



注意:下面代码中图片路径需自行更改否则可能达不到预期效果


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body {
            background-color: #ccc;
        }
        div {
            width: 400px;
            height: 500px;
            background-color: pink;
            background-image: url(img/l.jpg);
            background-repeat: no-repeat;
            
            /*利用方位名词来改变背景图片的位置*/
            background-position: left top;/* 默认左上角*/ 
            background-position: bottom right; /*右下角 方位名词不分顺序*/
            background-position: center center;
            background-position: left; /*方位名词只写一个 另一个默认为center*/
            background-position: 10px 30px; /*第一个为x坐标  第二个为y坐标*/
            background-position:  10px center;
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

返回目录


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body {
            /*
            background-image: url(img/ms.jpg);
            background-position: center -30px;
            background-repeat: no-repeat;
            background-color: #000;
             设置背景图像是否随内容滚动
            background-attachment: fixed; 
            background-attachment: local; 
            background-attachment: scroll; 默认是滚动的*/
            /*background-attachment相关介绍地址:https://developer.mozilla.org/zh-CN/docs/Web/CSS/background-attachment*/
             background: #000 url(img/ms.jpg) no-repeat fixed center -30px;
        }

        p {
            font-size: 22px;
            color: #fff;
        }
    </style>
</head>
<body>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
    <p>内容部分</p>
</body>
</html>

返回目录


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body {
            background: #000 url(img/king.jpg) no-repeat top center;
        }

        div {
            height: 400px;
            /* background: rgba(0,0,0,0.3); */
            background-color: rgba(0,0,0,0.3);
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

返回目录


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body {
            background-color: #ccc;
        }
        div {
            width: 100px;
            height: 140px;
            background: hotpink url(img/l.jpg) no-repeat;
            /* 设置背景图片大小 background-size   详细介绍请点击下面的MDN文档链接*/
			/*background-size的介绍:https://developer.mozilla.org/zh-CN/docs/Web/CSS/background-size*/

           /*  background-size: 100px; */ /*我们尽量改一个值 防止缩放失真扭曲*/ 
            /* background-size: 100%;  */ /* 268:418=100%*400:?*/
            /* 会自动调整缩放比例 保证图片始终填充满背景区域,假如有
              溢出部分则被隐藏 cover平时用的最多
            */ 
             /* background-size: cover; */
             background-size: contain;  
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

返回目录


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
         div {
             width: 500px;
             height: 500px;
             background: url(img/3.jpg) no-repeat left top,
              url(img/l.jpg) no-repeat left top hotpink;
         }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

返回目录


返回CSS基本语法

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值