使用background-size让背景图按比例适应div
用法:
background:url(’…/…png’) center center no-repeat
background-size:cover
background-size 设置背景图片大小
属性值:
- contain
让背景图完整显示,
若设置repeat会平铺满div,多余部分切除
no-repeat则可能导致页面部分空白。 - cover
让背景图片按照自身比例填满div,可能会导致图片部分未显示,
且在未设置attachment的情况下默认为scroll,当窗口缩放时背景图会出现空白页 - 具体数值或者百分比,如果只有有一个则按比例缩放
页面缩放时出现空白的解决方法:
窗口缩小时出现空白,将设置位置值center center 后一个改成attachment的fixed值就可以自适应且不出现空白,这里的fixed是保证在cover的情况下背景图不会随着页面的滚动缩放而缩放
background知识点
-
background-attachment 属性设置背景图像是否固定或者随着页面的其余部分滚动即fixed和scroll
-
background-image:使用的背景图像
background-color:背景颜色
background-repeat:如何重复背景图像,no-repeat 背景图像将仅显示一 次,repeat 默认将在垂直方向和水平方向重复,repeat-x,repeat-y. -
background-position:设置背景图像的起始位置
简写:background:color image repeat attachment position