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
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.
Just expanding on Brent's solution.
You can do the following for a pure CSS solution, it also makes the img box actually behave like an img box in a responsive design setting (that's what the transparent png is for), which is especially useful if your design uses responsive-dynamically-resizing images.
The image will only be loaded when the media query tied to visible-lg-block is triggered and display:none is changed to display:block. The transparent png is used to allow the browser to set appropriate height:width ratios for your <img> block (and thus the background-image) in a fluid design (height: auto; width: 100%).
So you end up with something like the following, for 3 different viewports:
And only your default media viewport size images load during the initial load, then afterwards, depending on your viewport, images will dynamically load.
And no javascript!
HTML5
<picture>
tag will help you to resolve the right image source depending on the screen widthApparently the browsers behaviour hasn't changed much over the past 5 years and many would still download the hidden images, even if there was a
display: none
property set on them.Even though there's a media queries workaround, it could only be useful when the image was set as a background in the CSS.
While I was thinking that there's just a JS solution to the problem (lazy load, picturefill, etc.), it appeared that there's a nice pure HTML solution that comes out of the box with HTML5.
And that is the
<picture>
tag.Here's how MDN describes it:
And here's how to use it:
The logic behind
The browser would load the source of the
img
tag, only if none of the media rules applies. When the<picture>
element is not supported by the browser, it will again fallback to showing theimg
tag.Normally you'd put the smallest image as the source of the
<img>
and thus not load the heavy images for larger screens. Vice versa, if a media rule applies, the source of the<img>
will not be downloaded, instead it will download the url's contents of the corresponding<source>
tag.Only pitfall here is that if the element is not supported by the browser, it will only load the small image. On the other hand in 2017 we ought to think and code in the mobile first approach.
And before someone got too exited, here's the current browser support for
<picture>
:Desktop browsers
Mobile browsers
More about the browser support you can find on Can I use.
The good thing is that html5please's sentence is to use it with a fallback. And I personally intend to take their advise.
More about the tag you can find in the W3C's specification. There's a disclaimer there, which I find important to mention:
So what it says is that it only helps you improve the performance when loading an image, by providing some context to it.
Anyhow, you can still try to make a dodgy hack to hide the image on small devices:
Thus the browser will not display the image and will only download the 1x1 pixel image, which can be cached if you use it more than once. Anyhow it's really not recommended to do that, because if the tag is not supported even the desktop browsers would show only the 1x1px image.
The background-image of a div element will load if the div is set do 'display:none'.
Anyway, if that same div has a parent and that parent is set to 'display:none', the background-image of the child element will not load. :)
Example using bootstrap:
Those images are loaded. The browser, due to the possibility of a script dynamically checking an element of the DOM, doesn't optimize elements (or elements content) away.
You may check it there : http://jsfiddle.net/dk3TA/
The image has a
display:none
style but its size is read by the script. You could also have checked it by looking at the "network" tab of your browser's developer tools.Note that if the browser is on a small CPU computer, not having to render the image (and layout the page) will make the whole rendering operation faster but I doubt this is something that really makes sense today.
If you want to prevent the image from loading you may simply not add the IMG element to your document (or set the IMG
src
attribute to"data:"
or"about:blank"
).EDIT :
Browsers are getting smarter. Today your browser (depending on the version) might skip the image loading if it can determine it's not useful.
Use @media query CSS, basically we just release a project where we had an enormous image of a tree on desktop at the side but not showing in table/mobile screens. So prevent image from loading its quite easy
Here is a small snippet:
You can use the same CSS to show and hide with display/visibility/opacity but image was still loading, this was the most fail safe code we came up with.
Quirks Mode: images and display: none