CSS实现自适应不同大小屏幕的背景大图的两种方法
一张清晰漂亮的背景图片能给网页加分不少,设计师也经常会给页面的背景使用大图,我们既不想图片因为不同分辨率图片变形,也不希望当在大屏的情况下,背景有一块露白,简而言之,就是实现能自适应屏幕大小又不会变形的背景大图,而且背景图片不会随着滚动条滚动而滚动。
用CSS实现真的很简单很简单,下面我们来看一下第一种方法具体的代码:
HTML代码:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 ????<meta http-equiv="content-type" content="text/html;charset=utf-8"/> 5 ????<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 6 ????<title>title</title> 7 </head> 8 <body> 9 <div class="wrapper">10 ????<!--背景图片-->11 ????<div id="web_bg" style="background-image: url(./img/bg.jpg);"></div>12 ????<!--其他代码 ... -->13 </div>14 </body>15 </html>
CSS代码:
1 #web_bg{ 2 ??position:fixed; 3 ??top: 0; 4 ??left: 0; 5 ??width:100%; 6 ??height:100%; 7 ??min-width: 1000px; 8 ??z-index:-10; 9 ??zoom: 1;10 ??background-color: #fff;11 ??background-repeat: no-repeat;12 ??background-size: cover;13 ??-webkit-background-size: cover;14 ??-o-background-size: cover;15 ??background-position: center 0;16 }
你看,代码就是这么简单。
下面,我们来分析一下,css中每句代码的作用是什么:
1 position:fixed;2 ??top: 0;3 ??left: 0;
这三句是让整个div固定在屏幕的最上方和最左方
1 ??width:100%;2 ??height:100%;3 ??min-width: 1000px;
上面前两句是让整个div跟屏幕实现一模一样的大小,从而达到全屏效果,而min-width是为了实现让屏幕宽度在1000px以内时,div的大小保持不变,也就是说在这种情况下,缩放屏幕宽度时,图片不要缩放(只有在1000px以内才有效)。
1 ??z-index:-10;
这个的目的是让整个div在HTML页面中各个层级的下方,正常情况下,第一个创建的层级z-index的值是0,所以如果我们这里写成-1也可以实现,不过这里写-10是确保整个div在最下面,因为如果页面中层级太多了,有的时候用-1不一定在最下面,但如果写成-100这样大数字的也没有什么意义。用index:-10 以此能达到看上去像背景图片,其实是一个最普通的div,只是层级关系变了,才让人看上去看是背景图片。
1 zoom: 1;
这个的目的是为了兼容IE浏览器
1 background-repeat: no-repeat;
上面这个是背景不要重复
1 ?background-size: cover;2 ??-webkit-background-size: cover;3 ??-o-background-size: cover;
上面三句是一个意思,就是让图片随屏幕大小同步缩放,但是有部分可能会被裁切,不过不至于会露白,下面两句是为chrome和opera浏览器作兼容。
1 ??background-position: center 0;
上面这句的意思就是图片的位置,居中,靠左对齐
第二种方法的代码
HTML代码如下:
1 <div class="about-me">2 ??????3 ??????<a href="http://iheima.com" class="introduction">4 ??????????<h3>About me</h3>5 ??????</a>6 </div>
css代码如下:
1 .about-me { 2 ????????????position: relative; 3 ????????????min-width: 1480px; 4 ????????????width: 100%; 5 ????????????height: 520px; 6 ????????????overflow: hidden; 7 ????????????img.me { 8 ????????????????position: absolute; 9 ????????????????bottom: 0;10 ????????????????left: 0;11 ????????????????width: 100%;12 ????????????????height: auto;13 ????????????????min-height: 520px;14 ????????????????z-index: 0;15 ????????????}16 ????????????.introduction {17 ????????????????display: block;18 ????????????????position: absolute;19 ????????????????left: 0;20 ????????????????bottom: 0;21 ????????????????right: 0;22 ????????????????top: 0;23 ????????????????width: 100%;24 ????????????????height: 100%;25 ????????????????color: #fff;26 ????????????????background: rgba(0, 0, 0, .6);27 ????????????????z-index: 2;28 ????????????????cursor: pointer;29 ????????????????h3 {30 ????????????????????margin: 100px auto 140px;31 ????????????????????width: 170px;32 ????????????????????height: 90px;33 ????????????????????line-height: 90px;34 ????????????????????font-size: 36px;35 ????????????????????border-bottom: 2px solid #0b6d99;36 ????????????????}37 ????????????}38 ????????}
CSS实现自适应不同大小屏幕的背景大图的两种方法(转自简书)
原文地址:https://www.cnblogs.com/zyxsblogs/p/9820928.html