携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第7天,点击查看活动详情 >>
一.背景介绍
.box1{
width: 500px;
height: 500px;
/*
background-color 设置背景颜色
*/
background-color: #bfa;
复制代码
/*
background-image 设置背景图片
- 可以同时设置背景图片和背景颜色,这样背景颜色将会成为图片的背景色
- 如果背景的图片小于元素,则背景图片会自动在元素中平铺将元素铺满
- 如果背景的图片大于元素,将会一个部分背景无法完全显示
- 如果背景图片和元素一样大,则会直接正常显示
*/
background-image: url("./img/1.png");
/*
background-repeat 用来设置背景的重复方式
可选值:
repeat 默认值 , 背景会沿着x轴 y轴双方向重复
repeat-x 沿着x轴方向重复
repeat-y 沿着y轴方向重复
no-repeat 背景图片不重复
*/
background-repeat: no-repeat;
/*
复制代码
background-position 用来设置背景图片的位置
设置方式:
通过 top left right bottom center 几个表示方位的词来设置背景图片的位置
使用方位词时必须要同时指定两个值,如果只写一个则第二个默认就是center
通过偏移量来指定背景图片的位置:
水平方向的偏移量 垂直方向变量
*/
/* background-position: center; */
background-position: -50px 300px;
}
复制代码
二.背景设置
.box1{
width: 500px;
height: 500px;
overflow: auto;
background-color: #bfa;
background-image: url("./img/2.jpg");
background-repeat: no-repeat;
background-position: 0 0;
padding: 10px;
复制代码
/*
设置背景的范围
background-clip
可选值:
border-box 默认值,背景会出现在边框的下边
padding-box 背景不会出现在边框,只出现在内容区和内边距
content-box 背景只会出现在内容区
background-origin 背景图片的偏移量计算的原点
padding-box 默认值,background-position从内边距处开始计算
content-box 背景图片的偏移量从内容区处计算
border-box 背景图片的变量从边框处开始计算
*/
/* background-origin: border-box;
background-clip: content-box; */
/*
background-size 设置背景图片的大小
第一个值表示宽度
第二个值表示高度
- 如果只写一个,则第二个值默认是 auto
cover 图片的比例不变,将元素铺满
contain 图片比例不变,将图片在元素中完整显示
*/
background-size: contain;
/*
background-color
background-image
background-repeat
background-position
background-size
background-origin
background-clip
background-attachment
复制代码
- backgound 背景相关的简写属性,所有背景相关的样式都可以通过该样式来设置
并且该样式没有顺序要求,也没有哪个属性是必须写的
注意:
background-size必须写在background-position的后边,并且使用/隔开
background-position/background-size
background-origin background-clip 两个样式 ,orgin要在clip的前边
*/
}
.box2{
width: 300px;
height: 1000px;
background-image: url('./img/1.png');
background-repeat: no-repeat;
background-position: 100px 100px;
/*
background-attachment
- 背景图片是否跟随元素移动
- 可选值:
scroll 默认值 背景图片会跟随元素移动
fixed 背景会固定在页面中,不会随元素移动
*/
background-attachment: fixed;
}
.box3{
border: 10px red double;
padding: 50px;
width: 500px;
height: 500px;
background: url('./img/2.jpg') #bfa center center/contain border-box content-box no-repeat ;
}
三.总结
本文就主要介绍这么多内容了,如想知道更多内容接下来尽情期待吧!