I am building a responsive website using media queries. I need to switch to a different navigation method for very small screens.
For desktop/tablet screens, I am using a sprite based UL/LI list method. For small smart phone screens, I will have simple link text.
If I use, Display: none; to hide the sprite based navigation for smart phones, will the sprite image still be loaded, but just not shown? Do I need to parse the image reference in my css media query for smart phones? Or should I just leave the image reference out of the initial css altogether since I am designing small to large (i.e. the default css is for small screens, and then media queries change things as the screen gets larger).
Images or wrapper elements that have display: none set, will still get loaded. So if you want to eg. load a different image for desktop and mobile, you can either: use display: none , but with the loading="lazy" parameter set on img .
Display none don't improve the performance because the goal of virtual scrolling is reduce the number of the elements into the dom. Make an element display none or remove an element, trigger a reflow, but with display none you worst the performance because you don't have the benefit of reduce the dom.
visibility:hidden hides the element, but it still takes up space in the layout. display:none removes the element from the document. It does not take up any space.
The autoplay attribute is of higher importance than preload=”none” and so the video will begin to immediately download and playback – just like it does today. On mobile devices, there is no autoplay, and the preload=”none” prevents the browser from downloading the video.
To answer your question, display: none
does not reduce load time. It still loads the content/classes/code in question, but the browser doesn't display/render them.
It sounds like you're using a mobile-first approach, so you could either:
Load all assets/classes/scripts regardless of mobile/tablet/desktop class you're aiming for and adapt the layouts using your media queries.
Load the required assets/classes/scripts as and only when the media query states change. The advantage of this is that the experience would be more relative the the device-type in question:
If you consider looking at option 2, then there are a variety of open-source asset-loaders that allow you to load CSS and Javascript code based upon media query state changes. [Note: More effort/design would be required to use this technique].
A simplified example of this using enquire.js (there are others asset loaders) would allow you to do the following:
<script type="text/javascript">
// MQ Mobile
enquire.register("screen and (max-width: 500px)", {
match : function() {
// Load a mobile JS file
loadJS('mobile.js');
// Load a mobile CSS file
loadCSS('mobile.css');
}
}).listen();
// MQ Desktop
enquire.register("screen and (min-width: 501px)", {
match : function() {
// Load a desktop JS file
loadJS('desktop.js');
// Load a desktop CSS file
loadCSS('desktop.css');
}
}).listen();
</script>
So, if a browser is 501px or above in width, then both desktop.js
and desktop.css
would load - enabling features/assets that aren't available under 501px and that aren't required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With