View Full Version : How to make images not load on a mobile site?
PaulV
11-28-2013, 01:33 PM
I know i can use display:none, but the page has a lot of pics. The mobile page has less content and few pics. To load quicker and waste less bandwith i want to pics not to load on a mobile phone.
How can I do that?
Thx for the replu, but i guess i was not clear.
I make one page with responsive design. I have the same html, but to CSS, one for big and one for small screens.
The CSS for small screens makes the page look different, much simpler and easy to read on a mobile screen.
I hide most pics and other superfluous content with display:none.
But it seems that still the images will needlessly load, therefore slowing the site down.
How to stop content from loading if i don't need it?
jscheuer1
11-28-2013, 08:38 PM
I'm not exactly sure what you're asking. It sounds like you might have two separate pages. Or that you want one page to appear differently depending upon whether it's being viewed on a mobile device or not. Generally javascript and/or media queries (a type of css) are used for these sorts of things. Javascript can device sniff (read the navigator.userAgent string looking for keywords like 'iphone' and 'android', etc.) and do different things based upon that. Media queries can structure different css dependent upon screen size, so a certain class of images and/or other elements (like ones with text and/or images in them) could be display none at smaller screen sizes.
Display none images generally will not load unless made visible. If that's good enough, media queries would be the way to go. If you need it to be more certain, javascript could actually remove some or all images from the DOM before they load. Or even more foolproof (from the point of view of preventing them from even beginning to load), not insert them unless a non-mobile device is detected.
djr33
11-28-2013, 09:28 PM
Making them "not load" is harder than making them load. That is, if you want to use some kind of "if" statement, it would probably be easier to do this on the computer than on the phone. So you can check roughly if (!mobile_device) {...
But as John suggested, having two pages (or stylesheets) would be a better approach. Using Javascript to detect a mobile browser isn't entirely reliable and having images conditionally display could cause other complications on an otherwise unproblematic website.
Beverleyh
11-29-2013, 01:30 PM
I was reading an interesting article the other day on media queries: http://zomigi.com/blog/essential-considerations-for-crafting-quality-media-queries/ and part of the discussion covered images downloaded (or not) inside of media queries.
The whole article is very interesting, and you will probably pick up a load of other useful ideas. Anyway, a link within that page goes to a website that shows test cases for methods to stop images from being downloaded on mobile devices. The full article is here: http://timkadlec.com/2012/04/media-query-asset-downloading-results/#test_three_background_image_parent_object_set_to_display_none
The concluding part of the post recommends 2 methods for avoiding image downloads on mobile;
1 - Set an image as a background on a div, and then instead of hiding that div, you hide the div's parent element
<div id="test3">
<div></div>
</div>
#test3 div {
background-image:url('images/test3.png');
width:200px;
height:75px;
}
@media all and (max-width: 600px) {
#test3 {
display:none;
}
}
2 - Use stacked media queries to serve all images (set as backgrounds on divs) to different sized screens
<div id="test5"></div>
@media all and (min-width: 601px) {
#test5 {
background-image:url('images/test5-desktop.png');
width:200px;
height:75px;
}
}
@media all and (max-width: 600px) {
#test5 {
background-image:url('images/test5-mobile.png');
width:200px;
height:75px;
}
}
Caveat on method 2
...if you use this method, you’ll need to consider alternate options for Internet Explorer 8 and under. Those versions of the browser don’t support media queries, so no image will be applied. Of course, this is simple enough to fix with conditional comments and an IE specific stylesheet.
Beverleyh
11-29-2013, 01:56 PM
Display none images generally will not load unless made visible. If that's good enough, media queries would be the way to go.
I just wanted to draw attention to the first 2 test results at http://timkadlec.com/2012/04/media-query-asset-downloading-results/#test_three_background_image_parent_object_set_to_display_none , which I think shows the 2 most common uses of display:none; - unfortunately these will still cause the images to download.
It's only when media queries are used as part of the 2 recommended methods, that display:none; stops the download.
Senid
10-27-2017, 10:39 PM
This is an old post but since it has a very good position on google due to the general topic then I'll add the information requested here:
The answer is the new picture tag:
<picture>
<source media="(min-width: 650px)" srcset="img_pink_flowers.jpg">
<source media="(min-width: 465px)" srcset="img_white_flower.jpg">
<img src="img_orange_flowers.jpg" alt="Flowers" style="width:auto;">
</picture>
Browser support information:
http://caniuse.com/#feat=picture
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.