Every responsive website development tutorial recommends using the display:none
CSS property to hide content from loading on mobile browsers so the website loads faster. Is it true? Does display:none
not load the images or does it still load the content on mobile browser? Is there any way to prevent loading unnecessary content on mobile browsers?
相关问题
- Adding a timeout to a render function in ReactJS
-
Why does the box-shadow property not apply to a
- Add animation to jQuery function Interval
- jQuery hover to slide?
- Issue with star rating css
Hi guys I was struggling with the same issue, how to not load an image on mobile.
But I figured out a good solution. First make an img tag and then load a blank svg in the src attribute. Now you can set your URL to the image as an inline style with content: url('link to your image');. Now wrap your img tag in a wrapper of your choice.
Set the wrapper to display none on the breakpoint where you dont want to load the image. The inline css of the img tag is now ignored since the style of an element wrapped in a wrapper with display none will be ignored, therefore the image is not loaded, until you reach a breakpoint where the wrapper has display block.
There you go, really easy way not to load an img on mobile breakpoint :)
Check out this codepen, for a working example: http://codepen.io/fennefoss/pen/jmXjvo
To prevent fetching resources, use the
<template>
element of Web Components.The trick to using display:none with images is to assign them an id. This was there is not a lot of code needed to make it work. Here is an example using media queries and 3 stylesheets. One for phone, one for tablet, and one for desktop. I have 3 images, image of a phone, a tablet, and a desktop. On a phone screen only an image of the phone will display, a tablet will display only the tablet image, a desktop displays on the desktop computer image. Here is a code example to make it work:
Source code:
The phone CSS which doesn't need a media query. Its the img#phone that makes it work:
The tablet css:
And the desktop css:
Good luck and let me know how it works for you.
If you make the image a background-image of a div in CSS, when that div is set to "display: none", the image will not load. When CSS is disabled, it still will not load, because, well, CSS is disabled.
Another possibility is using a
<noscript>
tag and placing the image inside the<noscript>
tag. Then use javascript to remove thenoscript
tag as you need the image. In this way you can load images on demand using progressive enhancement.Use this polyfill I wrote to read the contents of
<noscript>
tags in IE8https://github.com/jameswestgate/noscript-textcontent
Yes it will render faster, slightly, only because it doesn't have to render the image and is one less element to sort on the screen.
If you don't want it loaded, leave a DIV empty where you can load html into it later containing an
<img>
tag.Try using firebug or wireshark as I've mentioned before and you'll see that the files DO get transferred even if
display:none
is present.Opera is the only browser which will not load the image if the display is set to none.Opera has now moved to webkit and will render all images even if their display is set to none.Here is a testing page that will prove it:
http://www.quirksmode.org/css/displayimg.html