Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will browser download image twice if it's used in both an image's src as well as a div's background-image?

Tags:

html

browser

css

Due to reasons that are necessary but difficult to explain here I am setting both an absolutely positioned image as well as a background image on a div.

I want to make sure that once the browser has grabbed these images (they're very large) it doesn't re-grab them.

Let's pretend the image bg.jpg in the following code is 500KB. Does the following code cause the browser to download the image in its entirety twice for a total of 1000KB?

<style>     header {         background: url(img/bg.jpg) center center fixed transparent;     } </style>  <header>     <img src="img/bg.jpg" width="500" height="500"> </header> 

EDIT: Thank you all for your excellent answers. I just wanted to double check and make sure I wasn't causing the user to re-download these very large images.

like image 935
Jesse Atkinson Avatar asked Aug 01 '12 00:08

Jesse Atkinson


People also ask

How do you preload images in CSS?

Preloading images using HTML <link> Tag. These two value options of rel (relationship between the current document and the linked document) attribute are most relevant with the issue: prefetch : load the given resource while page rendering. preload : load the given resource before page rendering starts.

How do I make an image fit the screen in HTML?

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels. For example, the original image is 640×960.


2 Answers

Browsers are pretty smart when it comes to caching.It should only ask for it once.

Additionally when it asks the server for an image, it usually sends with the request for the image, a couple of headers that tell the server.. Hey, I want this image, but I got one already that has these attributes that you told me about it last time you sent it to me.

The server can then respond with a 200 meaning it's different content 304 meaning the one you have is the same, so I won't send it again, use the one you got..

One of these methods uses an ETAG header, but there are a few more.

Your server needs to support this, but most do.

Additionally, the interweb is made up of a bunch of caches, which will also look at these sort of header values and return stuff for you.. That's why the web scales so well ;-)

like image 89
grillp Avatar answered Oct 10 '22 05:10

grillp


No, the browser will cache it. Depending on the user's setting however, the browser may request the image again the next time the page is requested, like for example, if the user clears their cache between page visits.

like image 23
j08691 Avatar answered Oct 10 '22 06:10

j08691