css基础-5-背景

文章目录

背景

背景常用来设置一些小的logo、精灵图或者一些超大的图片背景,背景的优点在于可以通过设置位置来实现精确定位
css基础-5-背景

属性 作用
background-color 背景颜色 默认transparent
background-image 背景图片 url(背景图片)
background-size 大小 width,height,写一个保持比例
background-repeat 重复 repeat,no-repeat,repeat-x,repeat-y
background-position 背景位置 方位词top/bottom/left/right/center;单位x-y
background-attachment 背景附着 scroll滚动,fixed固定
background 半透明背景 rgba(0,0,0,0.5),最后一个是透明度
  • 给元素设置背景图片时需要首先指定background-image,url()中可以是本地图片也可以是网址
  • 定位的时候,可以使用方位词也可以使用距离元素左上角原点的单位,第一个值是x方向,第二个是y方向
  • 所在元素必须有大小,才能显示背景图片