分享让网页背景图片在任意分辨率屏幕都能平铺的方法

Author posted @ 2014年8月17日 16:39 in Html5 , 2917 阅读

看到了一篇很好的文章"Perfect Full Page Background Image".文中总结了多种让图片铺满整个屏幕的方法,不论屏幕的分辨率是多少。有用纯CSS实现的, 也有用 JQuery实现的,各种方法都提供了示例。

个人喜欢由Corey Worrell提供的纯CSS方法。这里没有直接用background-image,而是直接用的foreground image作为背景图。html部分如下:

<div id="bg">
  <img src="images/bg.jpg" alt="background image">
</div>

css部分:

#bg {
  position: fixed; 
  top: -50%; 
  left: -50%; 
  width: 200%; 
  height: 200%;
}
#bg img {
  position: absolute; 
  top: 0; 
  left: 0; 
  right: 0; 
  bottom: 0; 
  margin: auto; 
  /* preserve aspet ratio */
  min-width: 50%;
  min-height: 50%;
}

思路是先创建一个<div id="bg">,这个div的大小是窗口大小的两倍,位置是fixed,起始坐标为top:-50%; left:-50%;。然后把图片的位置设为top:0; left: 0; 再设置minm-width和min-height属性来保证图片大小不小于div的50%。由于这个div的大小是窗口的两倍,这样图片的大小就正好与窗口大小一样,并且是centered。

TV LED SONY 说:
2018年5月29日 04:08

思路是先创建一个<div id="bg">,这个div的大小是窗口大小的两倍,位置是fixed,起始坐标为top:-50%; left:-50%;。

sk 说:
2021年7月14日 03:12

Is anyone needing help in finding the door that you need? I heard a lot of good stuff about Caldwells. <a href="https://caldwells.com/">https://caldwells.com/</a> this link if you want to see their products.

sk 说:
2021年8月07日 10:53

As I web site possessor I believe the content matter here is rattling magnificent , appreciate it for your hard work. You should keep it up forever! Best of luck. facial equipment for sale

sk 说:
2021年8月08日 16:04

I?m certain there are a lot of added nice instances in the long term for individuals who study your website. Zemits Holistic Bionexis The New Generation of Holistic Body Sculpting

sk 说:
2021年9月10日 02:55

Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also canada visum online

sk 说:
2021年9月13日 07:30 我们的美国代写类型包含不仅限于:网课代修、EXAM代考、GRE/GMAT代考、AP代考、QUIZ代考、单元代考、写作类ESSAY,PAPER,REPORT,RESEARCH。作业类:ASSIGNMENT,HOMEWORK,PPT,CASE STUDY,REVIEW,CS编程等,数据分析 网课类:覆盖90% 人文社科,商,经,管。有需要随时可以扫描二维码或QQ/微信 7878393. exam代考
sk 说:
2021年9月13日 09:33

This is my first visit to your web journal! We are a group of volunteers and new activities in the same specialty. Website gave us helpful data to work. Good Morning Messages for Friends

Adilkhatri 说:
2021年10月05日 19:10

A debt of gratitude is in order for the instructive and accommodating post, clearly in your blog everything is great.. 바카라사이트

seo 说:
2021年10月07日 05:00

Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!THANKS!!!!!! buy now

seo 说:
2021年10月09日 03:00

Spot on with this write-up, I really assume this website needs much more consideration. I’ll probably be once more to read far more, thanks for that info. loweslife

merckseo 说:
2021年10月10日 01:41

Best Social Plan is the most trusted company for providing social media marketing services. Within some years it has gained popularity as a reliable company to its customers. This company is now managing its activity successfully. It is proved number one company by its quality services. It is noticed that more and more customers are showing their intention to take service from this company. Best Social Plan

seo 说:
2021年10月12日 08:28

오피를 모두 모아둔 곳에서 원하는 종류의 달콤월드,오피쓰,오피그램 등에서 전국에 있는 OP를 즐길 수 있는 완벽한 오피사이트를 모아 추천하고 쉽게 한눈에 선택이 가능합니다. 처음 오피를 이용하는 모든 분들에게 가장 편리한 구성으로 준비하여 안내해드립니다. 오피그램

Adilkhatri 说:
2021年10月14日 00:34

I would prescribe my profile is vital to me, I welcome you to examine this subject... MGWIN88

seo 说:
2021年10月15日 04:43

Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!Thanks 대출나라

seo 说:
2021年10月17日 07:55

I just want to tell you that I’m very new to weblog and honestly liked this web site. More than likely I’m planning to bookmark your blog post . You certainly come with exceptional articles and reviews. Bless you for sharing your web site. Buy Alopexy

adilkhatri 说:
2021年10月17日 18:25

Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too. There tend to be not many people who can certainly write not so simple posts that artistically. Continue the nice writing. 토토사이트

adilkhatri 说:
2021年10月18日 18:31

A debt of gratitude is in order for the instructive and accommodating post, clearly in your blog everything is great.. Visa ya Marekani ESTA

adilkhatri 说:
2021年10月18日 20:17

For this site, you will see our record, make sure to experience this information. 토토사이트

seo 说:
2021年10月19日 02:18

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. https://mobiledsng.com/product/cialis-20mg-price-in-pakistan

adilkhatri 说:
2021年10月21日 23:26

Incredible Blog! I might want to thank for the endeavors you have made in composing this post. I am trusting a similar best work from you later on also. I needed to thank you for this sites! Much obliged for sharing. Incredible sites! realizzazione siti web Milano

adilkhatri 说:
2021年10月23日 20:41

Decent data, profitable and fantastic plan, as offer well done with smart thoughts and ideas, loads of extraordinary data and motivation, both of which I require, on account of offer such a supportive data here. indian visa

adilkhatri 说:
2021年10月23日 23:57

I expounded on a comparable issue, I give you the connection to my site. طلب تأشيرة كندا عبر الإنترنت

adilkhatri 说:
2021年10月24日 02:32

Recognizes for paper such an advantageous creation, I lurched adjacent to your blog other than decode a restricted report. I need your system of engraving... Sewa Excavator

merckseo 说:
2021年10月25日 22:35

You re in purpose of actuality a without flaw website admin. The site stacking speed is astonishing. It sort of feels that you're doing any unmistakable trap. Besides, The substance are perfect work of art. you have completed a fabulous movement regarding this matter!  agen sbobet

merckseo 说:
2021年10月26日 19:31

Amid this site, you will see this shape, I very suggest you take in this audit.  안전공원


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter