CSS实现网页背景图片自适应全屏

时间:2019-09-29
本文章向大家介绍CSS实现网页背景图片自适应全屏,主要包括CSS实现网页背景图片自适应全屏使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

HTML:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>title</title>
</head>
<body>
<div class="wrapper">
    <!--背景图片-->
    <div id="web_bg" style="background-image: url(./img/bg.jpg);"></div>
    <!--其他代码 ... -->
</div>
</body>
</html>

CSS:

#web_bg{
  position:fixed;
  top: 0;
  left: 0;
  width:100%;
  height:100%;
  min-width: 1000px;
  z-index:-10;
  zoom: 1;
  background-color: #fff;
  background-repeat: no-repeat;
  background-size: cover;
  -webkit-background-size: cover;
  -o-background-size: cover;
  background-position: center 0;
}

下面,我们来分析一下,css中每句代码的作用是什么:

  position:fixed;
  top: 0;
  left: 0;

这三句是让整个div固定在屏幕的最上方和最左方

  width:100%;
  height:100%;
  min-width: 1000px;

上面前两句是让整个div跟屏幕实现一模一样的大小,从而达到全屏效果,而min-width是为了实现让屏幕宽度在1000px以内时,div的大小保持不变,也就是说在这种情况下,缩放屏幕宽度时,图片不要缩放(只有在1000px以内才有效)。

  z-index:-10;

这个的目的是让整个div在HTML页面中各个层级的下方,正常情况下,第一个创建的层级z-index的值是0,所以如果我们这里写成-1也可以实现,不过这里写-10是确保整个div在最下面,因为如果页面中层级太多了,有的时候用-1不一定在最下面,但如果写成-100这样大数字的也没有什么意义。用index:-10 以此能达到看上去像背景图片,其实是一个最普通的div,只是层级关系变了,才让人看上去看是背景图片。

  background-repeat: no-repeat;

上面这个是背景不要重复

  background-size: cover;
  -webkit-background-size: cover;
  -o-background-size: cover;

上面三句是一个意思,就是让图片随屏幕大小同步缩放,但是有部分可能会被裁切,不过不至于会露白,下面两句是为chrome和opera浏览器作兼容。

  background-position: center 0;

上面这句的意思就是图片的位置,居中,靠左对齐。

出处:https://www.jianshu.com/p/5480cd1a5d89

原文地址:https://www.cnblogs.com/liyunchuan/p/11608720.html