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

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

看到了一篇很好的文章"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.  안전공원

adilkhatri 说:
2021年10月27日 18:48

Here at this site extremely the critical material accumulation so everyone can appreciate a great deal. siti internet bologna

adilkhatri 说:
2021年10月27日 19:28

I suggest just great and dependable data, so observe it: Balika Vadhu

adilkhatri 说:
2021年10月28日 23:07

The issue you we included above is certainly regard proficient for anyone to work out. the possibility of your article is substantial and It will realize a positive way. 사설토토

Get 5 Chances To Win 说:
2021年10月30日 08:39

This is a truly good site post. Not too many people would actually, the way you just did. I am really impressed that there is so much information about this subject that have been uncovered and you’ve done your best, with so much class. If wanted to know more about green smoke reviews, than by all means come in and check our stuff. https://financehelper.net/

adilkhatri 说:
2021年11月01日 18:13

I am continually hunting on the web down articles that can help me. There is clearly a great deal to think about this. I think you made some great focuses in Features too. Continue working, extraordinary job ! climbing carabiner

adilkhatri 说:
2021年11月01日 18:22

I suggest just great and dependable data, so observe it: binoculars for hunting deer

adilkhatri 说:
2021年11月01日 18:31

You should essentially phenomenal also strong guidance, which implies take note: electric tiller

adilkhatri 说:
2021年11月01日 18:42

This is exceptionally fascinating, yet it is important to tap on this connection: violin strings

seo 说:
2021年11月14日 21:51

This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you. slotworld1234

seo 说:
2021年12月06日 01:36

This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. gclub

seo 说:
2022年1月03日 06:33

Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Thanks Weather Channel Live Stream

seoo 说:
2022年1月13日 09:17 Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene. 백링크
merckseo 说:
2022年3月08日 23:15

Here at this site extremely the critical material accumulation so everyone can appreciate a great deal.  Buy LinkedIn Followers

Assam 9th Class Mode 说:
2022年8月24日 21:49

The Board of Secondary Education, Assam, often known as SEBA, will conduct the 9th grade half-year and annual exams in primary and secondary schools in 2023. The SEBA Class 9th Question Paper 2023 is available here. Class 9 Assam 9th Class Model Paper 2023 Assamese Solutions, Class 9 Assamese Book Solutions, and SEBA Class 9 Assamese Question Answers SEBA Book for Class 9 in Assamese SEBA Class 9 Assamese Solutions, Class 9 Assamese Book Solutions, and EBA Class 9 Assamese Question Answers 9th Grade Assamese Book SEBA

Baby-to-Be Box 说:
2023年8月27日 19:39

Anticipation meets excitement with our Baby-to-Be Box. Designed to pamper expectant parents as they await their baby's arrival, this box is brimming with items that create a sense of joy and readiness. From soothing teas for the mom-to-be to adorable onesies for the little one, this box captures the magical moments of pregnancy and the promise of the future.

St Lucia Vacation Pa 说:
2023年8月27日 23:52

Explore the breathtaking beauty of Saint Lucia with our curated St Lucia vacation packages. Discover an array of options tailored to your preferences, combining top-notch accommodations, thrilling tours, and unforgettable experiences. Dive into the heart of this tropical paradise with Tour Saint Lucia's meticulously designed vacation packages that promise relaxation, adventure, and the essence of Caribbean hospitality.

civaget 说:
2024年1月11日 04:08

the air compressors that we use at home are the high powered ones, we also use it for cleaning. how do i make google docs dark mode


登录 *


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