CSS背景

360 阅读1分钟

背景颜色(color)

语法:

background-color: 颜色值; // 默认值是 transparent 透明的

背景图片(image)

语法:

background-image: url(images/1.jpg); // 必须加url,url 里面的地址不要加""

注意:背景图片是在盒子的底部

背景平铺(repeat)

语法:

background-repeat: repeat | no-repeat | repeat-x | repeat-y; // 平铺(默认)|不平铺|x轴平铺|y轴平铺

背景位置(position)

background-position: x坐标 y坐标; // 如 10px 50px;
background-position: right top; // 右上角
background-position: left bottom; // 左下角
background-position: center center; // 居中
background-position: left | right center; // x轴靠左|右,y轴居中
background-position: center top | bottom; // y轴靠上|下,x轴居中

背景附着(attachment)

background-attachment: scroll | fixed; // 默认是scroll,背景图像随对象内容滚动

背景透明(CSS3)

background: rgba(0,0,0,0.2);

注意:

最后一个参数alpha透明度,取值范围 0~1之间

习惯把0.2的0去掉,这样写 background: rgba(0,0,0,.2);

背景半透明指的是盒子背景半透明,盒子里面的内容不受影响

插入图片和背景图片区别

  • 插入图片,用的最多,比如产品展示类,移动位置只能靠盒模型 padding margin
  • 背景图片一般用于小图标背景,或者超大背景图,背景图片只能通过 background-position