基础代码:
div {
width: 200px;
height: 200px;
background: url(./css-logo.png);
}
<div></div>
1、background-repeat:默认是repeat
设置背景图片在容器内是否平铺。
![](https://img-blog.csdnimg.cn/direct/3834fd811cfd4ab2984b3633bf64b718.png)
![](https://img-blog.csdnimg.cn/direct/a6eeca406cb342bd81d358fa6ae62b50.png)
![](https://img-blog.csdnimg.cn/direct/b24d85c901eb486aadbeb60ed483833f.png)
2、background-position:可使用关键字或坐标(数字+单位,正负均可;水平方向,正数向右,垂直方向,正数向下。)
注:设置时可设置两个值,如center top,若两个值相同,可简写为center。
![](https://img-blog.csdnimg.cn/direct/2ca8a91e9bf14acfbcde918bda655ba8.png)
![](https://img-blog.csdnimg.cn/direct/562f78f2884545b68cd7b9e6f9de7cd5.png)
![](https://img-blog.csdnimg.cn/direct/919a7186bbc54325a3a725fabde4e549.png)
![](https://img-blog.csdnimg.cn/direct/7ec55bb8bcc24069b7b90edbe2b805e5.png)
![](https://img-blog.csdnimg.cn/direct/56e7347bc7ef4f2f925c7dd80c78b266.png)
![](https://img-blog.csdnimg.cn/direct/6c0603ea68494ce384e375b1d4ee498c.png)
3、background-size:设置背景图片大小(可设置为关键字:cover、contain;百分比-根据盒子尺寸计算大小;数字+单位)
contain:等比例缩放,以完全装入,可能显示空白;
cover:等比例缩放,以完全覆盖,可看不全。
![](https://img-blog.csdnimg.cn/direct/b34a66d2894244aa87a3c9389757aead.png)
![](https://img-blog.csdnimg.cn/direct/f3c8ca595e3d48db9278fb9dd0271c41.png)
![](https://img-blog.csdnimg.cn/direct/dfe31ca0399e44c3b2cc6865053ac9a9.png)
![](https://img-blog.csdnimg.cn/direct/4984075ba2bf4a5d8e9b1d7e2cb60122.png)
4、background-attachment:fixed,背景不会随元素的内容滚动。
![](https://img-blog.csdnimg.cn/direct/4242cb1d4ebc46468839fe900cf4d99a.png)
小贴士:
大背景图一般设置为background-repeat: no-repeat;background-position: center top;