元素设置背景图片,图片在页面上的属性由,大小,位置,显示比例,是否移动,图片源构成。
图片的来源可以是本地路径,也可以是BASE64字符串,具体信息如下。
.bg{
width: 200px;
height: 2000px;
background-color: #7e8c8d;
background-image: url("../../images/star.png");
/*背景重复
no-repeat 不重复
repeat 重复
repeat-y y轴重复
repeat-x x轴重复
*/
background-repeat: no-repeat;
/*背景图片位置
left center 代表左中,其他同理
10px 10px 代表偏移量,一个水平,一个垂直。
*/
background-position: center center;
/*设置背景的范围,两个配合使用
border-box 会出现在边框的下面
padding-box 只会出现在内容区和内边距
content-box 只会出现在内容区
background-origin 背景图片的原点
*/
background-clip: content-box;
background-origin: content-box;
/*设置图片大小
第一个代表宽度,第二个代表高度
只写第一个,第二个不写会等比例进行缩放,默认值为auto
第一个写auto,第二个写同理
cover 图片比例不变,将元素填满
contain 图片比例不变,在元素中显示完整
*/
background-size: 200px 200px;
/*元素背景元素是否跟随滚动
scroll 默认值,跟随滚动
fixed 固定,不移动
*/
background-attachment: fixed;